SpringMVC配置 事务管理
1、确保持久层配置完毕
2、pom.xml里追加spring-tx
3、application-context.xml追加
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <context:component-scan base-package="io.deolin.service" />
注意业务层的包名
4、在具体的业务方法上追加@Transactional注解
配置完成,通过两个insert方法之间追加一个异常来测试能否成功回滚
@Service public class MineServiceTxImpl implements MineService { private static final Logger log = LogManager.getLogger(MineServiceTxImpl.class); @Autowired private UserMapper userMapper; @Override @Transactional public void addUser() throws Exception { log.info("开始"); UserPo po = new UserPo(); po.setUsername("sss"); po.setPassword("aaa"); userMapper.add(po); Integer.valueOf("a"); userMapper.add(po); log.info("结束"); } }