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">-->
<!--&lt;!&ndash;通过配置文件创建sessionfactory对象&ndash;&gt;-->
<!--<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事务管理的更多相关文章

  1. Spring整合hibernate4:事务管理

    Spring整合hibernate4:事务管理 Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTran ...

  2. 四 Hibernate的一级缓存&事务管理及其配置

    持久态对象: 自动更新数据库,原理是一级缓存. 缓存:是一种优化的方式,将数据存入内存,从缓存/内存中获取,不用通过存储源 Hibernate框架中提供了优化手段:缓存,抓取策略 Hibernate中 ...

  3. Spring+Mybatis+MySql+Maven 简单的事务管理案例

    利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ...

  4. Spring第13篇—–Spring整合Hibernate之声明式事务管理

    不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...

  5. Spring整合hibernate4:事务管理[转]

    Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作 ...

  6. Spring整合Hibernate--声明式事务管理

    Spring指定datasource 1. 新建jdbc.properties文件: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc: ...

  7. Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法

    1. tx 配置方法, 代码示例 javabean及其映射文件省略,和上篇的一样 CustomerDao.java, dao层接口 public interface CustomerDao { pub ...

  8. spring事物配置,声明式事务管理和基于@Transactional注解的使用

    http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...

  9. Spring详解(七)------事务管理

    PS:本篇博客源码下载链接:http://pan.baidu.com/s/1mi3NhX2 密码:3io2 1.事务介绍 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指 ...

  10. Spring详解(八)------事务管理

    PS:本篇博客源码下载链接:http://pan.baidu.com/s/1mi3NhX2 密码:3io2 1.事务介绍 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指 ...

随机推荐

  1. golang 定时器

    上网查了下相关资料,基本上都介绍的是github.com\robfig\cron这个包来执行定时任务,试了下确实可以执行.但是此包下没有删 除任务的方法,只有暂停的方法(Stop),若要停止之前的任务 ...

  2. struct和class

    先概述一下: 1.C# 是纯面向对象语言,struct 与 class 都是继承Object,都是对象.struct 是值类型.class 是引用类型. 2.struct是值类型,在Stack上分配地 ...

  3. ORA-12705: Cannot access NLS data files or invalid environment specified

    ASM实例无法启动 [grid@data ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 11 0 ...

  4. linux redis 安装

    linux下redis安装   我用的系统是:redhat [root@infa ~]# wget http://download.redis.io/releases/redis-2.8.12.tar ...

  5. 使用Beanstalkd_console

    作用:可以将 beanstalkd 的队列信息展示在图形化的操作界面一样,这样不仅给我么查看队列信息有很大的帮助,也可以形象地理解队列任务具体内容 使用操作:(前提条件是前面的队列能正常执行,) 首先 ...

  6. python 练习 3

    #!/usr/bin/python # -*- coding: utf-8 -*- def z94(): #斐波那契数列 def filie(x): a,b,t=1,1,0 if x==1 or x= ...

  7. [saiku] 连接 mondrain 数据源出错-空指针错误

    我的个亲娘,这个问题查半天终于查出来了. 一开始以为是配置的mysql的location连接有问题,各种修改啊各种尝试,还是不行. 好死不死报了空指针错误,让人无法下手. 后来发现是这样子的: 生成s ...

  8. HTML5自学笔记[ 3 ]表单验证反馈

    表单控件对象的validity对象可以设置或返回相关的验证信息(在invalid事件处理中获取validity对象): 属性valid:为true所有验证通过,为False至少有一种验证失败. 属性v ...

  9. WMI执行远程文件(RPC)

    通过wmi在远程机上执行命令(wmi:windows management interface 可以通过一个公共的接口访问不同操作系统(windows系统)的构成单元,利用它可以高效的管理远程和本地的 ...

  10. Eclipse Maven Web Application 设置配置文件

    默认的项目添加会有问题,各种版本和编译版本错误造成. 1.更改Maven编译版本 2.更改项目Facets针对的版本 3.更改Settings