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

歡迎訪問 生活随笔!

生活随笔

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

javascript

JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解

發布時間:2023/12/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<一> 配置為文件

<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" ><!-- 配置 C3P0 連接池數據源, 作為 DAO 層的數據源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl"><value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</value></property><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="user" value="root"></property><property name="password" value="920619"></property></bean><!-- 配置 DAO 層, 把 C3P0 連接池作為數據源注入到 DAO 層 --><bean id="accountDao" class="spring.things_2.AccountDao"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置 SERVICE 層, 把 DAO 層注入到 SERVICE 層 --><bean id="accountService" class="spring.things_2.AccountService"><property name="accountDao" ref="accountDao"></property></bean><!-- 配置事物管理類 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 開啟注解事物 --><tx:annotation-driven transaction-manager="transactionManager" /></beans>

?

<二> Dao 層和 Service?層的接口

package spring.things_2;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDao extends JdbcDaoSupport implements IAccountDao {@Overridepublic void outMoney(String outName, double money) {String sql = "update User set money = money - ? where UserName = ?";this.getJdbcTemplate().update(sql, money, outName);}@Overridepublic void inMoney(String inName, double money) {String sql = "update User set money = money + ? where UserName = ?";this.getJdbcTemplate().update(sql, money, inName);}} package spring.things_2;import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;/*** 注解該類為事物類( 參數類型如下 )* propagation: 事物的傳播行為* isolation: 事物的隔離級別* readOnly: 是否只讀* rollbackFor: 被回滾的異常類型* noRollbackFor: 不會滾的異常類型*/ @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT) public class AccountService implements IAccountService {// 注入的 Dao 層private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}// 這個方法在測試中是一個事物單元 @Overridepublic void transfer(String outName, String inName, double money) {accountDao.outMoney(outName, money);accountDao.inMoney(inName, money);}}

< 三 > 接口的實現類

package spring.things_2;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDao extends JdbcDaoSupport implements IAccountDao {@Overridepublic void outMoney(String outName, double money) {String sql = "update User set money = money - ? where UserName = ?";this.getJdbcTemplate().update(sql, money, outName);}@Overridepublic void inMoney(String inName, double money) {String sql = "update User set money = money + ? where UserName = ?";this.getJdbcTemplate().update(sql, money, inName);}} package spring.things_2;import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;/*** 注解該類為事物類( 參數類型如下 )* propagation: 事物的傳播行為* isolation: 事物的隔離級別* readOnly: 是否只讀* rollbackFor: 被回滾的異常類型* noRollbackFor: 不會滾的異常類型*/ @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT) public class AccountService implements IAccountService {// 注入的 Dao 層private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}// 這個方法在測試中是一個事物單元 @Overridepublic void transfer(String outName, String inName, double money) {accountDao.outMoney(outName, money);accountDao.inMoney(inName, money);}}

?

轉載于:https://www.cnblogs.com/lovling/p/6817282.html

總結

以上是生活随笔為你收集整理的JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解的全部內容,希望文章能夠幫你解決所遇到的問題。

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