Springboot2.X——手动回滚事务
程序员文章站
2022-07-07 10:39:46
...
一、demo前的准备
1.1、pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 引入rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>5.0.4</version> -->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 2.导入mybatis库文件 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 使用aop拦截器 需要的aop库文件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 使用thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 3.logback打印sql日志信息 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- 阿里巴巴json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
<!-- lombok 引入 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<!-- 热部署方式2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<!-- 热部署 end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1.2、项目结构
二、手动回滚方式
2.1、service实现类编写
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import cn.linkpower.dao.IUserMapper;
import cn.linkpower.service.IUserService;
import cn.linkpower.vo.Usrs;
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private IUserMapper userMapper;
@Transactional
@Override
public int addUser(Usrs user) {
userMapper.addUser(user);
return 0;
}
@Transactional
@Override
public int addUser2(Usrs user) {
userMapper.addUser(user);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return 0;
}
}
1.3、yml编写
server:
port: 80
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: xiangjiao
password: bunana
virtual-host: /xiangjiao
publisher-confirms: true #消息发送到转发器确认机制,是都确认回调
publisher-returns: true
datasource:
url: jdbc:mysql://127.0.0.1:3306/wanwan?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
#mybatis 配置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations:
- classpath:mybatis/user/**/*.xml
2.2、controller
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.linkpower.service.IUserService;
import cn.linkpower.service.impl.UserServiceImpl;
import cn.linkpower.vo.Usrs;
@Controller
public class TestController {
private static Logger log = LoggerFactory.getLogger(TestController.class);
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private IUserService userServiceImpl;
@RequestMapping("/sendMsg")
@ResponseBody
public String sendMsg(HttpServletRequest request){
String msg = request.getParameter("msg");
log.info("sendMsg --- msg--"+String.valueOf(msg));
rabbitTemplate.convertAndSend(msg);
return "send ok";
}
@RequestMapping("/addUser")
@ResponseBody
public String addUsers(){
Usrs user = new Usrs();
user.setUsername("xiangjiao");
user.setPwd("123456");
userServiceImpl.addUser(user);
return "ok";
}
@RequestMapping("/addUser2")
@ResponseBody
public String addUsers2(){
Usrs user = new Usrs();
user.setUsername("xiangjiao2");
user.setPwd("123456");
userServiceImpl.addUser2(user);
return "ok";
}
}
三、测试
请求正常的添加操作
请求添加了手动回滚代码的接口
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
四、部分代码文件下载
https://github.com/765199214/springboot2.0-transaction-noauto
上一篇: C语言文件操作总结
下一篇: c语言总结 7:文件相关操作