欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Spring 聲明式事務詳解

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

項目結構:

在這里插入圖片描述

表結構:

在這里插入圖片描述

在這里插入圖片描述

基于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.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)建事務管理器-->
    <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>
    <!--配置事務通知-->
    <tx:advice id="txAdvice">
        <!--配置事務參數(shù)-->
        <tx:attributes>
            <!--指定哪些方法上面添加事務-->
            <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)建事務管理器
     */
    @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));

事務參數(shù)

在這里插入圖片描述

在這里插入圖片描述

no-rollback-for

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

rollback-for

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

read-only

設置事務為只讀事務

timeout

以秒為單位,設置事務超出指定時常后自動回滾

默認為-1,即不管事務運行多久都不回滾

isolation

事務的隔離級別

在這里插入圖片描述

在這里插入圖片描述

默認為DEFAULT,即使用當前數(shù)據(jù)庫的隔離級別

在這里插入圖片描述

propagation

事務的傳播行為

在這里插入圖片描述

在這里插入圖片描述

默認為REQUIRED

在這里插入圖片描述

總結

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

相關文章

  • SpringBoot中優(yōu)化Undertow性能的方法總結

    SpringBoot中優(yōu)化Undertow性能的方法總結

    Undertow是一個采用 Java 開發(fā)的靈活的高性能Web服務器,提供包括阻塞和基于NIO的非堵塞機制,本文將給大家介紹SpringBoot中優(yōu)化Undertow性能的方法,文中有相關的代碼示例供大家參考,需要的朋友可以參考下
    2024-08-08
  • SpringBoot使用Filters實現(xiàn)請求過濾和預處理

    SpringBoot使用Filters實現(xiàn)請求過濾和預處理

    過濾器(Filter)是一種在Web應用中用于攔截和處理HTTP請求和響應的對象,在Java Web開發(fā)中,過濾器是實現(xiàn)特定功能,如認證、日志記錄和字符編碼處理的重要工具,本文主要介紹了SpringBoot使用Filters實現(xiàn)請求過濾和預處理,需要的朋友可以參考下
    2024-08-08
  • java之swing實現(xiàn)復選框的方法

    java之swing實現(xiàn)復選框的方法

    這篇文章主要介紹了java之swing實現(xiàn)復選框的方法,實例分析了java基于圖形界面復選框的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • 解決org.apache.ibatis.binding.BindingException:?Invalid?bound?statement?(not?found)問題(最新推薦)

    解決org.apache.ibatis.binding.BindingException:?Invalid?boun

    這篇文章主要介紹了解決org.apache.ibatis.binding.BindingException:?Invalid?bound?statement?(not?found)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • IDEA一致卡在build時間過長問題解決

    IDEA一致卡在build時間過長問題解決

    有很多小伙伴在起項目的時候巨慢,特別影響開發(fā)效率,本文主要介紹了IDEA一致卡在build時間過長問題解決,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • myBatis組件教程之緩存的實現(xiàn)與使用

    myBatis組件教程之緩存的實現(xiàn)與使用

    這篇文章主要給大家介紹了關于myBatis組件教程之緩存的實現(xiàn)與使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • Java執(zhí)行hadoop的基本操作實例代碼

    Java執(zhí)行hadoop的基本操作實例代碼

    這篇文章主要介紹了Java執(zhí)行hadoop的基本操作實例代碼的相關資料,需要的朋友可以參考下
    2017-04-04
  • mybatisplus解除分頁限制的實現(xiàn)

    mybatisplus解除分頁限制的實現(xiàn)

    這篇文章主要介紹了mybatisplus解除分頁限制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • IDEA 2020代碼提示忽略大小寫的問題

    IDEA 2020代碼提示忽略大小寫的問題

    這篇文章主要介紹了IDEA 2020代碼提示忽略大小寫的問題,本文通過圖文并茂的形式給大家分享解決方法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot服務上實現(xiàn)接口限流的方法

    SpringBoot服務上實現(xiàn)接口限流的方法

    這篇文章主要介紹了SpringBoot服務上實現(xiàn)接口限流的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10

最新評論