Spring3.x_Struts2.x_Hibernate4.x整合
在Spring与hibernate4的整合已经取消了HibernateTemplate和HibernateDaoSupport2个类的支持。
Hibernate4.2.15下载地址http://sourceforge.net/projects/hibernate/files/hibernate4/4.2.15.Final/
http://sourceforge.net/projects/hibernate/files/hibernate4/4.2.15.Final/hibernate-release-4.2.15.Final.zip/download
1.导入相应的jar包
2.修改Spring的配置文件
<!-- 使用hibernate4没有提供专门的annotation.AnnotationSessionFactoryBean针对Annotation的类,直接在LocalSessionFactoryBean中已经集成 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 设置Spring去那个包中查找相应的实体类 --> <property name="packagesToScan"> <value>org.oms.spring.model</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> </bean>
<!-- 事物管理也需要修改为hibernate4的支持--> <!-- 配置Spring的事物处理 --> <!-- 创建事物管理器 --> <!-- SessionFactory, DataSource, etc. omitted --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
<!-- Spring中提供了org.springframework.orm.hibernate3.support.OpenSessionInViewFilter; 这个类来实现OpenSessionInViewFilter的操作--> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 创建Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Context Configuration locations for Spring XML files --> <!-- Spring的监听器可以通过这个上下文参数来获取beans.xml的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml</param-value> </context-param> <!-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>SystemContextFilter</filter-name> <filter-class>org.oms.spring.filter.SystemContextFilter</filter-class> <init-param> <description>分页参数</description> <param-name>pageSize</param-name> <param-value>10</param-value> </init-param> </filter> <filter-mapping> <filter-name>SystemContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring中提供了org.springframework.orm.hibernate3.support.OpenSessionInViewFilter; 这个类来实现OpenSessionInViewFilter的操作--> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 如果把这个filter放在struts2的filter之后,所有的请求都会被struts2给控制,所以这个filter就不起作用了, 必须将这个filter放在struts2的filter之前--> <!-- <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.oms.spring.filter.OpenSessionFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 打开Spring的Annotation的支持 --> <context:annotation-config /> <!-- 设定Spring去哪些包中找Annotation --> <context:component-scan base-package="org.oms.spring" /> <!-- 创建数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置连接池的初始值 --> <property name="maxActive" value="500" /> <!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用布到的连接释放,一直减少到maxIdle为止 --> <property name="maxIdle" value="20" /> <!-- 当最小空闲时,当连接少于minIdle时会自动申请一些连接 --> <property name="minIdle" value="1" /> <property name="maxWait" value="1000" /> </bean> <!-- 导入src下的jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 创建sessionFactory工厂 --> <!-- 如果使用的是Annotation方式不能使用org.springframework.orm.hibernate3.LocalSessionFactoryBean,而应该使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean --> <!-- 使用hibernate4没有提供专门的annotation.AnnotationSessionFactoryBean针对Annotation的类,直接在LocalSessionFactoryBean中已经集成 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 设置Spring去那个包中查找相应的实体类 --> <property name="packagesToScan"> <value>org.oms.spring.model</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> </bean> <!-- 配置Spring的事物处理 --> <!-- 创建事物管理器 --> <!-- SessionFactory, DataSource, etc. omitted --> <!-- 事物管理也需要修改为hibernate4的支持--> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置AOP,Spring是通过AOP来进行事物管理的 --> <aop:config> <!-- 设置pointcut表示哪些方法要加入事物处理 --> <!-- 以下的事务是声明在Dao中,但是通常都会在Service中来处理多个业务逻辑的关系, 如:删除,更新等,此时如果在执行了一个步骤之后抛出抛出异常就会导致数据部完整,所以事务不应该在Dao中处理,而应该在Service中处理, 这也是就是Spring提供的一个非常方便的工具,声明式的事务 --> <aop:pointcut id="allMethods" expression="execution(* org.oms.spring.service.*.*(..))" /> <!-- 通过advisor来确定具体要加入事物控制的方法 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" /> </aop:config> <!-- 配置哪些方法要加入事物控制 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 让所有方法都加入事物管理(效率低,实际使用中按需处理),为了提供效率,可以把一些查询之类的方法设置为只读事务 --> <tx:method name="*" propagation="REQUIRED" read-only="true"/> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="save" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
package org.oms.spring.dao; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.oms.spring.model.pager.Pager; import org.oms.spring.model.pager.SystemContext; /** * 可以考虑考虑把所有公共的方法都写在BaseDao中,这个时候,让所有的DAO都继承BaseDao; * 这样基本上就实现了大量的基础方法,如果DAO中又一些特殊方法,再再具体的实现类的DAO中创建。 * 在Spring与hibernate4的整合已经取消了HibernateTemplate和HibernateDaoSupport2个类的支持 * @author sunlight * */ public class BaseDao<T> implements IBaseDao<T>{ private SessionFactory sessionFactory; /** * 此处不能使用setSessionFactory注入,因为setSessionFactory在HibernateDaoSupport中已经定义了,而且还是final的, * 所以不能被覆盖 * @param sessionFactory */ @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory=sessionFactory; } protected Session getSession(){ return sessionFactory.getCurrentSession(); } /** * 创建一个Class的对象来获取泛型的class */ private Class<T> clz; @SuppressWarnings("unchecked") public Class<T> getClz(){ if (clz==null) { clz=(Class<T>)(((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]); } return clz; } @Override public void add(T t) { this.getSession().save(t); } @Override public void delete(int id) { this.getSession().delete(this.load(id)); } @Override public void update(T t) { this.getSession().update(t); } @SuppressWarnings("unchecked") @Override public T load(int id) { //如果使用了自定义的OpenSessionFilter,需要按照如下方法来获取 // return (T)OpenSessionFilter.getSession().load(getClz(), id); /** * 如果使用了Spring提供的OpenSessionInViewFilter就按照整合的方法处理即可 */ return (T)this.getSession().load(getClz(), id); } @SuppressWarnings("unchecked") @Override public List<T> list(String hql, Object[] args) { Query query=this.getSession().createQuery(hql); if (args!=null) { for (int i = 0; i < args.length; i++) { query.setParameter(i, args[i]); } } List<T> list=query.list(); return list; } @Override public List<T> list(String hql) { return this.list(hql,null); } @Override public List<T> list(String hql, Object args) { return this.list(hql, new Object[]{args}); } @SuppressWarnings("unchecked") @Override public Pager<T> find(String hql, Object[] args) { Pager<T> pages=new Pager<T>(); int pageOffset=SystemContext.getPageOffset(); int pageSize=SystemContext.getPageSize(); Query query=this.getSession().createQuery(hql); Query cq=this.getSession().createQuery(getCountHql(hql)); if(args!=null){ int index=0; for (Object arg : args) { query.setParameter(index, arg); cq.setParameter(index, arg); index++; } } long totalRecord=(Long)cq.uniqueResult(); query.setFirstResult(pageOffset); query.setMaxResults(pageSize); List<T> datas=query.list(); pages.setDatas(datas); pages.setPageOffset(pageOffset); pages.setPageSize(pageSize); pages.setTotalRecord(totalRecord); return pages; } private String getCountHql(String hql){ //1.获取from 前面的字符串 String f=hql.substring(0,hql.indexOf("from")); //2.将from前面的字符串替换为select count(*) if (f.equals("")) { hql="select count(*) "+hql; }else { hql=hql.replace(f, "select count(*) "); } //3.将fetch 替换为"",因为抓取查询不能使用count(*) hql=hql.replace("fetch", ""); return hql; } @Override public Pager<T> find(String hql, Object args) { return this.find(hql, new Object[]{args}); } @Override public Pager<T> find(String hql) { return this.find(hql, null); } }
个人备忘笔记~~