springboot mybatis 事务PlatformTransactionManager
@Configuration
publicclass TransactionManagerConfigBean {
@Autowired
private DataSource dataSource;
/**
* 自定义一个事务管理器1,同时作为默认事务管理器
* @return
*/
@Bean(name = "txManager1")
@Primary
public PlatformTransactionManager txManager1() {
returnnew DataSourceTransactionManager(dataSource);
}
/**
* 自定义一个事务管理器2
* @return
*/
@Bean(name = "txManager2")
public PlatformTransactionManager txManager2() {
returnnew DataSourceTransactionManager(dataSource);
}
}
@Service
publicclass ApiService {
@Autowired
private RoleMapper roleMapper;
@Autowired
private MenuMapper menuMapper;
@Transactional(value = "txManager2")
public void insert(Role role, Menu menu) throws Exception {
// 新增角色信息
roleMapper.insert(role);
// 新增菜单信息
menuMapper.insert(menu);
}
}