由于使用了微服务,会有多个数据库的情况,有时业务需要,需要切换数据源,所以使用了Mybatis plus的@DS来切换多数据源
yml数据库配置如下:
spring:
datasource:
dynamic:
primary: master
datasource:
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://user
username: root
password: root
common:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://book
username: root
password: root
service如下,默认是master数据源
@Service
@Slf4j
public class MasterService {
@Autowired
UserService userService;
@Autowired
BookService bookService;
/**必须master库方法先执行,才能回滚,达到事务效果*/
@Transactional(rollbackFor = Exception.class)
public void upload(ReqDto reqDto){
userService.save(reqDto);
bookService.save(reqDto);
}
}