Spring中事务的简单使用
1.什么是事务
- 事务时数据库操作最基本单元,是逻辑上的一组操作,要么都成功,如果有一个操作失败所有的操作都失败。
- 典型场景:银行转账A转账给B100元
- 两个步骤:将A的余额减少100,再将B的余额增加100
事务的四个特性(ACID)
- 原子性:事务是不可分割的,要么全部执行,要么全部不执行
- 一致性:保证数据的状态操作前和操作后保持一致
- 隔离性:多个事务同时操作相同数据库的同一个数据时,一个事务的执行不受另外一个事务的干扰
- 持久性:一个事务一旦提交,则数据将持久化到本地,除非其他事务对其进行修改
2.搭建事务环境
2.1理解事务逻辑
2.2创建事务使用的数据表并添加记录
2.3创建service,搭建dao,完成对象创建和注入关系
1.service注入dao,在dao中注入JDBCTemplate,在JDBCTemplate中注入DataSource
整体架构
UserDao文件
package com.atguigu.spring5.dao;
/**
* @author
* @create 2022-03-02 15:06
*/
public interface UserDao {
}
UserDaoImpl文件
package com.atguigu.spring5.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author
* @create 2022-03-02 15:06
*/
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
}
UserService文件
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author
* @create 2022-03-02 15:06
*/
@Service
public class UserService {
@Autowired
private UserDao userDao;
}
spring.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">
<!--组件扫描-->
<context:component-scan base-package="com.atguigu"></context:component-scan>
<!--配置数据库连接池-->
<context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driverclass}"></property>
</bean>
<!--JDBCTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
druit.properties配置文件
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT
jdbc.driverclass=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=abc123
2.4创建事务中转账的相关方法
UserDao.java 文件
package com.atguigu.spring5.dao;
/**
* @author huxuyang
* @create 2022-03-02 15:06
*/
public interface UserDao {
void addMoney();
void reduceMoney();
}
UserDaoImpl.java 文件
package com.atguigu.spring5.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author huxuyang
* @create 2022-03-02 15:06
*/
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addMoney() {
String sql = "update t_account set money = money- ? where username= ?";
jdbcTemplate.update(sql, 100, "zhangsan");
}
@Override
public void reduceMoney() {
String sql = "update t_account set money = money + ? where username= ?";
jdbcTemplate.update(sql, 100, "lisi");
}
}
UserService.java文件
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author huxuyang
* @create 2022-03-02 15:06
*/
@Service
public class UserService {
// 注入dao
@Autowired
private UserDao userDao;
// 转账的过程
public void accountMoney(){
// 1.先给张三减100
userDao.reduceMoney();
// 2.再给李四增100
userDao.addMoney();
}
}
2.5测试
package com.atguigu.spring5.test;
import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
/**
* @author huxuyang
* @create 2022-03-02 15:09
*/
public class test {
@Test