@Transactional
public BooleanMessage importUserOne(String ppUserId) {
deleteOneUser(ppUserId);
transferOneUser(ppUserId);
return BooleanMessage.getSuccessMessage("同步成功!");
}
@Transactional
public void deleteOneUser(String userid) {
try {
// 删除:用户
this.deleteUsers(userid);
} catch (Exception e) {
throw new RuntimeException("delete error : "+e.getMessage());
}
}
public void deleteUsers(String userid) {
dd用户Service.delete(userid);
}
public boolean delete(String ppUserId) {
return ddMapper.deleteByPrimaryKey(ppUserId) == 1;
}
@Transactional
public void transferOneUser(String userid) {
try {
// 迁移:用户
dd用户Service.transferUsers(findOne(userid));
} catch (Exception e) {
throw new RuntimeException("transferOneUser error : "+e.getMessage());
}
}
@Transactional
public void transferUsers(Users one) {
if (one == null) {
throw new NullPointerException("Users is null!");
}
try {
String userid = one.getUserid() == null ? "" : one.getUserid();
用户 mm用户 = new 用户();
mm用户.set用户id(userid);
this.add(mm用户);
} catch (Exception e) {
throw new RuntimeException("users add error : "+e.getMessage());
}
}
public boolean add(用户 mm用户) {
return ddMapper.insert(mm用户) == 1;
}
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
@SpringBootApplication
@ComponentScan(basePackages={"importdata.*"})
public class BshImportdataApplication {
public static void main(String[] args) {
SpringApplication.run(BshImportdataApplication.class, args);
}
}
```我在spring boot 2.1.1版本中,在service层中的方法上加上@Transactional的注解,
没办法启动这个方法的事务回滚,里面的方法执行一半出错也不会回滚成没执行之前的一样,求大神解决
类似这个 https://blog.csdn.net/weixin_42080277/article/details/86220280
你的service层添加组件:@Service
看过这个文章你就知道如何解决了,为数据库的操作都加上事务注解即可(简单的可以直接将事务注解写在service类上面,这样service中每一个操作都是有事务的),https://blog.csdn.net/u010597819/article/details/100125118
@Transactional(rollback=Exception.class)直接指定Exception异常回滚吧,你回滚的异常类型是不是方法内抛出的异常类型,@Transactional捕捉指定的异常类型进行回滚,如果抛出的异常不是指定异常,也不是指定异常的子类,或者继承类,一样不会回滚
其实是没有写事务管理器,单单只加个注解不行,谢谢大家