最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SSM如何實現(xiàn)在Controller中添加事務(wù)管理

 更新時間:2022年02月26日 12:14:25   作者:van久  
這篇文章主要介紹了SSM如何實現(xiàn)在Controller中添加事務(wù)管理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SSM在Controller中添加事務(wù)管理

本人使用:

  • 集成開發(fā)環(huán)境:idea
  • 項目管理工具:maven
  • 數(shù)據(jù)庫:oracle
  • 框架:Spring+SpringMVC+myBatis

一般而言,事務(wù)都是加在Service層的,但也可以加在Controller層。。                        

看了不少人的博客,總結(jié)出兩個方法:

  • 在controller層寫編程式事務(wù)
  • 將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中

現(xiàn)在具體來說說怎么實現(xiàn)的:

1.在controller層寫編程式事務(wù)【繁瑣,不推薦】

spring-mybatis.xml中事物管理器的配置依舊

<!-- 配置數(shù)據(jù)源事務(wù) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? <property name="dataSource" ?ref="dataSource"/>
</bean>?
<!--?
? ? 注解方式配置事務(wù) @Transactional
? ? 但因為是在controller中寫編程式事務(wù),這里可以不配置<tx:annotation-driven transaction-manager="transactionManager" />
-->
<tx:annotation-driven transaction-manager="transactionManager" />

在controller中的方法里編寫事務(wù)

//在每個controller中注入transactionManager
@Resource
private PlatformTransactionManager transactionManager;
?
@PostMapping(value = "setCode")
@ResponseBody
public void setCode(Invoice invoice, InvoiceAddress invoiceAddress,String token,String orderIDs,
? ? ? ? ? ? ? ? ? ? Integer pid,HttpServletResponse response){?
? ? DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
? ? defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
? ? TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition);
?
? ? try {
? ? ? ? invoiceService.insert(token,pid,invoice);
? ? ? ? int iID= invoice.getId();
? ? ? ? String substring = orderIDs.substring(0, orderIDs.length()-1);
? ? ? ? String[] split = substring.split(",");
? ? ? ? for (String string2 : split) {
? ? ? ? ? ? bOrderService.updateIStatus("1",string2);
? ? ? ? }
? ? ? ? invoiceOrderService.insert(iID,substring);
? ? ? ? if(Integer.parseInt(invoice.getiType())==1){
? ? ? ? ? ? invoiceAddressService.insert(iID,invoiceAddress);
? ? ? ? }
?
? ? ? ? System.out.println("======制造一個運行時異常aa======");
? ? ? ? System.out.println("運行時異常:"+100/0);
?
? ? ? ? //沒有異常便手動提交事務(wù)
? ? ? ? transactionManager.commit(status);
? ? ? ? printJson(response,result(200,"ok"));
? ? }catch (Exception e){
? ? ? ? //有異常便回滾事務(wù)
? ? ? ? transactionManager.rollback(status);
? ? ? ? e.printStackTrace();
? ? ? ? printJson(response,result(500,"false"));
? ? }?
}

2.將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中【簡單明了、一勞永逸】

spring-mybatis.xml中事物管理器配置不變

在spring-mvc.xml中也定義事務(wù)配置:

<!--
? ? 命名空間中 加入:
? ? xmlns:tx="http://www.springframework.org/schema/tx"? ??
? ? http://www.springframework.org/schema/tx
? ? http://www.springframework.org/schema/tx/spring-tx.xsd
-->
<tx:annotation-driven/>

將@Transactional(rollbackFor = { Exception.class })注解打在Controller上

@Controller
@RequestMapping(value = "/invoiceC")
@Transactional(rollbackFor = { Exception.class })
public class InvoiceController extends BaseController {??
? ? @Autowired
? ? private InvoiceService invoiceService;
?
? ? @Autowired
? ? private InvoiceOrderService invoiceOrderService;
?
? ? @Autowired
? ? private InvoiceAddressService invoiceAddressService;
?
? ? @Autowired
? ? private BalanceRechangeOrderService bOrderService;? ??
?
? ? @PostMapping(value = "setCode")
? ? @ResponseBody
? ? public void setCode(Invoice invoice, InvoiceAddress invoiceAddress,String token,String orderIDs,
? ? ? ? ? ? ? ? ? ? ? ? Integer pid,HttpServletResponse response){
? ? ? ? invoiceService.insert(token,pid,invoice);? ? ? ??
? ? ? ? int iID= invoice.getId();
? ? ? ? String substring = orderIDs.substring(0, orderIDs.length()-1);//截取最后一個
? ? ? ? String[] split = substring.split(",");//以逗號分割?
? ? ? ? for (String string2 : split) {
? ? ? ? ? ? bOrderService.updateIStatus("1",string2);
? ? ? ? }?
? ? ? ? invoiceOrderService.insert(iID,substring);?
? ? ? ? if(Integer.parseInt(invoice.getiType())==1){
? ? ? ? ? ? //紙質(zhì)發(fā)票,收貨地址
? ? ? ? ? ? invoiceAddressService.insert(iID,invoiceAddress);
? ? ? ? }?
? ? ? ? System.out.println("======制造一個運行時異常aa======");
? ? ? ? System.out.println("運行時異常:"+100/0);
? ? ? ? printJson(response,result(200,"ok"));?
? ? }
}

