當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解
生活随笔
收集整理的這篇文章主要介紹了
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&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 注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 包商银行信用卡提现利息多少?怎么计算?
- 下一篇: 利用CSS、JavaScript及Aja