spring基于XML的声明式事务控制-配置步骤
生活随笔
收集整理的這篇文章主要介紹了
spring基于XML的声明式事务控制-配置步骤
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day04_learn_05tx_xml</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.45</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies></project>
package com.learn.dao;import com.learn.domain.Account;/*** 賬戶的持久層接口*/
public interface IAccountDao {/*** 根據(jù)Id查詢賬戶* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 根據(jù)名稱查詢賬戶* @param accountName* @return*/Account findAccountByName(String accountName);/*** 更新賬戶* @param account*/void updateAccount(Account account);
}
package com.learn.dao.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;/*** 賬戶的持久層實(shí)現(xiàn)類*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {public Account findAccountById(Integer accountId) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);return accounts.isEmpty()?null:accounts.get(0);}public Account findAccountByName(String accountName) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);if(accounts.isEmpty()){return null;}if(accounts.size()>1){throw new RuntimeException("結(jié)果集不唯一");}return accounts.get(0);}public void updateAccount(Account account) {super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}
}
package com.learn.domain;import java.io.Serializable;/*** 賬戶的實(shí)體類*/
public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
package com.learn.service;import com.learn.domain.Account;/*** 賬戶的業(yè)務(wù)層接口*/
public interface IAccountService {/*** 根據(jù)id查詢賬戶信息* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 轉(zhuǎn)賬* @param sourceName 轉(zhuǎn)成賬戶名稱* @param targetName 轉(zhuǎn)入賬戶名稱* @param money 轉(zhuǎn)賬金額*/void transfer(String sourceName, String targetName, Float money);
}
package com.learn.service.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import com.learn.service.IAccountService;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類** 事務(wù)控制應(yīng)該都是在業(yè)務(wù)層*/
public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}public void transfer(String sourceName, String targetName, Float money) {System.out.println("transfer....");//2.1根據(jù)名稱查詢轉(zhuǎn)出賬戶Account source = accountDao.findAccountByName(sourceName);//2.2根據(jù)名稱查詢轉(zhuǎn)入賬戶Account target = accountDao.findAccountByName(targetName);//2.3轉(zhuǎn)出賬戶減錢source.setMoney(source.getMoney()-money);//2.4轉(zhuǎn)入賬戶加錢target.setMoney(target.getMoney()+money);//2.5更新轉(zhuǎn)出賬戶accountDao.updateAccount(source);int i=1/0;//2.6更新轉(zhuǎn)入賬戶accountDao.updateAccount(target);}
}
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置業(yè)務(wù)層--><bean id="accountService" class="com.learn.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- 配置賬戶的持久層--><bean id="accountDao" class="com.learn.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置數(shù)據(jù)源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!-- spring中基于XML的聲明式事務(wù)控制配置步驟1、配置事務(wù)管理器2、配置事務(wù)的通知此時(shí)我們需要導(dǎo)入事務(wù)的約束 tx名稱空間和約束,同時(shí)也需要aop的使用tx:advice標(biāo)簽配置事務(wù)通知屬性:id:給事務(wù)通知起一個(gè)唯一標(biāo)識(shí)transaction-manager:給事務(wù)通知提供一個(gè)事務(wù)管理器引用3、配置AOP中的通用切入點(diǎn)表達(dá)式4、建立事務(wù)通知和切入點(diǎn)表達(dá)式的對應(yīng)關(guān)系5、配置事務(wù)的屬性是在事務(wù)的通知tx:advice標(biāo)簽的內(nèi)部--><!-- 配置事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事務(wù)的通知--><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 配置事務(wù)的屬性isolation:用于指定事務(wù)的隔離級(jí)別。默認(rèn)值是DEFAULT,表示使用數(shù)據(jù)庫的默認(rèn)隔離級(jí)別。propagation:用于指定事務(wù)的傳播行為。默認(rèn)值是REQUIRED,表示一定會(huì)有事務(wù),增刪改的選擇。查詢方法可以選擇SUPPORTS。read-only:用于指定事務(wù)是否只讀。只有查詢方法才能設(shè)置為true。默認(rèn)值是false,表示讀寫。timeout:用于指定事務(wù)的超時(shí)時(shí)間,默認(rèn)值是-1,表示永不超時(shí)。如果指定了數(shù)值,以秒為單位。rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)回滾,產(chǎn)生其他異常時(shí),事務(wù)不回滾。沒有默認(rèn)值。表示任何異常都回滾。no-rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)不回滾,產(chǎn)生其他異常時(shí)事務(wù)回滾。沒有默認(rèn)值。表示任何異常都回滾。--><tx:attributes><tx:method name="*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method></tx:attributes></tx:advice><!-- 配置aop--><aop:config><!-- 配置切入點(diǎn)表達(dá)式--><aop:pointcut id="pt1" expression="execution(* com.learn.service.impl.*.*(..))"></aop:pointcut><!--建立切入點(diǎn)表達(dá)式和事務(wù)通知的對應(yīng)關(guān)系 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor></aop:config>
</beans>
package com.learn.test;import com.learn.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 使用Junit單元測試:測試我們的配置*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {@Autowiredprivate IAccountService as;@Testpublic void testTransfer(){as.transfer("aaa","bbb",100f);}}
?
總結(jié)
以上是生活随笔為你收集整理的spring基于XML的声明式事务控制-配置步骤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring中事务控制的一组API
- 下一篇: 模版设计模式概述和使用