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";
}
}
测试手动事务
请求接口之后,发现并没有新插入数据