springboot mybatis手动事务

创建springboot项目

搭建最简单的SpringBoot项目-CSDN博客

引入mybatis和数据库依赖


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.3.0
    
    
        mysql
        mysql-connector-java
        8.0.33
    
    
        org.projectlombok
        lombok
        1.18.28
        provided
    

创建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配置文件






    
        insert into tbl_user (id, name, addr, update_time) values (#{id}, #{name}, #{addr}, #{updateTime})
    


创建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手动事务_第1张图片

你可能感兴趣的:(spring,boot,mybatis,java)