現(xiàn)在,我們來談?wù)劄槭裁粗埃浚?=》

  • 在spring-mybatis.xml的<aop:config>添加對Controller的聲明式事務(wù)攔截
  • 在Controller的class加上@Transactional

兩者均未生效呢???

原理:因為spring容器和spring-mvc是父子容器。在服務(wù)器啟動時,會先加載web.xml配置文件 ==> 再加載spring配置文件 ==> 再回到web.xml【加載監(jiān)聽器;加載過濾器;加載前端控制器】==>再加載springMVC配置文件

在Spring配置文件中,我們掃描注冊的是service實現(xiàn)類,就算掃描注冊了controller 也會在后面加載SpringMVC配置文件[掃描注冊controller]覆蓋掉,所以想要在controller中實現(xiàn)事務(wù)管理,僅在spring配置文件配置<tx:annotation-driven>或<aop:config>是沒有效果的,必須將事務(wù)配置定義在Spring MVC的應(yīng)用上下文(spring-mvc.xml)中。

因為在spring-framework-reference.pdf文檔中說明了:                                                                                                                                    

<tx:annoation-driven/>只會查找和它在相同的應(yīng)用上下文件中定義的bean上面的@Transactional注解

SSM下Controller層的事務(wù)配置問題

在寫項目過程中遇到了多表聯(lián)合修改數(shù)據(jù)時的事務(wù)問題,按照之前的學(xué)習(xí),事務(wù)都是配置在service層中的,但是我的項目模塊里一個service對應(yīng)一個數(shù)據(jù)表,所以想在controller層加一個針對多個表的數(shù)據(jù)修改以及添加的事務(wù)配置。悲慘的是,在controller層配置事務(wù)出錯沒有回滾!

按照我已所接觸的邏輯,控制層是不建議寫業(yè)務(wù)邏輯的,所以在里面調(diào)用的是多個service層的接口(使用Autowired)來調(diào)用多個表的業(yè)務(wù)操作。但是多個表形成一個事務(wù),所以我沒找在service層里單獨添加事務(wù)的合適的方法。如果有前輩想到合適的方法,望賜教!叩謝!

解決

原來的配置

首先是在service層上添加事務(wù)的配置,我這里的事務(wù)處理采用的是注解的方式,所以配置文件相較于配置事務(wù)的方式大大簡化了。

首先命名空間中加入:

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd

然后是xml文件的配置:

? <!-- service除了業(yè)務(wù)(操作dao)還要有事務(wù) -->
? <tx:annotation-driven
? transaction-manager="txManager" />
? <!-- 配置Spring的聲明式事務(wù)管理器 -->
? <bean id="txManager"
? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? <property name="dataSource" ref="dataSource"></property>
? </bean>

其中,數(shù)據(jù)源我是配置在了dao層的配置文件中,由于都在spring的管理之下,所以在service直接使用是能夠找到的。

以下是我的maven依賴的jar包版本: 

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
? <dependency>
? ?? ?<groupId>org.springframework</groupId>
? ?? ?<artifactId>spring-tx</artifactId>
? ?? ?<version>5.1.5.RELEASE</version>
? </dependency>
? <!-- Spring jdbc事務(wù)管理 -->
? <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
? <dependency>
? ?? ?<groupId>org.springframework</groupId>
? ?? ?<artifactId>spring-jdbc</artifactId>
? ?? ?<version>5.1.5.RELEASE</version>
? </dependency>

以上是我起初的配置。但是僅僅這樣是無法在controller層添加事務(wù)的。

修正后的配置

在service層的配置文件不變的情況下,我們想要在controller層添加事務(wù),只需要在spring-mvc.xml中引入事務(wù)的注解驅(qū)動標(biāo)簽即可。

<!--在xml文件頭部引入命名空間,參考serviice層-->
<tx:annotation-driven/>

為什么會這樣?

首先我們來看配置文件的加載:

