springboot mybatis手动事务的实现
作者:Steven-Russell
本文主要介绍了springboot mybatis手动事务的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
创建springboot项目
引入mybatis和数据库依赖
<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.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> <scope>provided</scope> </dependency> </dependencies>
创建springboot配置文件
spring: datasource: url: jdbc:mysql://192.168.30.128:3306/wd username: root password: 4752 mybatis: mapper-locations: classpath:mapper/*Mapper.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
创建启动类
package com.wd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootMybatisApp { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisApp.class, args); } }
创建数据库实体
package com.wd.pojo; import lombok.Data; import java.util.Date; @Data public class User { private int id; private String name; private String addr; private Date updateTime; }
创建Mapper
package com.wd.mapper; import com.wd.pojo.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { int addUser(User user); }
创建mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wd.mapper.UserMapper"> <insert id="addUser"> insert into tbl_user (id, name, addr, update_time) values (#{id}, #{name}, #{addr}, #{updateTime}) </insert> </mapper>
创建controller
package com.wd.controller; import com.wd.mapper.UserMapper; import com.wd.pojo.User; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.web.bind.annotation.*; import java.util.Date; @RestController public class UserController { private final UserMapper userMapper; private final PlatformTransactionManager transactionManager; public UserController(UserMapper userMapper, PlatformTransactionManager platformTransactionManager) { this.userMapper = userMapper; this.transactionManager = platformTransactionManager; } @GetMapping(value = "testTransaction") public String testTransaction() { DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition(); transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus transStatus = transactionManager.getTransaction(transDefinition); try { User user = new User(); user.setId(1); user.setName("aaa"); user.setUpdateTime(new Date()); userMapper.addUser(user); int i =0; int i1 = 1 / i; transactionManager.commit(transStatus); } catch (Exception e) { e.printStackTrace(); transactionManager.rollback(transStatus); } return "success"; } }
测试手动事务
请求接口之后,发现并没有新插入数据
到此这篇关于springboot mybatis手动事务的实现的文章就介绍到这了,更多相关springboot mybatis手动事务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!