声明式事务-整合Spring、Hibernate
编程式事务:通过编码的方式,让事务处理的代码侵入到核心的业务代码中。
声明式事务:完成了事务处理的代码和业务核心代码的解耦合。提供事务处理代码的复用性和降低维护成本。
声明式事务:aop最典型的应用。
使用动态代理实现事务的管理:
Jdk: 实现动态是通过实现某个特定的接口(代理类和目标类必须是相同的接口),产生一个虚拟的class文件(代理类的)。必须有接口的实现才能使用jdk完成动态代理
Cglib:动态代理,如果没有接口的实现使用cglib完成动态代理,使用了一个asm框架,完成某个类(目标类)的子类(虚拟的class文件)
Hibernate,spring 使用jdk的动态代理(默认情况),也可以使用cglig
1 新建java项目
进行spring+hibernate的集成:
使用IOC进行对象管理和注入
使用AOP进行事务的管理
2加入jar包
Spring的jar包
Hibernate的jar包
Dbcp的数据库连接池包(c3p0)
3 建立配置文件
在src目录下:
Hibernate.cfg.xml
applicationContext.xml
hibernate配置文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/ssh
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- hiberante 其他的属性 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 加载映射文件 -->
<mapping resource="org/guangsoft/pojo/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
4建立Dao接口
package org.guangsoft.dao;
import org.guangsoft.pojo.Student;
/***
* StudentDao接口
* **/
public interface StudentDao
{
public void addStudent(Student stu);
}
5建立Dao接口的实现类
package org.guangsoft.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.guangsoft.dao.StudentDao;
import org.guangsoft.pojo.Student;
/***
* StudentDao接口的实现类,完成数据库的访问
* HibernateDaoSupport:是spring完成对hibernate的支持
* private HibernateTemplate hibernateTemplate;用来完成数据的crud操作
* 给HibernateDaoSupport注入sessoinFactory
* super.getSession();获得hibernate原始的session对象
* ***/
public class StudentDaoImpl extends HibernateDaoSupport
implements StudentDao
{
/***
* 添加学生信息
* ***/
@Override
public void addStudent(Student stu)
{
super.getHibernateTemplate().save(stu);
}
}
6 建立service接口
package org.guangsoft.service;
import org.guangsoft.pojo.Student;
/** 建立用户service接口 ***/
public interface StudentService
{
public void saveUser(Student stu);
}
7 建立service接口的实现类
package org.guangsoft.service.impl;
import org.guangsoft.dao.StudentDao;
import org.guangsoft.pojo.Student;
import org.guangsoft.service.StudentService;
public class StudentServiceImpl implements StudentService
{
// 声明dao对象
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao)
{
this.studentDao = studentDao;
}
@Override
public void saveUser(Student stu)
{
studentDao.addStudent(stu);
}
}
8进行spring的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 实例化一个SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 加载hibernate的配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 实例Dao对象 -->
<bean id="studentDao" class="org.guangsoft.dao.impl.StudentDaoImpl">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 实例化service对象 -->
<bean id="studentService" class="org.guangsoft.service.impl.StudentServiceImpl">
<!-- 注入dao对象 -->
<property name="studentDao" ref="studentDao"></property>
</bean>
<!-- 声明式事务:进行事务的配置 -->
<!-- 1 实例化事务管理器对象 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2 进行事务特征的声明: 切面 id:用来完成切面在其他地方引用 transaction-manager:引用事务管理器对象 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- name:声明的是将哪些命名规范的方法,纳入事务管理 propagation:事务传播机制。 PROPAGATION_REQUIRED:
如果存在一个事务,则支持当前事务。如果没有事务则开启 isolation:事务的隔离级别,是数据库平台默认的隔离级别 read-only:被刺事务操作的数据是只读的
rollback-for=:异常的完全限定名,如果发生这样的异常进行回滚 -->
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="modf*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 3进行AOP的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
id="pc" />
<!-- 进行织入 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
</beans>
9添加事务的测试
package org.guangsoft.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.guangsoft.pojo.Student;
import org.guangsoft.service.StudentService;
public class TransactionTest
{
@Test
public void testTransaction()
{
// 加载spring的配置文件,获得bean容器
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获得bean对象
StudentService studentService = (StudentService) ac
.getBean("studentService");
Student stu = new Student();
stu.setSname("小强");
studentService.saveUser(stu);
}
}
1 实例化事务管理器对象
2 声明事务切面(事务的特征)
3 进行aop的配置
声明式事务-整合Spring、Hibernate的更多相关文章
- 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。
使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务.
- 【声明式事务】Spring声明式事务实现(三)
以MyBatis为例. 一.基于注解的声明式事务配置 1. 添加tx名字空间 xmlns:tx="http://www.springframework.org/schema/tx" ...
- 【声明式事务】Spring事务介绍(一)
事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务有两种管理方式:编程式事务和声明式事务 编程式事务使用TransactionTemplate或者直接 ...
- 【声明式事务】Spring事务特性(二)
spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. 其中TransactionDefin ...
- 声明式事务xml Spring
!--JDBC事务管理器--><bean id="tansactionManager" class="org.springframework.jdbc.dat ...
- 【spring 7】spring和Hibernate的整合:声明式事务
一.声明式事务简介 Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- spring事务管理——编程式事务、声明式事务
本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...
随机推荐
- iSCSI配置流程
Windows群集两个节点:分别在SQL01和SQL02设置连接共享磁盘: 此前已经在存储服务器通过StarWind创建了三个虚拟磁盘:Quemon+data+backup:starwind安装请参考 ...
- dfs序 + RMQ = LCA
dfs序是指你用dfs遍历一棵树时,每个节点会按照遍历到的先后顺序得到一个序号.然后你用这些序号,可以把整个遍历过程表示出来. 如上图所示,则整个遍历过程为1 2 3 2 4 5 4 6 4 2 1 ...
- join 和 union 区别
JOIN和UNION区别 join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 .JOIN用于按照ON条件联接两个表 ...
- LR测试登陆后进行的操作时 绕过登录
oadrunner web_add_cookie web_add_cookie 这个的函数原来真的能过逃过登录,哈哈,这个苦苦纠结我的问题呀. 函数原型:int web_add_cookie( con ...
- [BZOJ4016][FJOI2014]最短路径树问题
[BZOJ4016][FJOI2014]最短路径树问题 试题描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长 ...
- STM32F103ZET6 用定时器级联方式输出特定数目的PWM(转载)
STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5.TIM8,共6个.这里需要使用定时器的级联功能,ST的RM0008 REV12的P388和P399页上有说明对于特定的定 ...
- NET-SNMP开发——日志输出
NET-SNMP开发——日志输出 net-snmp的日志输出功能是很强大的,与日志输出相关函数声明在net-snmp-5.7.3\include\net-snmp\library\snmp_loggi ...
- fzu2172 字符串dp
F - 巡了南山我巡北山 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- C# 基于json通讯中的中文的处理
如果通讯中产生了\\u4e00-\\u9fa5范围的中文的unicode代码,而不是\u4e00-\u9fa5范围的,那么c#的处理就比较麻烦了. 破解方法: 机制 它会把\\u4e00拆成部分来识别 ...
- 多节点 devstack 部署
1, 网络配置 每个节点 /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.42.11 netmask ...