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

Spring的事務(wù)控制實(shí)現(xiàn)方法

 更新時(shí)間:2022年07月29日 09:10:51   作者:興奮の大公猴  
這篇文章主要為大家詳細(xì)介紹了Spring的事務(wù)控制實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spring的事務(wù)控制實(shí)現(xiàn),供大家參考,具體內(nèi)容如下

提示:這里使用的是xml的方式配置事務(wù)的

前言

例:當(dāng)銀行轉(zhuǎn)賬的時(shí)候,如果轉(zhuǎn)賬和收款的一方出現(xiàn)問題,那么這次的轉(zhuǎn)賬則不成功,此處如果沒有事務(wù)管理,那么可能出現(xiàn)一方已經(jīng)轉(zhuǎn)賬成功,另一方卻沒有收款的問題。為了避免此問題,應(yīng)當(dāng)使用到事務(wù)管理。

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、Spring聲明式事務(wù)控制

示例:

二、使用步驟

1.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:aop="http://www.springframework.org/schema/aop"
? ? ? ?xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? ?xmlns:context="http://www.springframework.org/schema/context"
? ? ? ?xsi:schemaLocation="
? ? ? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ?http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
? ? ? ?http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
? ? ? ?http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

? ? <!-- ?1.加載jdbc.properties ?-->
? ? <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

? ? <!-- ?2.配置數(shù)據(jù)源對象 ?-->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? <property name="driverClassName" value="${driver}"></property>
? ? ? ? <property name="url" value="${url}"></property>
? ? ? ? <property name="username" value="${user}"></property>
? ? ? ? <property name="password" value="${password}"></property>
? ? </bean>

? ? <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>

? ? <bean id="accountDao" class="com.codejams.dao.impl.AccountDaoImpl">
? ? ? ? <property name="jdbcTemplate" ref="jdbcTemplate"/>
? ? </bean>

<!-- ? ?<bean class="com.codejams.controller.DemoTest">-->
<!-- ? ? ? ?<property name="accountService" ref="accountService"/>-->
<!-- ? ?</bean>-->

? ? <!--目標(biāo)對象 ?內(nèi)部的方法就是切點(diǎn)-->
? ? <bean id="accountService" class="com.codejams.service.impl.AccountServiceImpl">
? ? ? ? <property name="accountDao" ref="accountDao"/>
? ? </bean>

? ? <!--配置平臺事務(wù)管理器-->
? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>

? ? <!--通知 ?事務(wù)的增強(qiáng)-->
? ? <tx:advice id="txAdvice" transaction-manager="transactionManager">
? ? ? ? <!--設(shè)置事務(wù)的屬性信息的-->
? ? ? ? <tx:attributes>
? ? ? ? ? ? <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
? ? ? ? ? ? <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
? ? ? ? ? ? <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
? ? ? ? ? ? <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
? ? ? ? ? ? <tx:method name="*"/>
? ? ? ? </tx:attributes>
? ? </tx:advice>

? ? <!--配置事務(wù)的aop織入-->
? ? <aop:config>
? ? ? ? <aop:pointcut id="txPointcut" expression="execution(* com.codejams.service.impl.*.*(..))"/>
? ? ? ? <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
? ? </aop:config>

</beans>

2.AccountDaoImpl代碼

代碼如下(示例):

package com.codejams.dao.impl;

import com.codejams.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {

? ? private JdbcTemplate jdbcTemplate;

? ? public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
? ? ? ? this.jdbcTemplate = jdbcTemplate;
? ? }

? ? public void out(String outMan, double money) {
? ? ? ? jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
? ? }

? ? public void in(String inMan, double money) {
? ? ? ? jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
? ? }
}

3.AccountServiceImpl代碼

代碼如下(示例):

package com.codejams.service.impl;

import com.codejams.dao.AccountDao;
import com.codejams.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;

public class AccountServiceImpl implements AccountService {

? ? //@Autowired
? ? private AccountDao accountDao;
? ? public void setAccountDao(AccountDao accountDao) {
? ? ? ? this.accountDao = accountDao;
? ? }

? ? public void transfer(String outMan, String inMan, double money) {
? ? ? ? accountDao.out(outMan,money);
? ? ? ? int i = 1/0;
? ? ? ? accountDao.in(inMan,money);
? ? }
}

結(jié)果展示

手動在這里制造一個(gè)異常

此時(shí)數(shù)據(jù)庫的狀態(tài)為兩人均為5000

執(zhí)行后,可以看見這里報(bào)了異常,并且數(shù)據(jù)庫的兩人的money都沒有更改

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA 使用mybatis插件Free Mybatis plugin的步驟(推薦)

    IDEA 使用mybatis插件Free Mybatis plugin的步驟(推薦)

    這篇文章主要介紹了IDEA 使用mybatis插件Free Mybatis plugin的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Spring中@Configuration注解和@Component注解的區(qū)別詳解

    Spring中@Configuration注解和@Component注解的區(qū)別詳解

    這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過如下一個(gè)案例,在不分析源碼的情況下,小伙伴們先來直觀感受一下這兩個(gè)之間的區(qū)別,需要的朋友可以參考下
    2023-09-09
  • 淺談JavaWeb中的web.xml配置部署描述符文件

    淺談JavaWeb中的web.xml配置部署描述符文件

    下面小編就為大家?guī)硪黄獪\談JavaWeb中的web.xml配置部署描述符文件。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • java繼承的概念及案例解析

    java繼承的概念及案例解析

    這篇文章主要介紹了java繼承的概念及案例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java對象池管理方式common-pool2使用

    java對象池管理方式common-pool2使用

    這篇文章主要為大家介紹了java對象池common-pool2使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表

    MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表

    這篇文章主要介紹了MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java網(wǎng)絡(luò)編程之UDP網(wǎng)絡(luò)通信詳解

    Java網(wǎng)絡(luò)編程之UDP網(wǎng)絡(luò)通信詳解

    這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程中的UDP網(wǎng)絡(luò)通信的原理與實(shí)現(xiàn),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-09-09
  • JAVA提高第十篇 ArrayList深入分析

    JAVA提高第十篇 ArrayList深入分析

    這篇文章主要深入分析了JAVA提高第十篇ArrayList的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java中CAS機(jī)制實(shí)現(xiàn)方法詳解

    Java中CAS機(jī)制實(shí)現(xiàn)方法詳解

    傳統(tǒng)的并發(fā)控制手段如synchronized和ReentrantLock雖有效防止資源競爭,卻可能引起性能開銷,相比之下,CAS(CompareAndSwap)提供一種輕量級的樂觀鎖策略,通過硬件級別的原子指令實(shí)現(xiàn)無鎖并發(fā),提高性能,需要的朋友可以參考下
    2024-09-09
  • 帶著新人看java虛擬機(jī)01(推薦)

    帶著新人看java虛擬機(jī)01(推薦)

    這篇文章主要介紹了java虛擬機(jī),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

张家港市| 京山县| 朝阳区| 湛江市| 邢台县| 延安市| 南阳市| 祁东县| 乌什县| 连南| 唐山市| 漳浦县| 池州市| 五河县| 那坡县| 五大连池市| 资阳市| 昭觉县| 上栗县| 天长市| 汾阳市| 锡林浩特市| 孟津县| 开鲁县| 武陟县| 东光县| 富民县| 福建省| 尤溪县| 焉耆| 洪雅县| 融水| 宁晋县| 尤溪县| 神木县| 汾阳市| 永济市| 炎陵县| 奉化市| 阿克陶县| 阜平县|