目录
- 创建springboot项目
- 引入myBATis和数据库依赖
- 创建springboot配置文件
- 创建启动类
- 创建数据库实体
- 创建Mapper
- 创建mapper配置文件
- 创建controller
- 测试手动事务
创建springboot项目
搭建最简单的SpringBoot项目-CSDN博客
引入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> <http://www.devze.com/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(Sprihttp://www.devze.comngbootMybatisApp.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 uEugAlrjshRser); }
创建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); EugAlrJShR int i =0; int i1 = 1 / i; tjsransactionManager.commit(transStatus); } catch (Exception e) { e.printStackTrace(); transactionManager.rollback(transStatus); } return "success"; } }
测试手动事务
请求接口之后,发现并没有新插入数据
到此这篇关于springboot mybatis手动事务的实现的文章就介绍到这了,更多相关springboot mybatis手动事务内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论