<!--数据源放入容器-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--aop注解事务 开启事务的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
/**
* @Transactional:
* 可以配置到类上,表明次类中所有方法都具有此事务
* 可以配置到方法 ,表明 此方法具有此事务
* 若同时配置到了类和方法上 就近原则
* propagation 默认是required
* readonly 默认是 false
*/
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//放在类上所有方法都需要控制当前类所有的方法都具有事务 // 默认增删改
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional(propagation = Propagation.REQUIRED, timeout = -1, readOnly = false)
public void tran(String inName, String outName, int money) throws SQLException {
accountDao.out (outName, money);
int i = 1 / 0;
accountDao.in (inName, money);
}
public void save() {
}
;
public void update() {
}
;
@Transactional(readOnly = true)
public void find() {
}
;
}