springboot使用事物注解方式代码实例
作者:&天涯海角& 时间:2022-07-09 00:13:21
这篇文章主要介绍了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
标签:spring,boot,事物,注解,方式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c++异常处理机制示例及详细讲解
2022-04-27 11:31:27
Android 判断日期是否在一年以内的算法实例
2023-03-22 06:04:13
checkpoint 机制具体实现示例详解
2023-03-31 21:42:01
浅谈resultMap的用法及关联结果集映射
2022-12-25 17:42:21
![](https://img.aspxhome.com/file/2023/5/129985_0s.png)
C#实现Excel转PDF时设置内容适应页面宽度
2022-09-15 19:23:26
![](https://img.aspxhome.com/file/2023/6/116556_0s.png)
Spring Security添加验证码的两种方式小结
2021-08-05 17:24:25
SpringBoot微服务注册分布式Consul的详细过程
2022-05-30 04:27:53
![](https://img.aspxhome.com/file/2023/4/129184_0s.jpg)
详解C# FileStream类
2022-10-19 13:44:27
springboot多模块化整合mybatis,mapper自动注入失败问题及解决
2022-02-20 08:06:29
![](https://img.aspxhome.com/file/2023/8/60778_0s.jpg)
使用Filter过滤器中访问getSession()要转化
2022-10-01 16:20:04
Springboot公共字段填充及ThreadLocal模块改进方案
2023-11-17 22:58:39
![](https://img.aspxhome.com/file/2023/4/59184_0s.png)
java制作复制文件工具代码分享
2022-08-05 05:30:22
C#集合之字典的用法
2022-04-11 23:27:19
![](https://img.aspxhome.com/file/2023/0/108790_0s.jpg)
C#枚举类型与位域枚举Enum
2023-03-02 06:52:27
springboot整合JSR303校验功能实现代码
2023-10-11 21:46:25
![](https://img.aspxhome.com/file/2023/8/72378_0s.jpg)
Android 实现当下最流行的吸顶效果
2021-12-30 18:46:33
![](https://img.aspxhome.com/file/2023/6/113276_0s.gif)
springboot默认日志框架选择源码解析(推荐)
2021-06-19 08:02:51
Android实现3D云标签效果
2022-06-05 10:33:50
![](https://img.aspxhome.com/file/2023/2/113322_0s.gif)
Java实现复制文件并命名的超简洁写法
2022-02-12 12:54:06
android TextView设置中文字体加粗实现方法
2023-08-06 02:32:03