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

  1. sessionFactory.getCurrentSession();

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

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

一般情况是:在IService接口上加上:@Transactional(propagation = Propagation.REQUIRED, readOnly = false) , 这样就能正常使用事务,这里本人觉得是hibernate4设计不好的地方。

最好运行测试即可


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

springmvc3.1.1+hibernate4的更多相关文章

  1. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  2. spring4+hibernate4+maven环境搭建

    本文主要介绍利用maven搭建spring4+hibernate4开发环境. 首先我们创建一个maven项目,具体步骤就不详细介绍了,看看我们pom.xml文件 <project xmlns=& ...

  3. Spring4 MVC Hibernate4集成 Annotation

    Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...

  4. Spring4 MVC Hibernate4集成

      Spring4 MVC Hibernate4集成 一.    本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二.    工程目录 三. ...

  5. Hibernate4集成 Annotation使用教程

    Spring4 MVC Hibernate4集成 Annotation 一.    本文所用环境 Spring4.0.3.RELEASE.Hibernate4.3.5.Final.Mysql 二.   ...

  6. SpringMVC+Spring3+Hibernate4开发环境的搭建

    在项目早期比较简单,大多用JSP .Servlet + JDBC 直接获取,以后使用 Struts1(Struts2)+Spring+Hibernate, 严格格按照分层概念驱动项目开发.利用这段时间 ...

  7. Spring4 SpringMVC Hibernate4 Freemaker 集成示例

    变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...

  8. schemamvcSpringMVC+Spring3+Hibernate4开发环境搭建

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下schemamvc <?xml version="1.0" encoding=" ...

  9. springMVC+Hibernate4+Spring整合一(配置文件部分)

    本实例采用springMvc hibernate 与 spring 进行整合, 用springmvc 取代了原先ssh(struts,spring,hibernate)中的struts来扮演view层 ...

随机推荐

  1. ZOJ1610_Count the Colors(段树/为段更新)

    解决报告 意甲冠军: 一定长度8000段染.寻求染色完成后,.. 思路: 区间问题用线段树.成段的更新区间.最后把全部的区间下压到叶子结点,统计叶子结点的颜色. #include <iostre ...

  2. js 通信

    js 页面间的通信 看了一下公司原来的代码,原页面ajax post返回一个页面完整的HTML,然后再打开一个新页面并输出ajax返回的所有代码到新页面上,在新页面上以表单提交的形式实现重定向. 任凭 ...

  3. Nyoj 引水工程(最小生成树)

    描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工程,事关中华民族长远发展.“南水北调工程”,旨在缓解中国华北和西北地区水资源短缺的国家 ...

  4. POJ 2502 Subway (Dijkstra 最短+建设规划)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 Descriptio ...

  5. JAVA设计模式(09):结构化-代理模式(Proxy)

    一,定义:  代理模式(Proxy):为其它对象提供一种代理以控制对这个对象的訪问. 二.其类图: 三,分类一:静态代理 1,介绍:也就是须要我们为目标对象编写一个代理对象,在编译期就生成了这个代理对 ...

  6. validateRequest="false"属性及xss攻击

    validateRequest="false"   指是否要IIS验证页面提交的非法字符,比如:>,<号等,当我们需要将一定格式得html代码获得,插入数据库时候,就要 ...

  7. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  8. spring data jpa使用懒操作

    如果model对象的某属性使用lazy load,调用这个属性时会报错, failed to lazily initialize a collection of role could not init ...

  9. 用fcntl()设置堵塞函数的堵塞性质

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types. ...

  10. 【转】Appium 服务器端从启动到case完成的活动分析

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39474151 此文的目的主要是通过分析Appium Server打印出来的log,加深对A ...