注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS("master")
public @interface Master {
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS("slave")
public @interface Slave {
}
service
这边有两个service,分别做了多数据源的转换
@Service
@Master
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
}
@Service
@Slave
public class TrainDataServiceImpl extends ServiceImpl<TrainDataMapper, TrainData> implements TrainDataService {
}
生效与否
@Service
@Master
@RequiredArgsConstructor
public class TestService {
private final TrainDataService trainDataService;
private final RoleService roleService;
public String login() {
// 生效
QueryWrapper<TrainData> wrapper = new QueryWrapper<>();
wrapper.eq(true,"id",3);
return trainDataService.getOne(wrapper).getCompletion();
}
public String login1() {
// 失效,因为用了lambdaQuery()
String s = roleService.lambdaQuery().list().get(0).getRoleId() + "";
return s;
}
}
结论
- 方法上使用了事务导致失效。
- 使用了lambdaQuery()导致切换失效。
评论区