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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置数据库连接池(数据源) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<!-- 连接数据库的url -->
<property name="jdbcUrl" value="jdbc:mysql:///transaction?useUnicode=true&characterEncoding=utf-8"></property>
<!-- 连接数据库的用户名 -->
<property name="user" value="root"></property>
<!-- 连接数据库的密码 -->
<property name="password" value="123456"></property>
</bean>
<!-- 事务管理对象,依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 编写通知:对事务进行增强(通知),需要编写对切入点和具体执行事务细节 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- name 表示任意方法名称 -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 编写aop,让spring自动对目标生成代理,需要使用Aspcetj的表达式-->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.itheima.transaction.*.*(..))" id="pc"/>
<!-- 切面:将切入点与通知整合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<!-- 配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认使用数据库源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义id为accountDaoImpl的Bean -->
<bean id="accountDaoImpl" class="com.itheima.transaction.AccountDaoImpl">
<!-- 将jdbcTemplate注入到AccountDaoImpl实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
AccountDaoImpl
package com.itheima.transaction;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImpl implements AccountDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void transfer(int fromId, int toId, double money) {
String sql1 = "UPDATE account SET money = money - ? where id = ?";
this.jdbcTemplate.update(sql1,money,fromId);
//int i = 10 / 0;
String sql2 = "UPDATE account SET money = money + ? where id = ?";
this.jdbcTemplate.update(sql2,money,toId);
}
}
测试方法
package com.itheima.transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AccountTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService)applicationContext.getBean("accountDaoImpl");
//调用实例中的转账方法
accountService.tranfer(1, 2, 500);
System.out.println("转账成功.................");
}
}