spring + springmvc+ mybatis 事务管理及控制
2023-09-14 09:07:46 时间
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 对dataSource 数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 事务管理 属性/通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <!-- select,count开头的方法,开启只读,提高数据库访问性能 --> <tx:method name="select*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- 对其他方法 使用默认的事务管理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事务 aop 配置/纳入 --> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* 包名..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> </aop:config> </beans>
将如上代码方法另存一个XML并在web.xml中引用,或直接加载在web.xml中。关于expression的详解 可以查看我的另一篇文章,里面有详解。
最简单的方法是先在spring配置文件里加上
1
|
< tx:annotation-driven transaction-manager = "transactionManager" /> |
再在Controller上使用spring的事务注解
也可以用AOP方式配置事务
1
2
3
4
5
6
|
< tx:advice id = "txAdvice" transaction-manager = "transactionManager" > < tx:attributes > <!--添加方法名称正则表达式以及事务属性--> < tx:method name = "*" propagation = "REQUIRED" read-only = "true" /> </ tx:attributes > </ tx:advice > |
1
2
3
4
5
6
|
< aop:config proxy-target-class = "true" > < aop:pointcut id = "aspectMethods" expression = "execution(* org.xxx.controller.*.*(..))" /> < aop:advisor advice-ref = "txAdvice" pointcut-ref = "aspectMethods" /> </ aop:config > |
相关文章
- Spring学习笔记(三)——Spring注解开发&spring与Junit整合测试
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- Spring学习笔记(九)——SpringMVC实现文件上传
- Spring学习笔记(十八)——spring日志框架的配置和使用
- idea web项目 怎么配置 artifacts_springmvc配置视频
- Spring MVC框架学习(二)---- 使用原生的配置 熟悉 SpringMVC 的执行流程
- Spring batch教程 之 spring batch简介
- SpringMVC框架介绍[通俗易懂]
- SpringMVC 执行流程
- springmvc笔记_SpringMVC优点
- spring springMVC mybatis_javaweb servlet
- spring application context in module springmvc-6.file is included 4 contexts
- 理论:第五章:Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的
- Java项目:CRM客户关系管理系统(Spring+SpringMVC+MyBatis + maven)
- springmvc集成使用redis过程
- SpringMVC+uploadify3.2.1版实现附件上传功能详解编程语言
- springMVC入门项目demo详解编程语言
- springMVC spring mvc velocity的模板引擎详解编程语言
- springmvc 构造RESTful URL详解编程语言
- spring的AOP(五)—-Spring AOP 编程(AspectJ )详解编程语言
- spring的启动过程——spring和springMVC父子容器的原理详解编程语言
- SpringMVC防止XSS注入详解编程语言
- Spring Boot(十二):spring boot如何测试打包部署详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- springmvc整合redis架构搭建实例详解编程语言
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- Spring框架致力于搭建基于MSSQL的稳健开发体系(spring mssql)