spring 声明式事务管理注解方式实现

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 05:18   1067   0

使用注解实现Spring的声明式事务管理,更加简单!

步骤:

1) 必须引入Aop相关的jar文件

2) bean.xml中指定注解方式实现声明式事务管理以及应用的事务管理器类

3)在需要添加事务控制的地方,写上: @Transactional

@Transactional注解:

1)应用事务的注解

2)定义到方法上: 当前方法应用spring的声明式事务

3)定义到类上: 当前类的所有的方法都应用Spring声明式事务管理;

4)定义到父类上: 当执行父类的方法时候应用事务。

案例:

1.Dept实体类

<span style="font-family:Courier New;font-size:14px;">public class Dept {
 private int deptId;
 private String deptName;
 public int getDeptId() {
  return deptId;
 }
 //get set
 
}</span>
2.DeptDao 通过注解的方式注入-->因此要想找到JdbcTemple类需要在bean.xml配置文件中进行配置

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

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class DeptDao {
 //IOC容器注入
 @Resource
 private JdbcTemplate jdbcTemplate;
 public void save(Dept dept){
  String sql="insert into t_dept(deptName) values(?)";
  jdbcTemplate.update(sql,dept.getDeptName());
 }
}</span>
3.DeptService --->事务是在service层的.要想使用事务只需要在方法上添加@Transactional即可(添加到类上也可以 这样的话就对此类中的所有方法进行事务配置)
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.anno;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class DeptService {
 //注入
 @Resource
 private DeptDao deptDao;
 @Transactional
 public void save(Dept dept){//如果这里加了事务 出错了 会自动回滚  都会保存失败
  //如果没有添加事务  出错了 第一个会保存成功
  deptDao.save(dept);
  
 }
}</span>
4.bean.xml---->要想使用注解事务 只需要在配置文件中指定注解实现事务

<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>
       <bean id="jdbcTemple" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
       </bean>
      <!-- 开启注解扫描 -->
      <context:component-scan base-package="cn.itcast.anno"></context:component-scan>
       <!-- -***************************spring声明式事务管理xml方式************************************************* -->
       <!-- 1.配置事务管理器 -->
       <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
       </bean>
       <!-- 注解方式实现事务: 指定注解方式实现事务-->
       <tx:annotation-driven transaction-manager="txManager"/>
       
</beans></span>

5.测试类App

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

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
 @Test
 public void test(){
  ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/anno/bean.xml");
  DeptService deptService=(DeptService) ac.getBean("deptService");
  Dept dept=new Dept();
  dept.setDeptName("annoTX");
  deptService.save(dept);
 }
}
</span>
运行结果测试:


事务总结:

如果使用spring提供的事务管理方式..假设在数据库层出现了错误.就会 回滚事务..数据库不会有数据

如果不采用事务..如果出现了错误..就不会回滚..数据库中会有数据.举例:

如果A转账给B A转账100给B..不使用事务.B出错了..事务不会回滚!A少了100 B没有增加

如果使用事务..则这个操作是在同一个事务下 .只要出错了事务就会回滚!可以保证用户的安全


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP