日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring的事务控制-基于xml方式

發布時間:2024/10/5 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring的事务控制-基于xml方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹:該程序模擬了轉賬操作,即Jone減少500元,tom增加500元

1.導入坐標

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.9.RELEASE</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency>

2.創建Account實體類

public class Account {private String name;private String money;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;} }

3.創建AccountDao接口以及實現類AccountDaoImpl;

public interface AccountDao {public void out(String outMan,double money);public void in(String inMan,double money); } public class AccountDaoImpl implements AccountDao {private JdbcTemplate template;public void setTemplate(JdbcTemplate template) {this.template = template;}@Overridepublic void out(String outMan, double money) {template.update("update account set money=money-? where name=?",money,outMan);}@Overridepublic void in(String inMan, double money) {template.update("update account set money=money+? where name=?",money,inMan);} }

4.創建AccountService接口以及AccountServiceImpl實現類

public interface AccountService {public void transfer(String outMan,String inMan,double money); } public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void transfer(String outMan, String inMan,double money) {accountDao.out(outMan,money);accountDao.in(inMan,money);}}

5.編寫spring配置文件(applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--將AccountDao注入到spring容器--><bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl"><property name="template" ref="jdbcTemplate"/></bean> <!--將AccountService注入到spring容器--><bean id="accountService" class="com.hao.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean> <!-- 配置數據源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.cj.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/><property name="user" value="root"/><property name="password" value="hao20001010"/></bean> <!-- 將模板對象注入到spring容器--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean> </beans>

6.數據庫表的創建

7.測試

public class AccountController {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");AccountService service= context.getBean(AccountService.class);service.transfer("Jone","tom",500);} }

結果:


將Jone和tom的錢修改為5000元

當我們在業務方法transfer中手動加入錯誤代碼,讓其報錯

再此運行,我們發現控制臺報錯,接著查看數據庫中的數據

出現這樣的原因就是,當程序執行完accountDao.out(outMan,money)時,Jone的錢減少500,但是當執行下一步int i=1/0時,程序出現錯誤,然后就不再執行下一步功能了,所有出現了這樣的情況;

這樣的問題該怎么解決呢?
利用aop思想,將事務提取出來交給spring管理,在程序運行時,與方法進行切入即可;這樣可以保證如果程序運行時出現錯誤,則雙方的金額都不會改變

<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--將AccountDao注入到spring容器--><bean id="accountDao" class="com.hao.dao.impl.AccountDaoImpl"><property name="template" ref="jdbcTemplate"/></bean> <!--將AccountService注入到spring容器,目標對象,內部的方法就是切點--><bean id="accountService" class="com.hao.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean><!-- 配置平臺事務管理器,當前的DataSourceTransactionManager是jdbc、mybatis技術,如果以后使用了其他技術,則此處需要修改--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean> <!-- 通知,事務的增強,引入事務的命名空間tx--><tx:advice id="tx" transaction-manager="transactionManager"><!--設置屬性信息的 --><tx:attributes><tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/><tx:method name="*"/></tx:attributes></tx:advice> <!-- 配置aop織入,advice-ref引入通知(唯一標識id) pointcut(切入的方法)--><aop:config><aop:advisor advice-ref="tx" pointcut="execution(* com.hao.service.impl.*.*(..))"/></aop:config><!-- 配置數據源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.cj.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/><property name="user" value="root"/><property name="password" value="hao20001010"/></bean> <!-- 將模板對象注入到spring容器--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean> </beans>

將金額修改為5000元
再次運行:

===================================================

<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>

1.name:切入點方法名稱
2.isolation:事務的隔離級別
3.propogation:事務的傳播行為
4.timeout:超時時間
5.read-only:是否只讀

總結

以上是生活随笔為你收集整理的Spring的事务控制-基于xml方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。