- 浏览: 92982 次
- 性别:
- 来自: 广州
最新评论
-
onleoneggp:
...
MyEclipse for spring 注册码 -
9976:
谢谢。可以用。
myeclipse 8.6 M1 注册码 收藏 -
greatghoul:
请教,如果xml是utf-8格式的,如何在属性中写入中文.
简单的Dom4j写入XML文件例子 -
fffddgx:
不错 有收获,谢谢
报错信息 -
lufei_vs:
引用
利用反射机制模拟Hibernate的Session
首先,建一个web project,然后添加对hibernate和spring的支持,我们使用的是hibernate3.1和spring2.0,然后导入dwr.jar和commons-pool-1.3.jar(不知道myeclipse怎么搞的,添加Spring功能支持的时候就有了commons-dbcp.jar,居然没有其依赖的commons-pool-x.jar,只好单独添加了,另外,需要将Spring2.0 AOP Liberaries里的asm2.2.3.jar删除,因为好像和Hiberate中的生成代理用的asm.jar冲突,我把Spring2.0 AOP Liberaries排到最后仍然有冲突,所以只好删掉了,不知道大家遇到过这种情况么)。我们使用myeclise自带的Derby数据库,在里面建一个表BOOK:
ID bigint primary key,autoincrement
NAME varchar(20)
ISBM varchar(20)
AUTHOR varchar(15)
然后利用myeclipse的hibernate反向工程生成领域模型:Book.java, DAO:BookDAO.jar, Book 的映射文件Book.hbm.xml:
生成的代码及配置文件如下:
package edu.jlu.fuliang.domain; /** *//** * Book generated by MyEclipse Persistence Tools */ public class Book implements java.io.Serializable { // Fields private Long id; private String name; private String isbm; private String author; // Constructors /** *//** default constructor */ public Book() { } /** *//** minimal constructor */ public Book(Long id, String name, String isbm) { this.id = id; this.name = name; this.isbm = isbm; } /** *//** full constructor */ public Book(Long id, String name, String isbm, String author) { this.id = id; this.name = name; this.isbm = isbm; this.author = author; } // Property accessors public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getIsbm() { return this.isbm; } public void setIsbm(String isbm) { this.isbm = isbm; } public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } public String toString(){ return "[id=" + id + ",name=" + name + ",isbm=" + isbm + ",author=" + author + "]"; } }
edu.jlu.fuliang.dao.BookDAO.java: package edu.jlu.fuliang.dao; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.springframework.context.ApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import edu.jlu.fuliang.domain.Book; /** *//** * Data access object (DAO) for domain model class Book. * * @see edu.jlu.fuliang.domain.Book * @author MyEclipse Persistence Tools */ public class BookDAO extends HibernateDaoSupport ...{ private static final Log log = LogFactory.getLog(BookDAO.class); // property constants public static final String NAME = "name"; public static final String ISBM = "isbm"; public static final String AUTHOR = "author"; protected void initDao() ...{ // do nothing } public void save(Book transientInstance) ...{ log.debug("saving Book instance"); try ...{ getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) ...{ log.error("save failed", re); throw re; } } public void delete(Book persistentInstance) ...{ log.debug("deleting Book instance"); try ...{ getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) ...{ log.error("delete failed", re); throw re; } } public Book findById(java.lang.Long id) ...{ log.debug("getting Book instance with id: " + id); try ...{ Book instance = (Book) getHibernateTemplate().get( "edu.jlu.fuliang.domain.Book", id); return instance; } catch (RuntimeException re) ...{ log.error("get failed", re); throw re; } } public List findByExample(Book instance) ...{ log.debug("finding Book instance by example"); try ...{ List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) ...{ log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) ...{ log.debug("finding Book instance with property: " + propertyName + ", value: " + value); try ...{ String queryString = "from Book as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) ...{ log.error("find by property name failed", re); throw re; } } public List findByName(Object name) ...{ return findByProperty(NAME, name); } public List findByIsbm(Object isbm) ...{ return findByProperty(ISBM, isbm); } public List findByAuthor(Object author) ...{ return findByProperty(AUTHOR, author); } public List findAll() ...{ log.debug("finding all Book instances"); try ...{ String queryString = "from Book"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) ...{ log.error("find all failed", re); throw re; } } public Book merge(Book detachedInstance) ...{ log.debug("merging Book instance"); try ...{ Book result = (Book) getHibernateTemplate().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) ...{ log.error("merge failed", re); throw re; } } public void attachDirty(Book instance) ...{ log.debug("attaching dirty Book instance"); try ...{ getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) ...{ log.error("attach failed", re); throw re; } } public void attachClean(Book instance) ...{ log.debug("attaching clean Book instance"); try ...{ getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) ...{ log.error("attach failed", re); throw re; } } public static BookDAO getFromApplicationContext(ApplicationContext ctx) ...{ return (BookDAO) ctx.getBean("BookDAO"); } }Book.hbm.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="edu.jlu.fuliang.domain.Book" table="BOOK" schema="CLASSICCARS"> <id name="id" type="java.lang.Long"> <column name="ID" /> <generator class="identity" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="20" not-null="true" /> </property> <property name="isbm" type="java.lang.String"> <column name="ISBM" length="20" not-null="true" unique="true" /> </property> <property name="author" type="java.lang.String"> <column name="AUTHOR" length="15" /> </property> </class> </hibernate-mapping> 下面我们配置一下Spring,我们把applicationContext.xml分成了三个,分别是applicationContext-db.xml,applicationContext-dao.xml,applicationContext-service.我们看看如何配置: applicationContext-db.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"> </property> <property name="url" value="jdbc:derby://localhost:1527/myeclipse;create=true"> </property> <property name="username" value="classiccars"></property> <property name="password" value="myeclipse"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>edu/jlu/fuliang/domain/Book.hbm.xml</value> </list> </property> </bean> </beans> applicationContext-dao.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="bookDAO" class="edu.jlu.fuliang.dao.BookDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans> 接下来我们来写我们的Service层: edu.jlu.fuliang.service.BookManageService.java package edu.jlu.fuliang.service; import java.util.List; import edu.jlu.fuliang.domain.Book; public interface BookManageService { public List<Book> getAllBooks(); public List<Book> getBookByName(String name); public void updateBook(Book book); public void addBook(Book book); public void deleteBook(long id); } edu.jlu.fuliang.service.impl.BookManageServiceImpl.java: package edu.jlu.fuliang.serviceImpl; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import edu.jlu.fuliang.dao.BookDAO; import edu.jlu.fuliang.domain.Book; import edu.jlu.fuliang.service.BookManageService; public class BookManageServiceImpl implements BookManageService{ private static final Log log = LogFactory.getLog(BookManageServiceImpl.class); private BookDAO bookDAO; @Override public void addBook(Book book) { bookDAO.save(book); } @Override public void deleteBook(long id) { Book book = bookDAO.findById(id); bookDAO.delete(book); } @Override public List<Book> getAllBooks() { return bookDAO.findAll(); } @Override public List<Book> getBookByName(String name) { return bookDAO.findByName(name); } @Override public void updateBook(Book book) { log.info(book); bookDAO.attachDirty(book); } public BookDAO getBookDAO() { return bookDAO; } public void setBookDAO(BookDAO bookDAO) { this.bookDAO = bookDAO; } } 然后我们来配置Service和事务: applicationContext-service.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="bookManageServiceTarget" class="edu.jlu.fuliang.serviceImpl.BookManageServiceImpl"> <property name="bookDAO"> <ref bean="bookDAO"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="bookManageService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="target" ref="bookManageServiceTarget"/> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> </beans> 最后我们来把dwr整合进来: 我们在web.xml添加ContextLoaderListener来加载applicationContext-db.xml,applicationContext-dao.xm,applicationContext-service.xml,以及配置DwrServlet: web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>在dwr.xml中配制bean的创建方式,类型转换,以及Service要暴露的接口: <dwr> <allow> <convert converter="bean" match="edu.jlu.fuliang.domain.Book"/> <create creator="spring" javascript="BookManageService" > <param name="beanName" value="bookManageService"/> <include method="getAllBooks"/> <include method="getBookByName"/> <include method="updateBook"/> <include method="addBook"/> <include method="deleteBook"/> </create> </allow> </dwr>
最后,我们编写jsp页面index.jsp: <html> <head><title>DWR test</title> <script type='text/javascript' src='/DWRTest/dwr/interface/BookManageService.js'></script> <script type='text/javascript' src='/DWRTest/dwr/engine.js'></script> <script type='text/javascript' src='/DWRTest/dwr/util.js'></script> <script type="text/javascript"> var bookCache = ...{}; var currentBook = null; function loadAllBooks(){ BookManageService.getAllBooks(handleGetAllBooks,handleGetAllBooksError); } function handleGetAllBooks(books){ dwr.util.removeAllRows("booksBody",{ filter:function(tr) ...{ return (tr.id != "pattern"); }}); var book,id; for(var i = 0; i < books.length; i++){ book = books[i]; id = book.id; dwr.util.cloneNode("pattern", { idSuffix:id }); dwr.util.setValue("t_name" + id, book.name); dwr.util.setValue("t_isbm" + id, book.isbm); dwr.util.setValue("t_author" + id,book.author); $("pattern" + id).style.display = "block"; bookCache[id] = book; } } function handleGetAllBooksError(msg){ alert("Error: " + msg); } function addBook(){ var book = ...{name:null,isbm:null,author:null}; dwr.util.getValues(book); dwr.engine.beginBatch(); BookManageService.addBook(book); loadAllBooks(); dwr.engine.endBatch(); } function editBook(btId){ currentBook = bookCache[btId.substring(4)]; dwr.util.setValues(currentBook); } function updateBook(){ var book = {id:null,name:null,isbm:null,author:null}; dwr.util.getValues(book); book.id = currentBook.id; BookManageService.updateBook(book,handleUpdateBook,handleUpdateBookError); } function handleUpdateBook(){ alert("Update book successfully!"); loadAllBooks(); } function handleUpdateBookError(msg){ alert("Error: " + msg); } function deleteBook(btId){ var i = confirm("Are you sure to delete the book?"); if(i == true) BookManageService.deleteBook(btId.substring(6),handleDeleteBook,handleDeleteBookError); } function handleDeleteBook(){ alert("The book has been delete successfully!"); loadAllBooks(); } function handleDeleteBookError(msg){ alert("Error: " + msg); } </script> </head> <body onload="loadAllBooks()"> <div> <h2>Add book</h2> <table> <tr> <td>Name:</td><td><input type="text" id="name"></td> </tr> <tr> <td>ISBN:</td> <td><input type="text" id="isbm"></td> </tr> <tr> <td>Author:</td> <td><input type="text" id="author"></td> </tr> <tr> <td colspan="2"> <input type="button" id="add" value="Add" onclick="addBook()"> <input type="button" id="update" value="Update" onclick="updateBook()"> </td> </tr> </table> </div> <hr> <div id="list"> <table border="1"> <thead> <tr> <th>Name</th> <th>ISBN</th> <th>Author</th> <th>Action</th> </tr> </thead> <tbody id="booksBody"> <tr id="pattern" style="display:none;"> <td> <span id="t_name"></span> </td> <td> <span id="t_isbm"></span> </td> <td> <span id="t_author"></span> </td> <td> <span id="action"> <input id="edit" type="button" value="Edit" onclick="editBook(this.id)"/> <input id="delete" type="button" value="Delete" onclick="deleteBook(this.id)"/> </span> </td> </tr> </tbody> </table> </div> </body>
相关推荐
struts1+spring+hibernate+DWR整合方案详解
在"struts2+hibernate+spring+dwr整合"的项目中,这四个框架的协同工作通常如下: 1. Struts2 负责处理HTTP请求,解析用户的输入,根据配置决定执行哪个Action,Action完成后通过Result返回响应。 2. Spring 作为...
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
综合以上,本项目通过Struts2、Spring、Hibernate和DWR的整合,构建了一个具备用户注册、多级下拉联动和表格刷新功能的Web应用。这种整合不仅提高了开发效率,也提升了用户体验,展示了Java Web开发的强大能力。在...
"Struts2 + Spring + Hibernate + DWR"是一个常见的Java Web开发框架组合,用于构建动态、数据驱动的Web应用程序。这个项目部署笔记将深入探讨这四个组件的核心功能以及它们如何协同工作。 首先,Struts2是一个基于...
该示例使用MyEclipse 5.5 IDE用来整合四个技术(Struts 1.2, Spring 2.0, Hibernate 3.2和Ajax技术)的轻量级的JavaEE。 操作步骤如下: 1. 下载并解压之后,使用MyEclipse导入工程 2. 找到demo目录中的.sql数据库...
总结,Spring+Dwr+Hibernate的整合项目提供了一种高效的方式,用于开发具备实时交互特性的Web应用。通过Spring的管理能力、Hibernate的ORM功能以及DWR的双向通信支持,开发者可以构建出高性能、易于维护的企业级应用...
《DWR+Spring+Hibernate整合应用详解》 在IT领域,DWR(Direct Web Remoting)、Spring和Hibernate是三个至关重要的技术组件,它们分别在Web应用程序的远程调用、依赖注入和对象关系映射方面发挥着核心作用。将这三...
在SSH整合中,Spring作为整体框架的粘合剂,管理Struts和Hibernate的生命周期,同时也负责事务管理。在注册应用中,Spring可以用来管理用户注册的服务层对象,实现业务逻辑的解耦。 **Hibernate框架**:Hibernate是...
用户注册和登录是每个系统的必须存在的一部分,基于Dwr+Struts2+Spring+Hibernate写了一个用户登录注册系统。 其中用Dwr去进行用户注册的用户是否存在的验证。 全部业务控制交由Spring去进行处理。事务处理也...
该源码成功整合了Struts2.0+Hibernate+Spring(SSH架构)+DWR+MySql+Tomcat,直接拷贝部署就可以运行,前提建立相应的数据库。模块包括(组织机构管理,人员管理,角色管理,权限管理等等),每个模块均有增删查改等...
"Struts+Spring+Hibernate人力资源管理系统"是一个理想的学习实例,它整合了三个强大的Java技术框架,旨在提供全面、稳定且易于维护的人事管理解决方案。 Struts、Spring和Hibernate,这三者构成了企业级Java应用...
**整合DWR、EXTJS、Spring和Hibernate** 将这四者结合,可以创建出高性能的Web应用。DWR处理客户端与服务器的通信,EXTJS负责用户界面,Spring作为应用的架构基础,提供服务层和数据访问层的支持,而Hibernate则用于...
2. 集成Hibernate:设置Hibernate配置文件,定义数据源、实体类和映射文件,确保与Spring的整合。 3. 配置Struts:创建Struts配置文件,定义Action类及其对应的URL路径,以及结果视图的跳转。 4. 实现Ajax:在客户端...
标题 "ext2+spring+hibernate+dwr.rar_dwr MenuManage.js_ext2 ja" 提示我们这是一个关于集成技术的项目,其中包含了ext2、Spring、Hibernate和DWR(Direct Web Remoting)四个主要组件。这个项目的具体实现是通过...
这是一个基于Java技术栈,采用Spring、Struts、Hibernate、jbpm和DWR等多种框架整合开发的OA(Office Automation)办公系统源码项目。这个系统的构建旨在实现企业的日常办公自动化,提高工作效率,简化工作流程,...
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
Struts、Spring、Hibernate 和 Direct Web Remoting (DWR) 是Java开发中常见的四大框架,它们各自在Web应用的不同层面发挥作用。在这个“struts+spring+hibernate+dwr+分页例子”中,我们将深入探讨这四个框架的集成...
在本项目中,Spring可能被用来管理Struts2中的Action实例,以及与Hibernate的整合,提供更高级的服务层。 3. Hibernate:Hibernate是一个流行的Java对象关系映射(ORM)框架,它可以将数据库操作转换为对Java对象的...
《Spring+Struts+Hibernate+DWR集成教程》是由知名技术博主saivicky精心制作的一份集成开发教程,旨在帮助开发者全面理解并掌握这四大框架的整合应用。本教程涵盖了Web开发中的MVC架构、持久层管理、远程调用等多个...