SpringBoot事物声明

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42969074/article/details/85083701

前面用SpringBoot整合了Mybatis,这里就用这个代码配置一下事物,springboot开启事务很简单,只需要一个注解@Transactional 就可以了。

首先要在启动类上面加一个@EnableTransactionManagement  表示开启SpringBoot的事物支持:

package com.thr;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@MapperScan("com.thr.dao")
@EnableTransactionManagement //开启SpringBoot的事物支持
public class SpringBootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }

}

在UserService层添加一个update方法:

package com.thr.service;

import com.thr.entity.User;

import java.util.List;

/**
 * @author
 * @name
 */
public interface UserService {

    List<User> findAllUser();

    int update();
}

UserService的实现类:这里在update方法上面添加了@Transactional注解,说明该方法已经添加的事物。

package com.thr.service.impl;

import com.thr.dao.UserMapper;
import com.thr.entity.User;
import com.thr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author
 * @name
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Transactional
    @Override
    public int update() {
        User user=new User();
        user.setId(1);
        user.setName("admin-update");
        user.setPassword("654321");
        int i=userMapper.updateByPrimaryKeySelective(user);
        System.out.println("更新结果"+i);

        //因为除数不为0,所以会出现异常,看是否回滚
        int num=10/0;

        return i;
    }
}

Controller类:

package com.thr.controller;


import com.thr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @name
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/user")
    public Object User(){
        return userService.findAllUser();
    }

    @GetMapping(value = "/update")
    public Object update(){
        return userService.update();
    }
}

然后运行启动类,访问http://localhost:8080/update 会报错,因为10不能除以0,然后我们看数据是否回滚了:

可以发现数据并没有发生改变,说明数据回滚,事物配置成功,然后将事物注解去掉:

    //@Transactional
    @Override
    public int update() {
        User user=new User();
        user.setId(1);
        user.setName("admin-update");
        user.setPassword("654321");
        int i=userMapper.updateByPrimaryKeySelective(user);
        System.out.println("更新结果"+i);

        //因为除数不为0,所以会出现异常,看是否回滚
        int num=10/0;

        return i;
    }

 重新运行,访问,也会出现异常,但是发现数据已经发生了改变。 

猜你喜欢

转载自blog.csdn.net/qq_42969074/article/details/85083701
今日推荐