demo
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
代码正文:
private TransactionTemplate transactionTemplate; //事务模板
@Autowired
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
@Override
public Boolean insertUserInfo(ChangeUserInfoDto dto) {
return transactionTemplate.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus transactionStatus) {
try {
changeUserInfoMapper.insertAllUserInfo(dto.getUid(),dto.getEmail(),dto.getOrgName(),dto.getOrgWebSite());
ChangeUserInfoDto dto1 = new ChangeUserInfoDto();
dto1.setImage(dto.getImage());
dto1.setUid(dto.getUid());
changeUserInfoMapper.updateOneUserInfo(dto1.getUid(),dto1.getOrgName(),dto1.getOrgWebSite(),dto1.getImage());
return true;
}catch (Exception e){
e.printStackTrace();
transactionStatus.setRollbackOnly();
}
return false;
}
});
}