首先在.xml中开启sprong对注解事务的支持
<tx:annotation-driven transaction-manager="transactionManager"/>
然后再Service中加上注解
@Service
@Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 转账
* @param source
* @param target
* @param money
*/
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void updateUser(String source, String target, Float money) {
userMapper.updateUserOfSub(source, money);
int a = 6/0;
userMapper.updateUserOfAdd(target, money);
}
}
最后测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class ServiceTest {
@Autowired
private UserService userService;
/**
* 转账业务
*/
@Test
public void testUpdate(){
userService.updateUser("张三丰","宋远桥",1F);
}
}