TransactionTemplate的使用
总结:在类中注入TransactionTemplate,即可在springboot中使用编程式事务。
spring支持编程式事务管理和声明式事务管理两种方式。
编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。Spring建议使用TransactionTemplate来管理编程事务。
声明式事务管理建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。Spring Boot推荐使用@Transactional注解来实现声明式事务管理。
1.为何用?
多数情况下,方法上声明@Transactional注解声明事务即可,简单、快捷、方便,但@Transactional声明式事务的可控性太弱了,只可在方法或类上声明,做不到细粒度的事务控制。
如果一个方法前10条sql都是select查询语句,只有最后2条sql是update语句,那么只对最后2条sql做事务即可。
2.如何用
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
springboot中引入mybatis-spring-boot-starter依赖包即可。
mybatis-spring-boot-starter依赖包中包含了spring-boot-starter-jdbc的依赖,spring-boot-starter-jdbc中包含DataSourceTransactionManager事务管理器以及自动注入配置类DataSourceTransactionManagerAutoConfiguration。
代码中使用,在使用bean中注入TransactionTemplate即可:
@Service public class TestServiceImpl { @Resource private TransactionTemplate transactionTemplate; public Object testTransaction() { //数据库查询 dao.select(1); return transactionTemplate.execute(status -> { //数据库新增 dao.insert(2); dao.insert(3); return new Object(); }); } }
TransactionTemplate简单使用
/** * 事务模板 * @author zz * */ public class TransactionTemplateSupport { @Autowired private PlatformTransactionManager transactionManager; private TransactionTemplate requiredTransactionTemplate; protected TransactionTemplate getRequiresNewTransactionTemplate(){ if (requiredTransactionTemplate == null){ requiredTransactionTemplate = new TransactionTemplate(transactionManager); requiredTransactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED); // requiredTransactionTemplate.setReadOnly(true); // requiredTransactionTemplate.setTimeout(30000); } return requiredTransactionTemplate; } }
@Service public class TestTransaction extends TransactionTemplateSupport { @Autowired private JdbcTemplate jdbcTemplate ; @Autowired private TransactionTemplate transactionTemplate; // @Transactional public void test(){ jdbcTemplate.execute("insert into user value (1,'aaa','aaa','aaa')"); int i = 1/0; jdbcTemplate.execute("insert into user value (2,'aaa','aaa','aaa')"); } public void test2(){ getRequiresNewTransactionTemplate() // transactionTemplate .execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { jdbcTemplate.execute("insert into user value (11,'BBBB','aaa','aaa')"); int i = 1/0; jdbcTemplate.execute("insert into user value (21,'aaa','NNNN','aaa')"); return null; } }); } }
以上是springboot编程式事务TransactionTemplate如何使用的详细内容。更多信息请关注PHP中文网其他相关文章!

Canal工作原理Canal模拟MySQLslave的交互协议,伪装自己为MySQLslave,向MySQLmaster发送dump协议MySQLmaster收到dump请求,开始推送binarylog给slave(也就是Canal)Canal解析binarylog对象(原始为byte流)MySQL打开binlog模式在MySQL配置文件my.cnf设置如下信息:[mysqld]#打开binloglog-bin=mysql-bin#选择ROW(行)模式binlog-format=ROW#配置My

前言SSE简单的来说就是服务器主动向前端推送数据的一种技术,它是单向的,也就是说前端是不能向服务器发送数据的。SSE适用于消息推送,监控等只需要服务器推送数据的场景中,下面是使用SpringBoot来实现一个简单的模拟向前端推动进度数据,前端页面接受后展示进度条。服务端在SpringBoot中使用时需要注意,最好使用SpringWeb提供的SseEmitter这个类来进行操作,我在刚开始时使用网上说的将Content-Type设置为text-stream这种方式发现每次前端每次都会重新创建接。最

一、手机扫二维码登录的原理二维码扫码登录是一种基于OAuth3.0协议的授权登录方式。在这种方式下,应用程序不需要获取用户的用户名和密码,只需要获取用户的授权即可。二维码扫码登录主要有以下几个步骤:应用程序生成一个二维码,并将该二维码展示给用户。用户使用扫码工具扫描该二维码,并在授权页面中授权。用户授权后,应用程序会获取一个授权码。应用程序使用该授权码向授权服务器请求访问令牌。授权服务器返回一个访问令牌给应用程序。应用程序使用该访问令牌访问资源服务器。通过以上步骤,二维码扫码登录可以实现用户的快

我们使用jasypt最新版本对敏感信息进行加解密。1.在项目pom文件中加入如下依赖:com.github.ulisesbocchiojasypt-spring-boot-starter3.0.32.创建加解密公用类:packagecom.myproject.common.utils;importorg.jasypt.encryption.pbe.PooledPBEStringEncryptor;importorg.jasypt.encryption.pbe.config.SimpleStrin

1.springboot2.x及以上版本在SpringBoot2.xAOP中会默认使用Cglib来实现,但是Spring5中默认还是使用jdk动态代理。SpringAOP默认使用JDK动态代理,如果对象没有实现接口,则使用CGLIB代理。当然,也可以强制使用CGLIB代理。在SpringBoot中,通过AopAutoConfiguration来自动装配AOP.2.Springboot1.xSpringboot1.xAOP默认还是使用JDK动态代理的3.SpringBoot2.x为何默认使用Cgl

知识准备需要理解ApachePOI遵循的标准(OfficeOpenXML(OOXML)标准和微软的OLE2复合文档格式(OLE2)),这将对应着API的依赖包。什么是POIApachePOI是用Java编写的免费开源的跨平台的JavaAPI,ApachePOI提供API给Java程序对MicrosoftOffice格式档案读和写的功能。POI为“PoorObfuscationImplementation”的首字母缩写,意为“简洁版的模糊实现”。ApachePOI是创建和维护操作各种符合Offic

1.首先新建一个shiroConfigshiro的配置类,代码如下:@ConfigurationpublicclassSpringShiroConfig{/***@paramrealms这儿使用接口集合是为了实现多验证登录时使用的*@return*/@BeanpublicSecurityManagersecurityManager(Collectionrealms){DefaultWebSecurityManagersManager=newDefaultWebSecurityManager();

一、定义视频上传请求接口publicAjaxResultvideoUploadFile(MultipartFilefile){try{if(null==file||file.isEmpty()){returnAjaxResult.error("文件为空");}StringossFilePrefix=StringUtils.genUUID();StringfileName=ossFilePrefix+"-"+file.getOriginalFilename(


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器