上篇介绍了基本的配置,这篇着重介绍与hibernate4整合。
1.web.xml文件中加入spring-hibernate的配置。新的web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springMVC 配置 --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截以html为后缀的请求 --> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout>10</session-timeout> </session-config> <welcome-file-list> <welcome-file>/views/index.jsp</welcome-file> </welcome-file-list> </web-app>
2.编写spring-hibernate.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 采用c3p0连接池,引入 c3p0-0.9.1.jar包 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${database.driver}" /> <property name="jdbcUrl" value="${database.url}" /> <property name="user" value="${database.user}" /> <property name="password" value="${database.password}" /> <property name="maxPoolSize" value="60" /> <property name="minPoolSize" value="1" /> <property name="initialPoolSize" value="10" /> <property name="acquireRetryAttempts" value="30" /> <property name="acquireRetryDelay" value="100" /> <property name="breakAfterAcquireFailure" value="false" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.rainbow.model</value><!-- 扫描实体类,也就是平时所说的model --> </list> </property> </bean> <!-- 开启AOP监听 只对当前配置文件有效 --> <!-- <aop:aspectj-autoproxy expose-proxy="true" /> --> <!-- 开启注解事务 只对当前配置文件有效 --> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 加入proxy-target-class="true" 表示基于cglib来实现代理类,这样需要将事务@Transactional的注解放在具体类或者具体实现上才能生效 --> <!-- 开启配置式定义事务 --> <!-- <aop:config proxy-target-class="true"> <aop:pointcut id="txPointcut" expression="execution(* com.rainbow.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="is*" read-only="true" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> --> </beans>
目前采用注解的方式进行配置的管理,另外,数据库连接池管理我们采用c3p0,测试数据库选用的mysql,这样我们需要引入新的jar包,
目前hibernate4 需要的包有
另外,我们如果需要调试日志的话,log4j.jar,slf4j-api-1.5.8.jar,slf4j-log4j12-1.5.8.jar, c3p0需要c3p0-0.9.1.jar,mysql需要mysql-connector-java-5.1.15.jar。这些包,另外autowired注册bean的时候需要cglib-nodep-2.1_3.jar
(既然介绍到这里,假如我们需要配置aop切片的话,需要aspectjrt-1.6.8.jar和aspectjweaver-1.6.8.jar)
(文件上传通常会用到commons-fileupload-1.2.2.jar)
3.编写相应的java文件。
具体方法请查看代码, 几点说明注释,数据持久层需要@Repository,这边有个问题需要注意一下:
1、以前集成hibernate3和spring的时候,spring的ORM包里提供了HibernateSupport和HibernateTemplate这两个辅助类,我用的是HibernateTemplate。不过在Hibernate4里,spring不再提供这种辅助类,用的是hibernate4的原生API
2、集成hibernate4之后,最小事务级别必须是Required,如果是以下的级别,或者没有开启事务的话,无法得到当前的Session
执行这行代码,会抛出No Session found for current thread
对于运行时,这个可能不是很大的问题,因为在Service层一般都会开启事务,只要保证级别高于Required就可以了。可是由于在Dao层是不会开启事务的,所以针对Dao层进行单元测试就有困难了。
一般情况是:在IService接口上加上:@Transactional(propagation = Propagation.REQUIRED, readOnly = false) , 这样就能正常使用事务,这里本人觉得是hibernate4设计不好的地方。
最好运行测试即可
一些后期的说明打算放在这里