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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

转账为demo,spring事务

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转账为demo,spring事务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring 事務使用

  • 1. 業務代碼
  • 2. xml配置
  • 3. 注解配置

1. 業務代碼

  • 數據表結構
  • dao
package com.lovely.dao.impl;import com.lovely.dao.AccountDao; import org.springframework.jdbc.core.JdbcTemplate;/*** @author echo lovely* @date 2020/8/9 11:01*/ public class AccountDaoImpl implements AccountDao {private JdbcTemplate jdbcTemplate;// 提供set方法 用于依賴注入public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}// 出賬public void out(String outMan, Double money) {String sql = "update account set money = money - ? where name = ?";jdbcTemplate.update(sql, new Object[]{money, outMan});}// 進賬public void in(String inMan, Double money) {String sql = "update account set money = money + ? where name = ?";jdbcTemplate.update(sql, new Object[]{money, inMan});} }
  • service
package com.lovely.service.impl;import com.lovely.dao.AccountDao; import com.lovely.service.AccountService;/*** @author echo lovely* @date 2020/8/9 11:08*/ public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}/*** 轉賬業務具體實現 需要使用事務* @param outMan 轉出人* @param inMan 收賬人* @param money money*/public void transfer(String outMan, String inMan, Double money) {accountDao.in(inMan, money);System.out.println(1/0);accountDao.out(outMan, money);} }
  • controller
package com.lovely.controller;import com.lovely.service.AccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author echo lovely* @date 2020/8/9 11:12*/ public class AccountController {public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = app.getBean(AccountService.class);// 轉賬測試accountService.transfer("jack", "rose", 1000.0);} }
  • 上面的bean的創建方式set注入,沒搞注解

2. xml配置

  • 核心 事務管理器 對轉賬方法動態進行事務控制
<!-- 數據源事務管理器 --><bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 事務通知 增強方法 --><tx:advice id="txAdvice" transaction-manager="tranManager"><!--事務屬性信息的配置--><tx:attributes><tx:method name="*"/><!--<tx:method name="transfer*" isolation="DEFAULT" read-only="false"/>--></tx:attributes></tx:advice><!-- 配置事務的織入 --><aop:config><!--<aop:pointcut id="myPointcut" expression="execution(* com.lovely.service.impl.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>--><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lovely.service.impl.*.*(..))"/></aop:config>

3. 注解配置

  • 注解配置
  • <!-- 數據源事務管理器 --><bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 開啟事務的注解驅動 --><tx:annotation-driven transaction-manager="tranManager"></tx:annotation-driven>
  • 事務注解
  • package com.lovely.service.impl;import com.lovely.dao.AccountDao; import com.lovely.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;/*** @author echo lovely* @date 2020/8/9 11:08*/@Service("accountServiceImpl") public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;/*** 轉賬業務具體實現 需要使用事務* @param outMan 轉出人* @param inMan 收賬人* @param money money*/@Transactional(timeout = 1)public void transfer(String outMan, String inMan, Double money) {accountDao.in(inMan, money);System.out.println(1/0);accountDao.out(outMan, money);} }

    總結

    以上是生活随笔為你收集整理的转账为demo,spring事务的全部內容,希望文章能夠幫你解決所遇到的問題。

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