使用SpringBoot注解方式处理事务回滚实现
作者:风学长 发布时间:2023-04-30 13:59:19
标签:SpringBoot,注解,事务回滚
我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚
1 Pojo类
package com.zxf.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String pwd;
private String head_img;
private String phone;
private Date create_time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getHead_img() {
return head_img;
}
public void setHead_img(String head_img) {
this.head_img = head_img;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
}
2 Mapper接口
我们这里使用注解的方式编写SQL语句
package com.zxf.mapper;
import com.zxf.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
@Insert("insert into user (name,pwd,head_img,phone,create_time) values(#{name},#{pwd},#{head_img},#{phone},#{create_time})")
public int save(User user);
}
3 Service接口和实现类
package com.zxf.service;
import com.zxf.domain.User;
public interface UserService {
public int save(User user);
}
package com.zxf.service.impl;
import com.zxf.domain.User;
import com.zxf.mapper.UserMapper;
import com.zxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional //实现事务的时候要在业务类中加入该注解
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int save(User user) {
int f= userMapper.save(user);
// int x=5/0;
return f;
}
}
4 Controller层
package com.zxf.controller;
import com.zxf.domain.User;
import com.zxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("api/v1/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("save")
public Object save(){
User user=new User();
user.setName("zhang3");
user.setPwd("abc123");
user.setCreate_time(new Date());
user.setPhone("1789238734");
user.setHead_img("aabbddd.jpg");
userService.save(user);
return user;
}
}
5 application.properits 配置文件
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/online
spring.datasource.username=root
spring.datasource.password=******
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
6 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zxf</groupId>
<artifactId>xf_spring2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xf_spring2</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6 SpringBoot启动类
package com.zxf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.zxf.mapper")//扫描mapper接口
@EnableTransactionManagement//事务处理的时候启动类必须要加的注解
public class XfSpring2Application {
public static void main(String[] args) {
SpringApplication.run(XfSpring2Application.class, args);
}
}
7 也是最重要,也是很多人忽视的地方,就是MySQL要支持事务处理才可以
这里一定要记住;否则你的SpringBoot的事务没有任何效果
来源:https://blog.csdn.net/zhang6132326/article/details/107918892


猜你喜欢
- 1:首先。创建一个springboot项目,这里我使用以及构建好基本框架的脚手架,打开是这个样子:Result类:已经封装好了三种返回类型的
- C#文件的读和写提供了非常多的方法基本一两行就可以搞定“读和写”,在编程里还是比较重要的什么是读?你的程序去读你磁盘里的文件上面是写?你的程
- 概念在Java中,对象的生命周期包括以下几个阶段:创建阶段(Created)应用阶段(In Use)不可见阶段(Invisible)不可达阶
- 一、目标效果聊天会话页的列表效果1、聊天数据不满一屏时,顶部显示所有聊天数据2、插入消息时如果最新消息紧靠列表底部时,则插入消息会使列表向上
- 废话不多说,直接上代码String longUrl = "https://open.weixin.qq.com/connect/o
- Glide 加载图片使用到的两个记录Glide 加载图片保存至本地指定路径/** * Glide 加载图片保存到
- 一个简单的微服务架构图本文设计的 Spring Cloud 版本以及用到的 Spring Cloud 组件Spring Cloud Hoxt
- Volley简介我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发
- 一、概述Socket类是Java执行客户端TCP操作的基础类,这个类本身使用代码通过主机操作系统的本地TCP栈进行通信。Socket类的方法
- Mavan pom文件引用依赖 <!-- hutool工具类--><dependency><gro
- Shiro 是Shiro 是一个 Apache 下的一开源项目项目,旨在简化身份验证和授权。 1:shiro的配置,通过maven
- 判断有无虚拟按键(导航栏)现在很大一部分手机没有虚拟按键,一部分有。我们在做适配的时候可能会用到这方面的知识。例如:屏幕填充整个屏幕的时候,
- 强调一下阅读系统源码,起码要对进程间通信要了解,对binder机制非常非常清楚,binder就是指南针,要不然你会晕头转向;强行阅读,就容易
- JVM内存模型/内存空间Java虚拟机JVM运行起来,就会给内存划分空间,这块空间成为运行时数据区。运行时数据区主要划分为以下 6
- 防止程序运行多个实例的方法有多种,如:通过使用互斥量和进程名等.而我想要实现的是:在程序运行多个实例时激活的是第一个实例,使其获得焦点,并在
- 本文实例分析了C#反射内存的处理。分享给大家供大家参考。具体分析如下:这段时间由于公司的项目的要求,我利用c#的反射的机制做了一个客户端框架
- 推荐idea2022最新激活教程:idea2021最新激活方法https://www.jb51.net/article/197138.htm
- ButterKnife的最新版本是8.4.0。首先,需要导入ButterKnife的jar包。在AndroidStudio中,File-&g
- 这篇文章主要介绍了基于Java检查IPv6地址的合法性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- Android插件开启对新Api的支持这一天小王导入了一个库,上线之后直接崩了一大片? 找到其中的问题:什么鬼哦?安卓8.0一下无法使用?