标签搜索

目 录CONTENT

文章目录

Mybatisplus多数据源@DS失效原因

陈铭
2023-08-28 / 0 评论 / 0 点赞 / 610 阅读 / 172 字 / 正在检测是否收录...

注解

@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;
    }
}

结论

  1. 方法上使用了事务导致失效。
  2. 使用了lambdaQuery()导致切换失效。
0

评论区