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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用aop解决事务问题(xml版)

發(fā)布時(shí)間:2023/12/3 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用aop解决事务问题(xml版) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、引入依賴


pom.xml代碼:

<?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.william</groupId><artifactId>spring_day04_01_tx_aop_xml</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</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>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency></dependencies></project>

二、目錄結(jié)構(gòu)

三、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"xmlns:context="http://www.springframework.org/schema/context"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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--開啟注解掃描--><context:component-scan base-package="com.william"></context:component-scan><!--創(chuàng)建QueryRunner--><bean id="QueryRunner" class="org.apache.commons.dbutils.QueryRunner"></bean><!--創(chuàng)建dataSource--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--引入屬性文件--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--使用aop解決事務(wù)問題--><!--配置切面:切入點(diǎn),增強(qiáng)--><!--增強(qiáng):事務(wù)管理--><bean id="transactionManager" class="com.william.utils.TransactionManager"></bean><!--aop配置--><aop:config><!--配置切面: ref 關(guān)聯(lián)增強(qiáng)對(duì)象--><aop:aspect ref="transactionManager"><!--配置切入點(diǎn)--><aop:pointcut id="pc" expression="execution(* com.william.service.Impl.*.*(..))"></aop:pointcut><!--織入--><aop:before method="beganTransaction" pointcut-ref="pc"></aop:before><aop:after-returning method="Commit" pointcut-ref="pc"></aop:after-returning><aop:after-throwing method="rollBack" pointcut-ref="pc"></aop:after-throwing><aop:after method="release" pointcut-ref="pc"></aop:after></aop:aspect></aop:config> </beans>

四、Class文件

1.AccountDaoImpl

代碼:

package com.william.Dao.Impl;import com.william.Dao.AccountDao; import com.william.domain.Account; import com.william.utils.ConnectionUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;import java.sql.SQLException; import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:00* @description :* @version: 1.0*/ @Repository public class AccountDaoImpl implements AccountDao {@AutowiredConnectionUtils connectionUtils;@AutowiredQueryRunner queryRunner;@Overridepublic Account findByUserName(String username ) {String sql="select * from account where name = ?";try {return queryRunner.query(connectionUtils.getThreadConnection(),sql,new BeanHandler<>(Account.class),username);} catch (SQLException e) {e.printStackTrace();}return null;}@Overridepublic void Update(Account account) {String sql =" update account set money = ? where id =?";Object [] params= {account.getMoney(),account.getId()};try {queryRunner.update(connectionUtils.getThreadConnection(),sql,params);} catch (SQLException e) {e.printStackTrace();}} }

2.AccountServiceImpl

package com.william.service.Impl;import com.william.Dao.AccountDao; import com.william.domain.Account; import com.william.service.AccountService; import com.william.utils.TransactionManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:14* @description :* @version: 1.0*/ @Service public class AccountServiceImpl implements AccountService {@AutowiredAccountDao accountDao;@Overridepublic void transfer(String FromMoney, String ToMoney, Float money) {//開啟事務(wù):transactionManager//查詢賬戶名稱 兩個(gè)賬戶名稱Account FromuserName = accountDao.findByUserName(FromMoney);Account ToUserName = accountDao.findByUserName(ToMoney);//修改金額FromuserName.setMoney(FromuserName.getMoney()-money);ToUserName.setMoney(ToUserName.getMoney()+money);//int i=1/0;//修改賬戶表accountDao.Update(FromuserName);accountDao.Update(ToUserName);} }

3.Utils工具類

(1)ConnectionUtils

package com.william.utils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component public class ConnectionUtils {private ThreadLocal<Connection> t1=new ThreadLocal();@AutowiredDataSource dataSource;/*** 用來獲取線程中連接對(duì)象* 1, 在第一次訪問該方法時(shí), 線程中沒有對(duì)象,先獲取一個(gè),放入線程*/public Connection getThreadConnection(){//獲取線程池中的對(duì)象Connection conn = t1.get();if (conn==null){try {conn = dataSource.getConnection();t1.set(conn);} catch (SQLException e) {e.printStackTrace();}}return conn;}/*** 移除線程*/public void remove(){t1.remove();} }

(2)TransactionManager

package com.william.utils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component public class TransactionManager {@AutowiredConnectionUtils connectionUtils;//開啟事務(wù)public void beganTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}//提交事務(wù)public void Commit(){try {connectionUtils.getThreadConnection().commit();} catch (SQLException e) {e.printStackTrace();}}//回滾public void rollBack(){try {connectionUtils.getThreadConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}//釋放資源public void release(){try {connectionUtils.getThreadConnection().setAutoCommit(true);connectionUtils.getThreadConnection().close();connectionUtils.remove();} catch (SQLException e) {e.printStackTrace();}} }

4.TestTrasfer測(cè)試類

package com.william;import com.william.service.AccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:22* @description :* @version: 1.0*/ @Component @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestTrasfer {@AutowiredAccountService accountService;@Testpublic void testTrasferMoney(){accountService.transfer("william","william-Li",500f);} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的使用aop解决事务问题(xml版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。