畅谈springboot2.0对数据库事务的处理

springboot 2.0 中,对数据库的事务 采用注解方式进行处理,推荐@Tranactional 加在接口的实现类上,这样的话,无论spring 使用哪种动态代理实现 对事务的操作, 该注解用在实现类上 比较便于管理,并且更加利灵活;

【PS】:倘若@Tranactional 加载 interface 上,只能够使用JDK 代理 ,这样的话,对代码使用以及编程 有很大的限制;

demo 分享:
https://github.com/medoo-Ai/transaction

create table t_user (
id int(12) auto_increment,
user_name varchar(60) not null,
note varchar(512),
primary key(id)
);

serviceImpl 添加事务控制、

@Slf4j
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public User getUser(Long id) {
        log.info(" getUser {}",id);
        return this.userMapper.getUser(id);
    }

    @Override
    @Transactional
    public int insertUser(User user) {
        log.info(" insertUser {}",user);
        return this.userMapper.insertUser(user);
    }
}

你可能感兴趣的:(死磕SpringBoot2.0)