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. Mysql 组合查询 UNION 与 UNION ALL

  2. spring源码深度解析-2功能扩展

    容器功能的扩展ApplicationContext用于扩展BeanFactory中现有的功能.究竟多出了哪些功能,进一步探索.写法上:BeanFactory bf = new XmlBeanFacto ...

  3. 堆排序(C语言)

    #ifndef HEAP_SORT_H #define HEAP_SROT_H #include<iostream> void maxHeap(int *arr,unsigned int ...

  4. aspcms标签使用经验

    1.调用导航栏标签写法,sort是网站后台栏目管理的编号{aspcms:type sort=19} <a href="[type:link]" title="[ty ...

  5. IoC 之 2.2 IoC 容器基本原理(贰)

    2.2.1  IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IoC ...

  6. 如何开发 Sublime Text 2 的插件

    Sublime Text 2是一个高度可定制的文本编辑器,一直以来对希望有一个快速强大现代的编辑工具的的程序员保持着持续的吸引力.现在,我们将创建自己的一个Sublime plugin,实现用Nett ...

  7. JavaScript开发者常忽略或误用的七个基础知识点

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  8. 文件I/O

    Linux顶层目录结构: /              根目录├── bin     存放用户二进制文件├── boot    存放内核引导配置文件├── dev     存放设备文件├── etc  ...

  9. 20145236 冯佳 《Java程序设计》第1周学习总结

    20145236 冯佳 <Java程序设计>第1周学习总结 教材学习内容总结 因为假期在家的时候并没有提前自学Java,所以,这周算是真正开始第一次接触Java.我对Java的了解也仅仅停 ...

  10. 问题解决The connection to adb is down, and a severe error has occured.

    遇到问题描述: 运行android程序控制台输出 [2013-06-25 11:10:32 - MyWellnessTracker] The connection to adb is down, an ...