* 接下来介绍XML版本的控制写法:
jdbc.user=root
jdbc.password=root
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/tx
jdbc.driverClass=com.mysql.jdbc.Driver
import com.atguigu.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TxTest {
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-config.xml");
@Test
public void test(){
BookService bookService = ioc.getBean(BookService.class);
bookService.checkout("Tom","ISBN-001");
System.out.println("结账完成!");
}
}
package com.atguigu.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
public class BookDao {
JdbcTemplate jdbcTemplate;
public void updateBalance(String userName, double price){
String sql = "UPDATE account SET balance = balance - ? WHERE username = ?";
jdbcTemplate.update(sql, price, userName);
}
public Double getPrice(String isbn){
String sql = "SELECT price FROM book WHERE isbn = ?";
return jdbcTemplate.queryForObject(sql, Double.class, isbn);
}
public void updateStock(String isbn){
String sql = "UPDATE book_stock SET stock = stock-1 WHERE isbn = ?";
jdbcTemplate.update(sql, isbn);
}
}
package com.atguigu.service;
import com.atguigu.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileNotFoundException;
public class BookService {
@Autowired
BookDao bookDao;
public void checkout(String username, String isbn){
// 第一步:减库存
bookDao.updateStock(isbn);
// 第二步:减余额,先查询图书价格
Double price = bookDao.getPrice(isbn);
bookDao.updateBalance(username, price);
}
}
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.atguigu"></context:component-scan>
<context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
<!-- 配置数据源 -->
<bean id="comboPooledDataSource" 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>
</bean>
<!-- 配置jdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 配置数据源 -->
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 事务控制 -->
<!-- 1.配置事务管理器(切面)让其进行控制,所以一定要导入面向切面的那几个包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.4.RELEASE.jar
spring-aspects-5.2.3.RELEASE.jar
-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制数据源 -->
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 2.开启基于xml配置的事务控制模式,依赖tx名称空间和aop名称空间。
1).配置出事务方法
2).告诉Spring哪些方法是事务方法,让事务切面按照切入点表达式去切入事务方法即可。
-->
<bean id="bookService" class="com.atguigu.service.BookService"></bean>
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.atguigu.service.*.*(..))"/>
<!-- 事务建议,事务增强 advice-ref="":指向事务管理器的配置 -->
<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<!-- 配置事务管理器 ,tx:attributes:事务属性 -->
<tx:advice id="myAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- 指明哪些方法是事务方法 -->
<tx:method name="*"/>
<tx:method name="checkout" propagation="REQUIRED" timeout="-1"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>