软件编程
位置:首页>> 软件编程>> java编程>> springboot使用事物注解方式代码实例

springboot使用事物注解方式代码实例

作者:&天涯海角&  发布时间:2022-07-09 00:13:21 

标签:spring,boot,事物,注解,方式

这篇文章主要介绍了springboot使用事物注解方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考

1.在启动类Application中添加注解@EnableTransactionManagement


import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //开启书屋管理注解模式 最新的版本可以省略
@MapperScan("com.xz.springboot.mapper") //扫描该包下所有的接口并为该接口生成实现类
public class Springboot01Application {
 public static void main(String[] args) {
   SpringApplication.run(Springboot01Application.class, args);
 }
}

2.在业务层添加@Transactional


import com.xz.springboot.bean.User;
import com.xz.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserService {
   @Autowired
   private UserMapper userMapper;
   public List<User> queryAll(){
     System.out.println("热部署");
     return userMapper.findAll();
   }
 @Transactional
 public void deleteById(Integer id) {
     userMapper.deleteById(id);
    // int c=10/0;
 }
}

来源:https://www.cnblogs.com/sitian2050/p/11824850.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com