Java Spring 聲明式事務(wù)詳解
項(xiàng)目結(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對(duì)象-->
<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>
<!--配置切入點(diǎn)和切面-->
<aop:config>
<!--配置切入點(diǎn)-->
<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對(duì)象
*/
@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ù)超出指定時(shí)常后自動(dòng)回滾
默認(rèn)為-1,即不管事務(wù)運(yùn)行多久都不回滾
isolation
事務(wù)的隔離級(jí)別


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

propagation
事務(wù)的傳播行為


默認(rèn)為REQUIRED

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java微信小程序醫(yī)院掛號(hào)系統(tǒng)
這篇文章主要介紹了基于Java微信小程序醫(yī)院掛號(hào)系統(tǒng),可以實(shí)現(xiàn)遠(yuǎn)程處理事務(wù),遠(yuǎn)程提交工作和隨時(shí)追蹤工作的狀態(tài),文中提供了解決思路和部分實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-03-03
springboot+dynamicDataSource動(dòng)態(tài)添加切換數(shù)據(jù)源方式
這篇文章主要介紹了springboot+dynamicDataSource動(dòng)態(tài)添加切換數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問(wèn)題
這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java二維數(shù)組與動(dòng)態(tài)數(shù)組ArrayList類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于Java二維數(shù)組與動(dòng)態(tài)數(shù)組ArrayList類(lèi)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
java數(shù)據(jù)結(jié)構(gòu)和算法中數(shù)組的簡(jiǎn)單入門(mén)
在本文里小編給大家整理了關(guān)于java數(shù)據(jù)結(jié)構(gòu)和算法中數(shù)組的簡(jiǎn)單入門(mén)知識(shí)點(diǎn)整理,需要的朋友們學(xué)習(xí)下。2019-06-06
Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用
這篇文章主要介紹了Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

