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

Java Spring 聲明式事務(wù)詳解

 更新時間:2021年09月23日 11:25:40   作者:philpy_used  
這篇文章主要介紹了spring 聲明式事務(wù)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

項目結(jié)構(gòu):

在這里插入圖片描述

表結(jié)構(gòu):

在這里插入圖片描述

在這里插入圖片描述

基于xml的聲明式事務(wù)配置

IAccountDao.java:

package tx.dao;
import java.math.BigDecimal;
public interface IAccountDao {
    void add(String name, BigDecimal money);
    void sub(String name, BigDecimal money);
}

AccountDaoImpl.java:

package tx.service.impl;
import tx.dao.IAccountDao;
import tx.service.IAccountService;
import java.math.BigDecimal;
public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao;
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void tran(String from, String to, BigDecimal money) {
        accountDao.sub(from, money);
        accountDao.add(to, money);
    }
}

IAccountService.java:

package tx.service;
import java.math.BigDecimal;
public interface IAccountService {
    void tran(String from, String to, BigDecimal money);
}

AccountDaoImpl.java:

package tx.dao.impl;
import org.springframework.jdbc.core.JdbcTemplate;
import tx.dao.IAccountDao;
import java.math.BigDecimal;
public class AccountDaoImpl implements IAccountDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public void add(String name, BigDecimal money) {
        jdbcTemplate.update("update account set balance = balance + ? where name = ? ", money.toString(), name);
    }
    @Override
    public void sub(String name, BigDecimal money) {
        jdbcTemplate.update("update account set balance = balance - ? where name = ? ", money.toString(), name);
    }
}
<?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
       ">
    <!--配置數(shù)據(jù)源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="19834044876"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    </bean>
    <!--創(chuàng)建事務(wù)管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置jdbcTemplate對象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--將JdbcTemplate注入到AccountDao中-->
    <bean id="accountDao" class="tx.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!--將AccountDao注入到AccountService中-->
    <bean id="accountService" class="tx.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--配置事務(wù)通知-->
    <tx:advice id="txAdvice">
        <!--配置事務(wù)參數(shù)-->
        <tx:attributes>
            <!--指定哪些方法上面添加事務(wù)-->
            <tx:method name="tran"/>  <!-- name="*", name="tran*", name="*tran", ... -->
        </tx:attributes>
    </tx:advice>
    <!--配置切入點和切面-->
    <aop:config>
        <!--配置切入點-->
        <aop:pointcut id="pointCut" expression="execution(* tx.service.IAccountService.*(..))"/>
        <!--配置通知-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
    </aop:config>
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext("tx.xml");
IAccountService accountService = context.getBean("accountService", IAccountService.class);
accountService.tran("小明", "小紅", new BigDecimal(500));

完全注解(零xml)方式配置

IAccountDao.java:

package tx.dao;
import java.math.BigDecimal;
public interface IAccountDao {
    void add(String name, BigDecimal money);
    void sub(String name, BigDecimal money);
}

AccountDaoImpl.java:

package tx.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import tx.dao.IAccountDao;
import java.math.BigDecimal;
@Repository
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void add(String name, BigDecimal money) {
        jdbcTemplate.update("update account set balance = balance + ? where name = ? ", money.toString(), name);
    }
    @Override
    public void sub(String name, BigDecimal money) {
        jdbcTemplate.update("update account set balance = balance - ? where name = ? ", money.toString(), name);
    }
}

IAccountService.java:

package tx.service;
import java.math.BigDecimal;
public interface IAccountService {
    void tran(String from, String to, BigDecimal money);
}

AccountServiceImpl.java:

package tx.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tx.dao.IAccountDao;
import tx.service.IAccountService;
import java.math.BigDecimal;
@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;
    @Override
    public void tran(String from, String to, BigDecimal money) {
        accountDao.sub(from, money);
        accountDao.add(to, money);
    }
}

TXConfig.java

package tx.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tx.service.IAccountService;
import tx.service.impl.AccountServiceImpl;
import javax.sql.DataSource;
@Configuration
@ComponentScan(basePackages = "tx")
@EnableTransactionManagement
public class TXConfig {
    /**
     * 配置數(shù)據(jù)源
     */
    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("19834044876");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }
    /**
     * 創(chuàng)建事務(wù)管理器
     */
    @Bean
    public DataSourceTransactionManager getTransactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }
    /**
     * 配置jdbcTemplate對象
     */
    @Bean
    public JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(getDataSource());
    }
    @Bean(name = "accountService")
    public IAccountService getAccountService() {
        return new AccountServiceImpl();
    }
}
ApplicationContext context = new AnnotationConfigApplicationContext(TXConfig.class);
IAccountService accountService = context.getBean("accountService", IAccountService.class);
accountService.tran("小明", "小紅", new BigDecimal(500));

