
//db.properties配置 src下的文件
jdbc.jdbcUrl=jdbc:mysql:///day43
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
//applicationContext.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!--指定读取配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 事物核心管管理器 依赖连接词池-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事物模板对象 -->
<bean name="transactionTemplat" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!-- 配置事物通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 配置织入 -->
<aop:config>
<!-- 切点表达式 -->
<aop:pointcut expression="execution(* cn.jy.service.*ServiceImp.*(..))" id="txPc"/>
<!-- 配通知给 切点-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>
<!--1 将连接池放进spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2 将jdbc模板放进连接池 -->
<!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- 3将AccountDaoImp放进连接池 -->
<bean name="accountDaoImp" class="cn.jy.dao.AccountDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4将userServiceImp放进连接池 -->
<bean name="accountService" class="cn.jy.service.AccountServiceImp">
<property name="ad" ref="accountDaoImp"></property>
</bean>
</beans>
//AccountDaoImp层
package cn.jy.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImp extends JdbcDaoSupport implements AccountDao {
@Override
public void addMoney(Integer id, Double money) {
getJdbcTemplate().update("update account1 set money=money+? where id=?",money,id);
}
@Override
public void decreaseMoney(Integer id, Double money) {
getJdbcTemplate().update("update account1 set money=money-? where id=?",money,id);
}
}
//AccountService层
package cn.jy.service;
import cn.jy.dao.AccountDao;
public class AccountServiceImp implements AccountService {
private AccountDao ad;
public void setAd(AccountDao ad) {
this.ad = ad;
}
@Override
public void transfer(Integer from, Integer to, Double money) {
ad.decreaseMoney(to, money);
//int i=0/0;
ad.addMoney(from, money);
}
}
//测试
package cn.jy.service;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="accountService")
private AccountService aa;
@Test
public void fun1(){
aa.transfer(1, 2,100d);
}
}