spring--(25)声明式事务
生活随笔
收集整理的這篇文章主要介紹了
spring--(25)声明式事务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
接上篇配置,在com.test.spring.tx包下加入業務接口和類
####業務接口類
####業務實現類,注意添加有事務
package com.test.spring.tx;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;@Service("bookShopService") public class BookShopServiceImpl implements BookShopService{@Autowiredprivate BookShopDao bookShopDao;//聲明式事務@Transactional@Overridepublic void purchase(int userId, int bookId) {//1.獲取書的單價double price = bookShopDao.getBookPriceByBookId(bookId);//2.更新書的數量bookShopDao.updateBookStock(bookId);//3.更新用戶余額bookShopDao.updateUserBalance(userId, price);}}####測試類
package com.test.spring.tx;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestTx {private ApplicationContext ctx;private BookShopService bookShopService;{ctx = new ClassPathXmlApplicationContext("applicationContext.xml");bookShopService = ctx.getBean(BookShopService.class);}@Testpublic void testBookService(){bookShopService.purchase(1000, 1001);}}####配置文件
<context:component-scan base-package="com.test.spring.tx"></context:component-scan><context:property-placeholder location="classpath:db.properties"/><!-- 配置數據源 --><bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="initialPoolSize" value="${jdbc.initPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSources"></property></bean><!-- 配置事物管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSources"></property></bean><!-- 啟用事物注解 --><tx:annotation-driven transaction-manager="transactionManager"/>轉載于:https://my.oschina.net/u/2312022/blog/742276
總結
以上是生活随笔為你收集整理的spring--(25)声明式事务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马斯克:特斯拉自研芯片不会叫作 GPU,
- 下一篇: 《东方 New World》现已发售,登