搭建过程完全参考 http://blog.csdn.net/chenyi0834/article/details/19631445
(SpringMVC+Hibernate+Spring整合实例(一) )
本篇关于SpringMVC基本都会采用注解的方式,首先配置好数据源以及事务spring-common.xml,放在config.spring包下:
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx/spring-tx.xsd">
然后配置关于SpringMVC的内容,下面配置中都有注释说明,就不再赘述,spring-mvc.xml放在config.spring包下:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
最后是web.xml中
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">
为了保证数据的一致性,在编程的时候往往需要引入事务这个概念。事务有4个特性:原子性、一致性、隔离性、持久性。
事务的种类有两种:编程式事务和声明式事务。编程式事务就是将事务处理放在程序中,而声明式事务则是通过配置文件或者注解进行操作。
在Spring中有声明式事务的概念,通过和Hibernate类似框架的集成,可以很好的完成声明式事务。
其实,不论在Spring中有几种配置Hibernate事务的方法,都逃不出一下几条:
1.配置SessionFactory
2.配置事务容器
3.配置事务规则
4.配置事务入口
后面一共为大家提供4种配置Hibernate事务的方法。
首先说下配置SessionFactory,配置SessionFactory有两种方式,一种是通过配置hibernate.cfg.xml文件的位置来配置SessionFactory,另一种就是在Spring配置文件中,手动配置数据源。
下面是两种配置SessionFactory的方式(第二种配置需要额外引入两个包:commons-dbcp、commons-pool)
第一种方式,利用tx标签配置事务。
首先,在配置文件中写入下面语句,打开注解功能
纵观以上四种在Spring中配置Hibernate事务的方法,其核心都是一样的,不同的只是实现的方式而已。所以看到这,这篇博文中你只需要记住4句话,就可以轻松理解在Spring中配置Hibernate事务的核心:
1.配置SessionFactory
2.配置事务容器
3.配置事务规则
4.配置事务入口