springmvc3.1.1+hibernate4

上篇介绍了基本的配置,这篇着重介绍与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 

Java代码  
  1. sessionFactory.getCurrentSession();  


执行这行代码,会抛出No Session found for current thread 

对于运行时,这个可能不是很大的问题,由于在Service层通常都会开启事务,只要保证级别高于Required就能够了。但是因为在Dao层是不会开启事务的,因此针对Dao层进行单元测试就有困难了。

通常状况是:在IService接口上加上:@Transactional(propagation = Propagation.REQUIRED, readOnly = false) , 这样就能正常使用事务,这里本人以为是hibernate4设计很差的地方。

 

最好运行测试便可


 

 

一些后期的说明打算放在这里