ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
只需要在当前Service的实现类的操作方法内,使用DatasourceUtil.changeDb() 指那打那; 注意:使用多数据为了保证事务传播性,请@DSTransactional 注解。 ***** 具体如下: ``` /** * 切换到指定数据源 */ @DSTransactional @Override public void add(ProjectGroupEntity projectGroup) { // 切换到-指定数据源 DatasourceUtil.changeDb(DatasourceContext.me().getDbName()); // 校验参数 checkParam(projectGroup, false); // 保存 this.save(projectGroup); } /** * 同时操作-主数据源和从数据源 */ @Override public List<ProTableEntity> getProTableList(ProTableSearch proTableQuery) { // 切换到-指定数据源 DatasourceUtil.changeDb(DatasourceContext.me().getDbName()); /** * 先查询当前数据源-项目表List */ // 创建查询包装器 LambdaQueryWrapper<ProjectTableEntity> queryWrapper = new LambdaQueryWrapper<>(); // 设置条件 queryWrapper.eq(ProjectTableEntity::getDelFlag, CommonDelFlagEnum.NOT_DELETE.getCode()); // 设置排序 queryWrapper.orderByAsc(ProjectTableEntity::getSort); // 查询 List<ProjectTableEntity> projectTableList = this.list(queryWrapper); // 获取数据集标识的List List<String> domainKeyList = projectTableList.stream().map(projectTable -> projectTable.getDomainKey()).collect(Collectors.toList()); /** * 再查询主库数据源-项目基础表List */ DatasourceUtil.changeMasterDb(); // 获取参数 String domainName = proTableQuery.getDomainName(); String domainCode = proTableQuery.getDomainCode(); // 创建查询包装器 LambdaQueryWrapper<ProTableEntity> proTableQueryWrapper = new LambdaQueryWrapper<>(); // 设置条件 proTableQueryWrapper.eq(ProTableEntity::getDataFlag, CommonDataFlagEnum.NORMAL.getCode()) .eq(ProTableEntity::getDelFlag, CommonDelFlagEnum.NOT_DELETE.getCode()); // 判断 if (domainKeyList != null && domainKeyList.size() > 0) { // 设置条件 proTableQueryWrapper.notIn(ProTableEntity::getDomainKey, domainKeyList); } // 设置排序 proTableQueryWrapper.orderByAsc(ProTableEntity::getSort); // 条件查询 if (StrUtil.isNotBlank(domainName)) { proTableQueryWrapper.like(ProTableEntity::getDomainName, domainName); } if (StrUtil.isNotBlank(domainCode)) { proTableQueryWrapper.like(ProTableEntity::getDomainCode, domainCode); } // 返回 return proTableService.list(proTableQueryWrapper); }