上篇介紹了基本的配置,這篇着重介紹與hibernate4整合。html
1.web.xml文件中加入spring-hibernate的配置。新的web.xml文件內容以下:java
<?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文件,內容以下:node
<?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包,mysql
目前hibernate4 須要的包有web
另外,咱們若是須要調試日誌的話,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.jarspring
(既然介紹到這裏,假如咱們須要配置aop切片的話,須要aspectjrt-1.6.8.jar和aspectjweaver-1.6.8.jar)sql
(文件上傳一般會用到commons-fileupload-1.2.2.jar)數據庫
3.編寫相應的java文件。express
具體方法請查看代碼, 幾點說明註釋,數據持久層須要@Repository,這邊有個問題須要注意一下:api
一、之前集成hibernate3和spring的時候,spring的ORM包裏提供了HibernateSupport和HibernateTemplate這兩個輔助類,我用的是HibernateTemplate。不過在Hibernate4裏,spring再也不提供這種輔助類,用的是hibernate4的原生API
二、集成hibernate4以後,最小事務級別必須是Required,若是是如下的級別,或者沒有開啓事務的話,沒法獲得當前的Session
執行這行代碼,會拋出No Session found for current thread
對於運行時,這個可能不是很大的問題,由於在Service層通常都會開啓事務,只要保證級別高於Required就能夠了。但是因爲在Dao層是不會開啓事務的,因此針對Dao層進行單元測試就有困難了。
通常狀況是:在IService接口上加上:@Transactional(propagation = Propagation.REQUIRED, readOnly = false) , 這樣就能正常使用事務,這裏本人以爲是hibernate4設計很差的地方。
最好運行測試便可
一些後期的說明打算放在這裏