1.首先创建配置类,增加MONGODB事务配置
@Configuration
public class TransactionConfig {
@Bean
MongoTransactionManager transactionManager(MongoDbFactory factory){
return new MongoTransactionManager(factory);
}
}
2.服务层使用mongotemplate方法上使用@Transactional注解标识使用事务支持
@Service
public class UserService {
@Resource
MongoTemplate mongotemplate;
public List<User> getAllUser(){
return mongotemplate.findAll(User.class);
}
@Transactional
public boolean addUser(User user, UserInfo userInfo){
user.setUserInfo(userInfo);
mongotemplate.save(userInfo);
// int a = 1/0; //事务测试代码
mongotemplate.save(user);
return true;
}
}