mybatis plus 手动提交事务的方式
时间: 2023-09-02 11:14:04 浏览: 115
手动提交事务的方式可以使用 Mybatis Plus 提供的 TransactionTemplate 或者编写代理类进行手动提交。例如:
```java
TransactionTemplate template = new TransactionTemplate(transactionManager);
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// 手动提交事务的代码块
}
});
```
或者使用代理类:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private DataSourceTransactionManager transactionManager;
private TransactionTemplate transactionTemplate;
@PostConstruct
public void init() {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
@Override
public void deleteUser(Long id) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// 手动提交事务的代码块
}
});
}
}
```
阅读全文