1、beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.2.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 16 17 <!-- 扫描包基础目录 --> 18 <context:component-scan base-package="com.wisezone"></context:component-scan> 19 20 <!-- 加载properties 配置文件 --> 21 <context:property-placeholder location="db.properties"/> 22 23 <!-- c3p0数据源配置 --> 24 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 25 <property name="driverClass" value="${driver}"></property> 26 <property name="jdbcUrl" value="${url}"></property> 27 <property name="user" value="${user}"></property> 28 <property name="password" value="${password}"></property> 29 </bean> 30 31 <!-- jdbc模板类配置 --> 32 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 33 <!-- 属性名称固定 --> 34 <property name="dataSource" ref="dataSource"></property> 35 </bean> 36 37 <aop:aspectj-autoproxy/> 38 39 <!-- 事物管理器配置 --> 40 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41 <property name="dataSource" ref="dataSource"></property> 42 </bean> 43 44 <tx:annotation-driven transaction-manager="txManager"/> 45 46 </beans>
1 package com.wisezone.service.impl; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Service; 6 import org.springframework.transaction.annotation.Propagation; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import com.wisezone.dao.AccountDao; 10 import com.wisezone.service.AccountService; 11 12 @Service 13 public class AccountServiceImpl implements AccountService { 14 15 @Resource 16 private AccountDao accountDao; 17 18 @Override 19 @Transactional(propagation=Propagation.REQUIRED) 20 public int updateAccountByTranfer(int yuanAid, int muBiaoAid, double money) { 21 22 int result = 0; 23 int r1 = accountDao.outAccount(yuanAid, money);//出账 24 int a = 1/0; 25 int r2 = accountDao.inAccount(muBiaoAid, money);//入账 26 27 if (r1 > 0 && r2 > 0) { 28 result = 1; 29 } 30 31 return result; 32 } 33 34 }