spring 声明式事务管理xml方式实现

事务控制概述:

编程式事务控制

         自己手动控制事务,就叫做编程式事务控制。

         Jdbc代码:

                   Conn.setAutoCommite(false);  // 设置手动控制事务

         Hibernate代码:

                   Session.beginTransaction();    // 开启一个事务

         【细粒度的事务控制:可以对指定的方法、指定的方法的某几行添加事务控制】

         (比较灵活,但开发起来比较繁琐:每次都要开启、提交、回滚.)

 

声明式事务控制

         Spring提供了对事务的管理, 这个就叫声明式事务管理。

         Spring提供了对事务控制的实现。用户如果想用Spring的声明式事务管理,只需要在配置文件中配置即可;不想使用时直接移除配置。这个实现了对事务控制的最大程度的解耦。

         Spring声明式事务管理,核心实现就是基于Aop

         【粗粒度的事务控制:只能给整个方法应用事务,不可以对方法的某几行应用事务。】

         (因为aop拦截的是方法。)

 

         Spring声明式事务管理器类:

                   Jdbc技术:DataSourceTransactionManager

                   Hibernate技术:HibernateTransactionManager


xml方式实现事务管理:-->底层使用了aop(面向切面编程的思想)

步骤:

         1) 引入spring-aop相关的4个jar文件(当然还包含核心包)

         2) 引入aop名称空间  【XML配置方式需要引入】

         3) 引入tx名称空间    【事务方式必须引入】

案例:

1)实体类Dept.java

<span style="font-family:Courier New;font-size:14px;">public class Dept {
	private int deptId;
	private String deptName; //set get方法
}</span>
2)j接口类DeptDao 因为要使用spring对jdbc的管理类JdbcTemple类 要依赖注入

<span style="font-family:Courier New;font-size:14px;">package cn.itcast.xml;

import org.springframework.jdbc.core.JdbcTemplate;

public class DeptDao {
	//IOC容器注入
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public void save(Dept dept){
		String sql="insert into t_dept(deptName) values(?)";
		jdbcTemplate.update(sql,dept.getDeptName());
	}
}</span>
3)DeptService -->要依赖注入deptDao

<span style="font-family:Courier New;font-size:14px;">package cn.itcast.xml;

public class DeptService {
	//注入
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	public void save(Dept dept){
		deptDao.save(dept);
	}
}
</span>
4)配置文件bean.xml

xml声明式事务的配置主要包括三个方面:

1)配置事务管理器-->要引用数据源dataSource

2)事务增强

3)aop的配置-->拦截哪些方法(切入点表达式)+应用上面的事务增强

<span style="font-family:Courier New;font-size:14px;"><?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
     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
        ">
       <!-- 数据库连接池的配置 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
       		<property name="jdbcUrl" value="jdbc:mysql:///day15"></property>
       		<property name="user" value="root"></property>
       		<property name="password" value="169500"></property>
       		<property name="initialPoolSize" value="3"></property>
       		<property name="maxPoolSize" value="5"></property>
       		<property name="maxStatements" value="100"></property>
       		<property name="acquireIncrement" value="2"></property>
       </bean>
       <!-- jdbcTemple的配置 -->
       <bean id="jdbcTemple" class="org.springframework.jdbc.core.JdbcTemplate">
       		<property name="dataSource" ref="dataSource"></property>
       </bean>
       <bean id="deptDao" class="cn.itcast.xml.DeptDao">
       	<property name="jdbcTemplate" ref="jdbcTemple"></property>
       </bean>
       <bean id="deptService" class="cn.itcast.xml.DeptService">
       	<property name="deptDao" ref="deptDao"></property>
       </bean>
       <!-- -***************************spring声明式事务管理xml方式************************************************* -->
       <!-- 1.配置事务管理器 -->
       <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       	<property name="dataSource" ref="dataSource"></property>
       </bean>
       <!-- 2.配置事务增强 -->
       <tx:advice id="txAdvice" transaction-manager="txManager" >
       	<tx:attributes>
       		<tx:method name="*" read-only="false"/>
       	</tx:attributes>
       </tx:advice>
       <!-- 3.aop配置 :拦截那些方法(切入点表达式)+应用上面的事务增强 -->
       <aop:config>
       	<aop:pointcut expression="execution(* cn.itcast.xml.DeptService.save(..) )" id="pa"/>
       	<aop:advisor advice-ref="txAdvice" pointcut-ref="pa"/>
       </aop:config>
</beans></span>
4)测试类App

public class App {
	@Test
	public void test(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/xml/bean.xml");
		DeptService deptService=(DeptService) ac.getBean("deptService");
		Dept dept=new Dept();
		dept.setDeptName("xmlTX");
		deptService.save(dept);
	}
}
运行结果:数据库表中会添加一条新的记录







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值