赞
踩
事务操作(事务概念)
1、什么事务
(1)事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操 作都失败
(2)典型场景:银行转账 * lucy 转账 100 元 给 mary * lucy 少 100,mary 多 100
2、事务四个特性(ACID)
(1)原子性
(2)一致性
(3)隔离性
(4)持久性
create table t_user(id int,username varchar(300),money double);
- @Service
- public class UserService {
- //注入 dao
- @Autowired
- private UserDao userDao;
- }
- @Repository
- public class UserDaoImpl implements UserDao {
- @Autowired
- private JdbcTemplate jdbcTemplate;
- }
- 3、在 dao 创建两个方法:多钱和少钱的方法,在 service 创建方法(转账的方法)
- @Repository
- public class UserDaoImpl implements UserDao {
- @Autowired
- private JdbcTemplate jdbcTemplate;
- //lucy 转账 100 给 mary
- //少钱
- @Override
- public void reduceMoney() {
- String sql = "update t_account set money=money-? where username=?";
- jdbcTemplate.update(sql,100,"lucy");
- }
- //多钱
- @Override
- public void addMoney() {
- String sql = "update t_account set money=money+? where username=?";
- jdbcTemplate.update(sql,100,"mary");
- }
- }
- @Service
- public class UserService {
- //注入 dao
- @Autowired
- private UserDao userDao;
- //转账的方法
- public void accountMoney() {
- //lucy 少 100
- userDao.reduceMoney();
- //mary 多 100
- userDao.addMoney();
- }
- }
- 4、上面代码,如果正常执行没有问题的,但是如果代码执行过程中出现异常,有问
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
编程时事务:一般不用这个原因为不灵活。
事务操作(Spring 事务管理介绍)
1、事务添加到 JavaEE 三层结构里面 Service 层(业务逻辑层)
2、在 Spring 进行事务管理操作
(1)有两种方式:编程式事务管理和声明式事务管理(使用)
3、声明式事务管理
(1)基于注解方式(使用)
(2)基于 xml 配置文件方式
4、在 Spring 进行声明式事务管理,底层使用 AOP 原理
5、Spring 事务管理 API
(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类
事务操作(生命式 事务管理 注解)
1.在sping框架中:配置xml文件配置事务管理器
- <!-- 数据库连接池 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
- destroy-method="close">
-
- <property name="url" value="jdbc:mysql://10.0.0.8:3306/test" />
- <property name="username" value="ecc_event_management" />
- <property name="password" value="94020212" />
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- </bean>
-
- <!-- JdbcTemplate 对象 -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <!--注入 dataSource-->
- <property name="dataSource" ref="dataSource"></property>
- </bean>
-
-
-
- <!-- 组件扫描 -->
- <context:component-scan base-package="sun.jdbctemp"></context:component-scan>
-
- <!--配置数据库事务 此处为新增-->
- <!--创建事务管理器 此处为新增-->
- <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>
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
2.配置sping配置文件,开启注解
(1)在sping配置文件中引入名称空间 tx
- <?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: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
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.alibaba.com/schema/stat
- http://www.alibaba.com/schema/stat.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
(2)开启事务注解
- <!--开启事务注解 此处为新增-->
- <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
(3)在service的类上面(获取service类里面的方法上)添加事务注解:
@Transactional事务注解
可以添加到类上面:表示类中的所有方法都有事务
也可以添加到方法上:表示这个方法添加事务
@Service @Transactional public class UserService
这里附上完整模拟转账的代码:
数据库数据的显示:
pojo层:
- package sun.jdbctemp.pojo;
- import org.springframework.stereotype.Repository;
- /**
- * @author sunyc
- * @create 2022-04-13 17:22
- */
- @Repository
- public class User {
- private int id;
- private String username;
- private double money;
-
- public User() {
- }
-
- public User(int id, String username, double money) {
- this.id = id;
- this.username = username;
- this.money = money;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public double getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
dao 层 接口以及实现类:
- package sun.jdbctemp.dao;
-
- import org.springframework.stereotype.Repository;
- import sun.jdbctemp.pojo.User;
-
- /**
- * @author sunyc
- * @create 2022-04-13 17:25
- */
- @Repository
- public interface UserDao {
- //添加用户
- int addUser(User user);
- //增加收入
- int addMoney(String username,double money);
- //减少收入
- int reduceMoney(String username,double money);
- }
-
-
-
- ----------------------------------------------------------------------------
- 实现接口的类
- package sun.jdbctemp.dao;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
- import sun.jdbctemp.pojo.User;
-
- /**
- * @author sunyc
- * @create 2022-04-13 17:27
- */
- @Repository
- public class UserDaoImpl implements UserDao{
- @Autowired
- private JdbcTemplate jdbcTemplate;
-
-
- @Override //添加用户
- public int addUser(User user) {
- String sql="insert into t_user values(?,?,?)";
- int update = jdbcTemplate.update(sql, user.getId(), user.getUsername(), user.getMoney());
- return update;
- }
- //加钱
- @Override
- public int addMoney(String username, double money) {
- String sql="update t_user set money=money+? where username=?";
- return jdbcTemplate.update(sql, money, username);
- }
- //减钱
- @Override
- public int reduceMoney(String username, double money) {
- String sql="update t_user set money=money-? where username=?";
- return jdbcTemplate.update(sql, money, username);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
service层:
- package sun.jdbctemp.service;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Isolation;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.transaction.reactive.TransactionContextManager;
- import sun.jdbctemp.dao.UserDao;
- import sun.jdbctemp.pojo.User;
-
- /**
- * @author sunyc
- * @create 2022-04-13 17:31
- 3、在 service 类上面(或者 service 类里面方法上面)添加事务注解
- (1)@Transactional,这个注解添加到类上面,也可以添加方法上面
- (2)如果把这个注解添加类上面,这个类里面所有的方法都添加事务
- (3)如果把这个注解添加方法上面,为这个方法添加事务
- */
- @Service
- @Transactional
- public class UserService {
- @Autowired
- private UserDao userDao;
- //添加用户数据
- public void addUser(User user){
- System.out.println(userDao.addUser(user));
- }
- //加钱
- public void addMoney(String username,double money){
- userDao.addMoney(username,money);
- }
- //减钱
- public void reduceMoney(String username,double money){
- userDao.addMoney(username,money);
- }
- //赚钱 addMoneytname 为加钱 reduceMoneyname 减钱 money 金额
- public void accountMoney(String addMoneytname,String reduceMoneyname,double money){
- //给 addMoneytname用户加钱
- userDao.addMoney(addMoneytname,money);
- int a=10/0;
- //给 reduceMoneyname 用户-钱
- userDao.reduceMoney(reduceMoneyname,money);
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
测试类:
- package sun.jdbctemp.test;
-
- import org.junit.jupiter.api.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import sun.jdbctemp.pojo.User;
- import sun.jdbctemp.service.UserService;
-
- import static org.junit.jupiter.api.Assertions.*;
-
- /**
- * @author sunyc
- * @create 2022-04-13 17:37
- */
- class UserServiceTest {
- @Test //初始化 --添加用户
- void addUser() {
- ApplicationContext context=new ClassPathXmlApplicationContext("mysqlbean.xml");
- UserService userService = context.getBean("userService", UserService.class);
- userService.addUser(new User(1,"sunchao",1000));
- userService.addUser(new User(2,"xiaoming",1000));
- }
-
- @Test//测试添加事务的转账功能
- void accountMoney() {
- ApplicationContext context=new ClassPathXmlApplicationContext("mysqlbean.xml");
- UserService userService = context.getBean("userService", UserService.class);
- userService.accountMoney("sunchao","xiaoming",100);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
数据结果显示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。