spring mvc 把事物处理配置到Controller层
本来事物处理是要配置到service的,无奈项目是这样的,来到新公司接手的项目是多个项目用的公共的service,为了不在service中不添加不是公用的方法,每个项目用到的方法都写在了controller层,现在呢要给一些多表操作的方法添加事物处理,本来是打算把controller层的方法挪到service,但是这样的公用的service就会会添加很多方法,而另一个项目就用不到,或者另一个项目用到的方法这个项目用不到。最终决定 如果事物能加在controller层 就可以解决现在的问题了。
具体做法如下 只罗列关键配置
在spring.xml中配置(就是sprign的配置文件)
<!-- 扫描service、dao -->
<!-- 扫描注解@Component , @Service , @Repository。 要把 controller去除,controller是在spring-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<!-- 扫描service、dao -->
<!-- 扫描注解@Component , @Service , @Repository。 要把 controller去除,controller是在spring-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<context:component-scan base-package="com.systek.scenic"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<!-- 或者如下写法也行 include是包含 exclude是排除 任意注释掉一个或者都不注释都可以的-->
<!--<context:component-scan base-package="com.systek.scenic">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>-->
在mvc.xml中配置(就是spring Mvc的配置文件)
<!-- 注解扫描包 --> <context:component-scan base-package="com.systek.scenic.web.controller"/> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 开启注解 --> <import resource="classpath:spring-tx.xml"/>
在事物处理spring-tx.xml的配置文件中配置
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 事务配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dbPool" /> </bean> <!-- 使用annotation注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
然后在>controller层中对应的方法上加上@Transactional注解即可 如
@RequestMapping(value = "/Save", method = RequestMethod.POST)
@ResponseBody
//@Transactional(rollbackFor = { Exception.class })
@Transactional
public Result save(HttpServletRequest request, GuiderRentVO guiderRentVO, Model model)
{
//......
}
经测试注解@Transactional写在controller是管用的 写在service是不管用的,因为我们的事务处理代码写在了mvc.xml中
如果需要在service也支持 可以尝试在spring.xml中写上事物处理的代码(本人尚未测试哦 只是思路)
分类:
java各框架
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
2016-09-05 android 之 webView 显示h5 执行选择图片或者拍照功能