Spring与Hibernate整合,实现Hibernate事务管理
1.所需的jar包
连接池/数据库驱动包
Hibernate相关jar
Spring 核心包(5个)
Spring aop 包(4个)
spring-orm-3.2.5.RELEASE.jar 【spring对hibernate的支持】
spring-tx-3.2.5.RELEASE.jar 【事务相关】
2.配置文件
Product.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.juaner.entity">
<class name="Product">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="price" column="price"/>
<property name="remark" column="remark"/>
</class> </hibernate-mapping>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">juaner767</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--spring默认以线程方式创建,配了反而有问题-->
<!-- session创建方式 -->
<!--<property name="hibernate.current_session_context_class">thread</property>--> <!-- 加载映射 -->
<mapping resource="com/juaner/entity/Product.hbm.xml"/> </session-factory>
</hibernate-configuration>
3.直接加载hibernate文件的方式配置
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--dao-->
<bean id="productDao" class="com.juaner.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--service-->
<bean id="productService" class="com.juaner.service.ProductService">
<property name="productDao" ref="productDao"/>
</bean> <!--spring与hibernate整合-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--通过配置文件创建sessionfactory对象-->
<property name="configLocation" value="hibernate.cfg.xml"></property>
</bean>
<!--事务配置-->
<!--配置事务管理器类-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--aop配置-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.juaner.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
4.spring配置连接池和hibernate常用配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--dao-->
<bean id="productDao" class="com.juaner.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--service-->
<bean id="productService" class="com.juaner.service.ProductService">
<property name="productDao" ref="productDao"/>
</bean> <!--数据源配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"/>
<property name="user" value="root"/>
<property name="password" value="juaner767"/>
<property name="initialPoolSize" value="3"/>
<property name="maxPoolSize" value="6"/>
<property name="maxStatements" value="100"/>
<property name="acquireIncrement" value="2"/>
</bean>
<!--spring与hibernate整合-->
<!--方式2,使用spring创建连接池对象-->
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">-->
<!--<!–通过配置文件创建sessionfactory对象–>-->
<!--<property name="configLocation" value="hibernate.cfg.xml"></property>-->
<!--<property name="dataSource" ref="dataSource"/>-->
<!--</bean>-->
<!--方式3,-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--hibernate常用配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--映射文件配置-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/juaner/entity/</value>
</list>
</property>
</bean>
<!--事务配置-->
<!--配置事务管理器类-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--aop配置-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.juaner.service.ProductService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
Spring与Hibernate整合,实现Hibernate事务管理的更多相关文章
- Spring整合hibernate4:事务管理
Spring整合hibernate4:事务管理 Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTran ...
- 四 Hibernate的一级缓存&事务管理及其配置
持久态对象: 自动更新数据库,原理是一级缓存. 缓存:是一种优化的方式,将数据存入内存,从缓存/内存中获取,不用通过存储源 Hibernate框架中提供了优化手段:缓存,抓取策略 Hibernate中 ...
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ...
- Spring第13篇—–Spring整合Hibernate之声明式事务管理
不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...
- Spring整合hibernate4:事务管理[转]
Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作 ...
- Spring整合Hibernate--声明式事务管理
Spring指定datasource 1. 新建jdbc.properties文件: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc: ...
- Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法
1. tx 配置方法, 代码示例 javabean及其映射文件省略,和上篇的一样 CustomerDao.java, dao层接口 public interface CustomerDao { pub ...
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...
- Spring详解(七)------事务管理
PS:本篇博客源码下载链接:http://pan.baidu.com/s/1mi3NhX2 密码:3io2 1.事务介绍 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指 ...
- Spring详解(八)------事务管理
PS:本篇博客源码下载链接:http://pan.baidu.com/s/1mi3NhX2 密码:3io2 1.事务介绍 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指 ...
随机推荐
- AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)
目录[-] 一.service引导 二.service 1.factory() 2.service() 3.provider() 一.service引导 刚开始学习Angular的时候,经常 ...
- jquery 获取鼠标位置
//获取鼠标位置 $(function(){ $('body').mousemove(function(e) { e = e || window.event; __xx = e.pageX || e. ...
- D3.js 力导向图的制作
力导向图中每一个节点都受到力的作用而运动,这种是一种非常绚丽的图表. 力导向图(Force-Directed Graph),是绘图的一种算法.在二维或三维空间里配置节点,节点之间用线连接,称为连线. ...
- linux下在jar包中找类是否存在
find /usr/lib -name "*.jar" -exec grep -Hsli 类名 {} \;
- eclipse下安装插件
最近想自己弄弄Python,手上就有eclipse,也不想在安装别的IDE占空间,就在网上找了一下eclipse支持开发python的插件,果然有. pydev官网地址:http://pydev.or ...
- APMServ本地搭建网站最好用的软件
APMServ 5.2.6 是一款拥有图形界面的快速搭建Apache 2.2.9.PHP 5.2.6.MySQL 5.1.28&4.0.26.Nginx 0.7.19.Memcached 1. ...
- 利用JAXB实现java实体类和xml互相转换
1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...
- 【Problem solved】发现输入法都是仅桌面使用,无法输入中文时
你打开命令提示符输入CTFMON就可以啦.
- ASP.NET MVC 5改进了基于过滤器的身份验证
ASP.NET MVC 5包含在最近发布的Visual Studio 2013开发者预览版中,它使开发人员可以应用身份验证过滤器,它们提供了使用各种第三方供应商或自定义的身份验证提供程序进行用户身份验 ...
- [bootstrap] 栅格系统和布局
1.简介 栅格系统(grid systems),也称为“网格系统”,运用固定的格子设计版面布局,风格工整简洁.是从平面栅格系统演变而来. Bootstrap建立在12列栅格系统.布局.组件之上.以规则 ...