springBoot 事务

一、简介    

   使用 springBoot + mybatis + mysql 实现一个简单的事务操作说明,主要使用 @Transactional 注解。

二、编码

2.1 mapper 层和 model 层编码

@Mapper
public interface UserMapper2 {
	
	@Select("select id,name,password,age from user")
	List<User> findUserAll();
	
	@Delete("delete from user where id=#{id}")
	int deleteUserById(int id);
	
	@Update({"update user set name=#{name},password=#{password},age=#{age} where id=#{id}"})
	int updateUserById(User user);
	
	@Insert({"insert into user(name,password,age) values(#{name},#{password},#{age})"})
	int insertUser(User user);
	
	@Select({"select * from user where id=#{id}"})
	User selectUserById(int id);
}
public class User {
	private int id;
	private String name;
	private String password;
	private int age;
	
	public User(String name, String password, int age) {
		super();
		this.name = name;
		this.password = password;
		this.age = age;
	}
    // setter、getter、toString()
}

2.2 service 层编码

public interface UserService2 {
	public List<User> findAll();
	int deleteUserById(int id);
	int updateUserById(User user);
	int insertUser(User user);
	User selectUserById(int id);
}
@Service
public class UserServiceImpl2 implements UserService2 {
	@Autowired
	UserMapper2 mapper;

	@Override
	public List<User> findAll() {
		
		return mapper.findUserAll();
	}
	@Override
	public int deleteUserById(int id) {
		
		return mapper.deleteUserById(id);
	}
	@Override
	public int updateUserById(User user) {
		
		return mapper.updateUserById(user);
	}
	@Override
	public int insertUser(User user) {
		
		return mapper.insertUser(user);
	}
	@Override
	public User selectUserById(int id) {
		
		return mapper.selectUserById(id);
	}
}

2.3 controller 层编码

@RestController
public class TestTransactionController {

	@Autowired
	UserService2 userService;
	
	@RequestMapping("/test1Transaction")
	public int test1Transaction() {
		userService.insertUser(new User("1","a",80));
		userService.insertUser(new User("2","a",80));
		userService.insertUser(new User("3","a",80));
		userService.insertUser(new User("1111111","a",80));
		userService.insertUser(new User("5","a",80));
		return 0;
	}
	
	@RequestMapping("/test2Transaction")
	@Transactional
	public int test2Transaction() {
		userService.insertUser(new User("6","a",80));
		userService.insertUser(new User("7","a",80));
		userService.insertUser(new User("8","a",80));
		userService.insertUser(new User("1111111","a",80));
		userService.insertUser(new User("10","a",80));
		return 0;
	}
}

2.4 编写数据库脚本

DROP DATABASE IF EXISTS zlits_user;
CREATE DATABASE zlits_user;
USE zlits_user;
SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(5) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '名字',
  `password` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '密码',
  `age` bigint(20)  DEFAULT NULL  COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='人员信息表';

2.5 项目结构图

三、测试

3.1 未添加事务测试

        ​在浏览器输入 http://localhost:8080/test1Transaction,由于第四条数据超长,所以发生了存储失败,但是前三条数据存储成功。数据并没有发生回滚,这个不是我们想要的东西。

3.2 添加事务测试

        ​在浏览器输入 http://localhost:8080/test2Transaction,查看数据库,发现数据没有存储成功,还是第一次存储的那三条数据。证明发生了回滚的操作,事务生效了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值