? <!-- 配置前端控制器 -->
? <servlet>
? <servlet-name>DispatcherServlet</servlet-name>
? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? <init-param>
? ?? ?<param-name>contextConfigLocation</param-name>
? ?? ?<param-value>classpath:spring-mvc.xml</param-value>
? </init-param>
? </servlet>
? <servlet-mapping>
? <servlet-name>DispatcherServlet</servlet-name>
? <url-pattern>*.action</url-pattern>
? </servlet-mapping>
? <!-- 配置spring容器加載的監(jiān)聽器 -->
? <listener>
? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <context-param>
? <param-name>contextConfigLocation</param-name>
? <param-value>classpath:spring-*.xml</param-value>
? </context-param>

以上是我的web.xml的部分配置。在項目啟動過程中,加載spring-mvc.xml是使用DispatcherServlet加載的,而加載spring-service.xml與spring-dao.xml使用的是ContextLoaderListener。

然后我們需要知道的是,ContextLoaderListener是早于DispatcherServlet啟動的,而在ContextLoaderListener加載service層配置時controller并沒有加載到容器中,但是此時事務(wù)的動態(tài)代理已經(jīng)切入到了service層,所以后續(xù)的controller層并沒有被增強。

因此,我們需要在controller層再次加入 <tx:annotation-driven/>。

僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決shiro 定時監(jiān)聽器不生效的問題 onExpiration不調(diào)用問題

    解決shiro 定時監(jiān)聽器不生效的問題 onExpiration不調(diào)用問題

    這篇文章主要介紹了解決shiro 定時監(jiān)聽器不生效的問題 onExpiration不調(diào)用問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot集成JWT生成token及校驗方法過程解析

    SpringBoot集成JWT生成token及校驗方法過程解析

    這篇文章主要介紹了SpringBoot集成JWT生成token及校驗方法過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • 一篇文章讓你徹底了解Java可重入鎖和不可重入鎖

    一篇文章讓你徹底了解Java可重入鎖和不可重入鎖

    最近正在閱讀Java ReentrantLock源碼,始終對可重入和不可重入概念理解不透徹,今天特地整理了本篇文章,讓你徹底了解Java可重入鎖和不可重入鎖,需要的朋友可以參考下
    2021-06-06
  • Java中FileOutputStream類的使用

    Java中FileOutputStream類的使用

    java.io.FileOutputStream類是文件輸出流,用于將數(shù)據(jù)寫出到文件,下面就來介紹一下Java中FileOutputStream類的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • logback自定義json日志輸出示例詳解

    logback自定義json日志輸出示例詳解

    這篇文章主要為大家介紹了logback自定義json日志輸出,就是通過logback日志體系以及l(fā)ogstash提供的json?log依賴將數(shù)據(jù)以json格式記錄到日志文件的例子
    2022-03-03
  • Spring?MVC文件請求處理MultipartResolver詳解

    Spring?MVC文件請求處理MultipartResolver詳解

    這篇文章主要介紹了Spring?MVC文件請求處理詳解:MultipartResolver,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • Java利用InputStream類實現(xiàn)文件讀取與處理

    Java利用InputStream類實現(xiàn)文件讀取與處理

    在Java開發(fā)中,輸入流(InputStream)是一個非常重要的概念,它涉及到文件讀寫、網(wǎng)絡(luò)傳輸?shù)榷鄠€方面,InputStream類是Java中輸入流的抽象基類,定義了讀取輸入流數(shù)據(jù)的方法,本文將以InputStream類為切入點,介紹Java中的輸入流概念及其應(yīng)用,需要的朋友可以參考下
    2023-11-11
  • java去除數(shù)組重復(fù)元素的四種方法

    java去除數(shù)組重復(fù)元素的四種方法

    本文給大家分享四種java去除數(shù)組重復(fù)元素的方法,每種方法通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-11-11
  • Java并發(fā)編程之ReentrantLock解析

    Java并發(fā)編程之ReentrantLock解析

    這篇文章主要介紹了Java并發(fā)編程之ReentrantLock解析,ReentrantLock內(nèi)容定義了一個抽象類Sync,繼承自AQS,而不是自己去繼承AQS,所有對ReentrantLock的操作都會轉(zhuǎn)化為對Sync的操作,需要的朋友可以參考下
    2023-12-12
  • 詳解Eclipse Validating緩慢的優(yōu)化

    詳解Eclipse Validating緩慢的優(yōu)化

    這篇文章主要介紹了詳解Eclipse Validating緩慢的優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論

海原县| 敖汉旗| 宁国市| 若羌县| 铜川市| 灵台县| 张家港市| 镇远县| 鹿泉市| 临朐县| 绿春县| 怀柔区| 怀远县| 潍坊市| 惠安县| 双牌县| 铁力市| 永和县| 天镇县| 红原县| 海门市| 洪洞县| 威远县| 沽源县| 宁蒗| 酒泉市| 庆安县| 富民县| 财经| 杂多县| 镇雄县| 车致| 兰溪市| 十堰市| 罗甸县| 滦南县| 丹阳市| 霞浦县| 余干县| 高安市| 义马市|