事務(wù)參數(shù)

在這里插入圖片描述

在這里插入圖片描述

no-rollback-for

指定碰到哪些異常不需要回滾

rollback-for

指定碰到哪些異常需要回滾

read-only

設(shè)置事務(wù)為只讀事務(wù)

timeout

以秒為單位,設(shè)置事務(wù)超出指定時常后自動回滾

默認(rèn)為-1,即不管事務(wù)運行多久都不回滾

isolation

事務(wù)的隔離級別

在這里插入圖片描述

在這里插入圖片描述

默認(rèn)為DEFAULT,即使用當(dāng)前數(shù)據(jù)庫的隔離級別

在這里插入圖片描述

propagation

事務(wù)的傳播行為

在這里插入圖片描述

在這里插入圖片描述

默認(rèn)為REQUIRED

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Java微信小程序醫(yī)院掛號系統(tǒng)

    Java微信小程序醫(yī)院掛號系統(tǒng)

    這篇文章主要介紹了基于Java微信小程序醫(yī)院掛號系統(tǒng),可以實現(xiàn)遠(yuǎn)程處理事務(wù),遠(yuǎn)程提交工作和隨時追蹤工作的狀態(tài),文中提供了解決思路和部分實現(xiàn)代碼,需要的朋友可以參考下
    2023-03-03
  • Java String類正則操作示例

    Java String類正則操作示例

    這篇文章主要介紹了Java String類正則操作,結(jié)合實例形式分析了java針對數(shù)字的正則驗證、過濾及郵箱正則驗證相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • springboot+dynamicDataSource動態(tài)添加切換數(shù)據(jù)源方式

    springboot+dynamicDataSource動態(tài)添加切換數(shù)據(jù)源方式

    這篇文章主要介紹了springboot+dynamicDataSource動態(tài)添加切換數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 一篇文章帶你解決 IDEA 每次新建項目 maven home directory 總是改變的問題

    一篇文章帶你解決 IDEA 每次新建項目 maven home directory 總是改變的問題

    這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項目 maven home directory 總是改變的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java二維數(shù)組與動態(tài)數(shù)組ArrayList類詳解

    Java二維數(shù)組與動態(tài)數(shù)組ArrayList類詳解

    這篇文章主要給大家介紹了關(guān)于Java二維數(shù)組與動態(tài)數(shù)組ArrayList類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java?GUI編程菜單組件實例詳解

    Java?GUI編程菜單組件實例詳解

    在實際開發(fā)中,除了主界面,還有一類比較重要的內(nèi)容就是菜單相關(guān)組件,可以通過菜單相關(guān)組件很方便的使用特定的功能,今天通過本文給大家介紹下Java?GUI編程菜單組件實例詳解,感興趣的朋友一起看看吧
    2022-04-04
  • JavaAPI的使用方法詳解

    JavaAPI的使用方法詳解

    這篇文章主要介紹了JavaAPI的使用方法詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • java數(shù)據(jù)結(jié)構(gòu)和算法中數(shù)組的簡單入門

    java數(shù)據(jù)結(jié)構(gòu)和算法中數(shù)組的簡單入門

    在本文里小編給大家整理了關(guān)于java數(shù)據(jù)結(jié)構(gòu)和算法中數(shù)組的簡單入門知識點整理,需要的朋友們學(xué)習(xí)下。
    2019-06-06
  • Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用

    Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用

    這篇文章主要介紹了Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 淺談Synchronized和Lock的區(qū)別

    淺談Synchronized和Lock的區(qū)別

    這篇文章主要介紹了淺談Synchronized和Lock的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評論

甘洛县| 巴里| 道真| 浦县| 若尔盖县| 丰台区| 蒙山县| 安溪县| 盘锦市| 徐州市| 龙海市| 蕉岭县| 郎溪县| 重庆市| 沂南县| 惠来县| 十堰市| 石台县| 穆棱市| 塘沽区| 永寿县| 镇平县| 长武县| 且末县| 酉阳| 镇安县| 盐边县| 团风县| 固安县| 万源市| 桦南县| 随州市| 西安市| 房产| 嵊泗县| 揭阳市| 丹巴县| 佛山市| 绥芬河市| 屏山县| 陵水|