接口
package cn. csy. account. service;
import java. math. BigDecimal;
public interface AccountService {
public void transBalance ( Long from, Long to, BigDecimal balance) ;
}
使用注解的方式
实现类
import cn. csy. account. mapper. AccountMapper;
import cn. csy. account. service. AccountService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import org. springframework. transaction. annotation. Propagation;
import org. springframework. transaction. annotation. Transactional;
import java. math. BigDecimal;
@Service
@Transactional ( propagation = Propagation. REQUIRED)
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void transBalance ( Long from, Long to, BigDecimal balance) {
accountMapper. subBalance ( balance, from) ;
accountMapper. addBalance ( balance, to) ;
}
}
service.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: component-scan base-package = " cn.csy.account.service" > </ context: component-scan>
< tx: annotation-driven> </ tx: annotation-driven>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
</ beans>
使用配置文件的方式
实现类
package cn. csy. account. service. impl;
import cn. csy. account. mapper. AccountMapper;
import cn. csy. account. service. AccountService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import org. springframework. transaction. annotation. Propagation;
import org. springframework. transaction. annotation. Transactional;
import java. math. BigDecimal;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void transBalance ( Long from, Long to, BigDecimal balance) {
accountMapper. subBalance ( balance, from) ;
accountMapper. addBalance ( balance, to) ;
}
}
service.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: component-scan base-package = " cn.csy.account.service" > </ context: component-scan>
< tx: annotation-driven> </ tx: annotation-driven>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< tx: advice id = " myAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " myTxMethod" expression = " execution(* cn.csy.account.service.impl.*.*(..))" />
< aop: advisor advice-ref = " myAdvice" pointcut-ref = " myTxMethod" />
</ aop: config>
</ beans>