Spring_事务(基于xml文件的方式)
2021-01-02 19:28
阅读:803
标签:this rri exec 数据源 read lis 管理器 配置 toc
applicationContext-tx-xml.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:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> context:property-placeholder location="classpath:db.properties" /> bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> property name="user" value="${jdbc.user}">property> property name="password" value="${jdbc.password}">property> property name="jdbcUrl" value="${jdbc.jdbcUrl}">property> property name="driverClass" value="${jdbc.driverClass}">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="dataSource">property> bean> bean id="bookShopDao" class="com.aff.spring.tx.xml.BookShopDaoImpl"> property name="jdbcTemplate" ref="jdbcTemplate">property> bean> bean id="bookShopService" class="com.aff.spring.tx.xml.service.impl.BookShopServiceImpl"> property name="bookShopDao" ref="bookShopDao">property> bean> bean id="cashier" class="com.aff.spring.tx.xml.service.impl.CashierImpl"> property name="bookShopService" ref="bookShopService">property> bean>beans>
BookShopDaoImpl.java
package com.aff.spring.tx.xml; import org.springframework.jdbc.core.JdbcTemplate; public class BookShopDaoImpl implements BookShopDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public int findBookPriceByIsBn(String isbn) { String sql = "select price from book where isbn = ?"; return jdbcTemplate.queryForObject(sql, Integer.class, isbn); } @Override public void updateBookStock(String isbn) { // 检查书的库存是否足够, 若不够抛出异常 String sql2 = "select stock from book_stock where isbn = ?"; int stock = jdbcTemplate.queryForObject(sql2, Integer.class, isbn); if (stock == 0) { throw new BookStockException("库存不足"); } String sql = "update book_stock set stock = stock -1 where isbn = ?"; jdbcTemplate.update(sql, isbn); } @Override public void updateUserAcount(String username, int price) { // 验证余额是否足够, 若不足则抛出异常 String sql2 = "select balance from account where username = ?"; int balance = jdbcTemplate.queryForObject(sql2, Integer.class, username); if (balance price) { throw new UserAccountException("余额不足"); } String sql = "update account set balance = balance-? where username = ?"; jdbcTemplate.update(sql, price, username); } }
BookShopServiceImpl.java
package com.aff.spring.tx.xml.service.impl; import com.aff.spring.tx.xml.BookShopDao; import com.aff.spring.tx.xml.service.BookShopService; public class BookShopServiceImpl implements BookShopService { private BookShopDao bookShopDao; public void setBookShopDao(BookShopDao bookShopDao) { this.bookShopDao = bookShopDao; } public void purchase(String username, String isbn) { // 1.获取书的单价 int price = bookShopDao.findBookPriceByIsBn(isbn); // 2.跟新书的库存 bookShopDao.updateBookStock(isbn); // 3. 跟新用户余额 bookShopDao.updateUserAcount(username, price); } }
CashierImpl.java
package com.aff.spring.tx.xml.service.impl; import java.util.List; import com.aff.spring.tx.xml.service.BookShopService; import com.aff.spring.tx.xml.service.Cashier; public class CashierImpl implements Cashier { private BookShopService bookShopService; public void setBookShopService(BookShopService bookShopService) { this.bookShopService = bookShopService; } @Override public void checkout(String username, Listisbns) { for (String isbn : isbns) { bookShopService.purchase(username, isbn); } } }
SpringTransactionTest.java
package com.aff.spring.tx.xml; import java.util.Arrays; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aff.spring.tx.xml.service.BookShopService; import com.aff.spring.tx.xml.service.Cashier; public class SpringTransactionTest { private ApplicationContext ctx = null; private BookShopDao bookShopDao = null; private BookShopService bookShopService = null; private Cashier cashier = null; { ctx = new ClassPathXmlApplicationContext("applicationContext-tx-xml.xml"); bookShopDao = ctx.getBean(BookShopDao.class); bookShopService = ctx.getBean(BookShopService.class); cashier = ctx.getBean(Cashier.class); } // 事务的传播 @Test public void testTransactionlPropagetion() { cashier.checkout("AA", Arrays.asList("1001","1002")); } @Test public void testBookShopService() { bookShopService.purchase("AA", "1001"); } }
目录
Spring_事务(基于xml文件的方式)
标签:this rri exec 数据源 read lis 管理器 配置 toc
原文地址:https://www.cnblogs.com/afangfang/p/12992718.html
下一篇:java 获取时间方式
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:Spring_事务(基于xml文件的方式)
文章链接:http://soscw.com/index.php/essay/39644.html
文章标题:Spring_事务(基于xml文件的方式)
文章链接:http://soscw.com/index.php/essay/39644.html
评论
亲,登录后才可以留言!