> 文档中心 > Spring5—事务

Spring5—事务

事务

  • 1. 概念
  • 2. 转账demo

1. 概念

  1. 事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操作都失败。
  2. 事务四个特性(ACID)
    (1)原子性
    (2)一致性
    (3)隔离性
    (4)持久性

2. 转账demo

在这里插入图片描述

demo整体结构

Spring5—事务

  1. 创建数据库表,并添加记录
    Spring5—事务
  2. 创建xml配置文件
<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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">        <context:component-scan base-package="com.spring"></context:component-scan>        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"   destroy-method="close"> <property name="url" value="jdbc:mysql:///userdb"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/>    </bean>        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  <property name="dataSource" ref="dataSource"></property>    </bean>        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource"></property>    </bean>        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>

或者使用全注解开发,创建配置类(作用同xml配置文件)

@Configuration //配置类注解@ComponentScan(basePackages = "com.spring")@EnableTransactionManagement //开启事务public class TxConfig {    //创建数据库连接池    @Bean    public DruidDataSource getDruidDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///userdb"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource;    }    //创建JdbcTemplate对象    @Bean    public JdbcTemplate getJdbcTemplate(DataSource dataSource) { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate;    }    //创建事务管理器    @Bean    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager;    }}
  1. 创建UserDaoImpl.java和UserService.java
    UserDaoImpl.java
@Repositorypublic class UserDaoImpl implements UserDao {    @Autowired    private JdbcTemplate jdbcTemplate;    @Override    public void add() { String sql = "update t_account set money = money + ? where username = ?"; jdbcTemplate.update(sql, 100, "CLarie");    }    @Override    public void reduce() { String sql = "update t_account set money = money - ? where username = ?"; jdbcTemplate.update(sql, 100, "Chiancc");    }}

UserService.java

@Service@Transactional  //开启事务public class UserService {    //调用两个方法完成转账    @Autowired    private UserDao userDao;    public void transfer() { userDao.reduce(); // int i = 2 / 0; userDao.add();    }}
  1. 测试类TestTrans.java
public class TestTrans {    @Test    public void test() { ApplicationContext context =  new ClassPathXmlApplicationContext("config.xml"); //使用xml配置文件 UserService userService = context.getBean("userService", UserService.class); userService.transfer();    }    @Test    public void test2() { ApplicationContext context =  new AnnotationConfigApplicationContext(TxConfig.class); //使用配置类 UserService userService = context.getBean("userService", UserService.class); userService.transfer();    }}