1.资源配置文件 db.properties
jdbc.user=root
jdbc.password=linuxG
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring4
jdbc.initialPoolSize=5
jdbc.maxPoolSize=10
2.applicationContext.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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.huangliusong.spring"></context:component-scan>
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置C3P0数据源 -->
<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="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置 Spring的jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3.BookShopDao.java
package com.huangliusong.spring.tx.xml;
public interface BookShopDao {
//根据书号获取价格
public int findBookPriceByIsBn(String isbn);
//更新书的库存 使得书号 对应的库存-1
public void updateBookStock(String isbn);
//更新用户的余额 使得username的balance-price
public void updateUserAcount(String username,int price);
}
BookShopImpl.java
package com.huangliusong.spring.tx.xml;
import org.springframework.jdbc.core.JdbcTemplate;
import com.huangliusong.spring.tx.exception.BookStockException;
import com.huangliusong.spring.tx.exception.UserAcountException;
public class BookShopImpl implements BookShopDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public BookShopImpl() {
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public int findBookPriceByIsBn(String isbn) {
String sql = "select stock from book_stock where isbn=?";
return jdbcTemplate.queryForObject(sql, Integer.class, isbn);
}
public BookShopImpl(JdbcTemplate jdbcTemplate) {
super();
this.jdbcTemplate = jdbcTemplate;
}
@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 acount where username=?";
int balance = jdbcTemplate.queryForObject(sql2, Integer.class, username);
if (balance == 0) {
// 抛出异常
throw new UserAcountException("余额不足");
}
String sql = "update acount set balance=balance-? where username=?";
jdbcTemplate.update(sql, price, username);
}
}
BookShopService.java
package com.huangliusong.spring.tx.xml;
public interface BookShopService {
public void purchase(String username, String isbn);
}
BookShopServiceImpl.java
package com.huangliusong.spring.tx.xml;
public class BookShopServiceImpl implements BookShopService {
private BookShopDao bookShopDao;
public BookShopDao getBookShopDao() {
return bookShopDao;
}
public void setBookShopDao(BookShopDao bookShopDao) {
this.bookShopDao = bookShopDao;
}
//添加事务注解
@Override
public void purchase(String username, String isbn) {
// 1获取书的单价
int price=bookShopDao.findBookPriceByIsBn(isbn);
// 2更新库存
bookShopDao.updateBookStock(isbn);
// 3更新用户的余额
bookShopDao.updateUserAcount(username, price);
}
}
Cashier.java
package com.huangliusong.spring.tx.xml;
import java.util.List;
public interface Cashier {
public void checkOut(String username,List<String> isbn);
}
CashierImpl.java
package com.huangliusong.spring.tx.xml;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.huangliusong.spring.tx.exception.UserAcountException;
public class CashierImpl implements Cashier {
private BookShopService bookShopService;
@Override
public void checkOut(String username, List<String> isbns) {
for (String isbn : isbns) {
bookShopService.purchase(username, isbn);
}
}
public BookShopService getBookShopService() {
return bookShopService;
}
public void setBookShopService(BookShopService bookShopService) {
this.bookShopService = bookShopService;
}
}
SpringTransactionTest.java
package com.huangliusong.spring.tx.xml;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTransactionTest {
private ApplicationContext ctx=null;
private BookShopDao bookShopDao=null;
private BookShopService bookShopService=null;
private Cashier cashier=null;
{
ctx=new ClassPathXmlApplicationContext("applicationContext-xmls.xml");
bookShopDao=(BookShopDao)ctx.getBean(BookShopDao.class);
bookShopService=(BookShopService)ctx.getBean(BookShopService.class);
cashier=(Cashier)ctx.getBean(Cashier.class);
}
@Test
public void testBookShopService(){
bookShopService.purchase("hls", "1");
}
@Test
public void testBookShopTransactional(){
cashier.checkOut("hls", Arrays.asList("1","2"));
}
}