使用mybatis example 和 java 8的特性來實現多表關聯且帶有查詢條件的查詢
Mybatis Example的好處
使用了物件導向的思想,其關聯資料庫的單表查詢都能自動生成。減少了勞動量,同時將複雜查詢拆分成單表查詢,加快了查詢速度。讓其複雜的關聯在service層中使用程式碼進行拼裝。這樣處理速度會比一個大查詢快得多,因為電腦本身就是幹這個計算的。
public ServiceResponse doInOperation(ServiceResponse serviceResponse) throws Exception {
//醫生資訊
DictUserOrgInfoExample dictUserOrgInfoExample = new DictUserOrgInfoExample();
DictUserOrgInfoExample.Criteria dictUserOrgInfoExampleCriteria = dictUserOrgInfoExample.createCriteria();
dictUserOrgInfoExampleCriteria.andActiveEqualTo(Boolean.TRUE)
.andAffiliatedDeptEqualTo(request.getDepartmentId())
.andOrgIdEqualTo(request.getLoginOrgId());
List<DictUserOrgInfo> dictUserOrgInfos = dictUserOrgInfoService.selectByExample(dictUserOrgInfoExample);
//醫生姓名
List<Long> doctorIds = dictUserOrgInfos.stream().map(DictUserOrgInfo::getUserBaseId).distinct().collect(Collectors.toList());
DictUserBaseInfoExample dictUserBaseInfoExample = new DictUserBaseInfoExample();
DictUserBaseInfoExample.Criteria dictUserBaseInfoExampleCriteria = dictUserBaseInfoExample.createCriteria();
dictUserBaseInfoExampleCriteria.andActiveEqualTo(Boolean.TRUE)
.andIdIn(doctorIds);
List<DictUserBaseInfo> dictUserBaseInfos = dictUserBaseInfoService.selectByExample(dictUserBaseInfoExample);
//職稱名稱
List<String> codes = dictUserOrgInfos.stream().map(DictUserOrgInfo::getProfessionalTitle).distinct().collect(Collectors.toList());
DictDataExample dictDataExample = new DictDataExample();
DictDataExample.Criteria dictDataExampleCriteria = dictDataExample.createCriteria();
dictDataExampleCriteria.andActiveEqualTo(Boolean.TRUE).andSubCodeIn(codes);
List<DictData> dictData = dictDataService.selectByExample(dictDataExample);
//科室
List<Long> deptId = dictUserOrgInfos.stream().map(DictUserOrgInfo::getAffiliatedDept).distinct().collect(Collectors.toList());
DictDepartmentExample dictDepartmentExample = new DictDepartmentExample();
DictDepartmentExample.Criteria dictDepartmentExampleCriteria = dictDepartmentExample.createCriteria();
dictDepartmentExampleCriteria.andActiveEqualTo(Boolean.TRUE)
.andIdIn(deptId);
List<DictDepartment> dictDepartments = dictDepartmentService.selectByExample(dictDepartmentExample);
//號源
OrdScheduleExample ordScheduleExample = new OrdScheduleExample();
OrdScheduleExample.Criteria ordScheduleExampleCriteria = ordScheduleExample.createCriteria();
ordScheduleExampleCriteria.andActiveEqualTo(Constants.FLAG_TRUE).andUserIdIn(doctorIds);
List<OrdSchedule> ordSchedules = ordScheduleService.selectByExample(ordScheduleExample);
if (StringUtil.isNotEmpty(request.getQueryStr())) {
dictUserOrgInfoExampleCriteria.andJobNumberLike(
String.format("%%%s%%", request.getQueryStr()));
dictUserBaseInfoExampleCriteria.andUserNameLike(
String.format("%%%s%%", request.getQueryStr()));
List<DictUserOrgInfo> orgInfos = dictUserOrgInfoService.selectByExample(dictUserOrgInfoExample);
List<Long> filterJobList = orgInfos.stream().map(DictUserOrgInfo::getId)
.distinct().collect(Collectors.toList());
List<DictUserBaseInfo> userBaseInfos = dictUserBaseInfoService.selectByExample(dictUserBaseInfoExample);
List<Long> filterNameList = userBaseInfos.stream().map(DictUserBaseInfo::getId)
.distinct().collect(Collectors.toList());
dictUserOrgInfos = dictUserOrgInfos.stream().filter(d -> filterJobList.contains(d.getId())
|| filterNameList.contains(d.getUserBaseId()))
.collect(Collectors.toList());
}
List<ScheduleDoctorListRsp> list = new ArrayList<>();
for (DictUserOrgInfo dictUserOrgInfo : dictUserOrgInfos) {
ScheduleDoctorListRsp scheduleDoctorListRsp = new ScheduleDoctorListRsp();
Optional<DictUserBaseInfo> dictUserBaseInfo = dictUserBaseInfos.stream()
.filter(d -> d.getId().equals(dictUserOrgInfo.getUserBaseId())).findFirst();
Optional<DictData> data = dictData.stream()
.filter(d -> d.getSubCode().equals(dictUserOrgInfo.getProfessionalTitle())).findFirst();
Optional<DictDepartment> dictDepartment = dictDepartments.stream()
.filter(d -> d.getId().equals(dictUserOrgInfo.getAffiliatedDept())).findFirst();
Optional<OrdSchedule> ordSchedule = ordSchedules.stream()
.filter(s -> s.getUserId().equals(dictUserOrgInfo.getUserBaseId())).findFirst();
scheduleDoctorListRsp.setDoctorName(dictUserBaseInfo.isPresent() ? dictUserBaseInfo.get().getUserName() : "");
scheduleDoctorListRsp.setDoctorId(dictUserOrgInfo.getUserBaseId());
scheduleDoctorListRsp.setDeptId(dictUserOrgInfo.getAffiliatedDept());
scheduleDoctorListRsp.setDeptName(dictDepartment.isPresent() ? dictDepartment.get().getDeptName() : "");
scheduleDoctorListRsp.setJobNumber(dictUserOrgInfo.getJobNumber());
scheduleDoctorListRsp.setJobTitle(data.isPresent() ? data.get().getTypeDesc() : "");
scheduleDoctorListRsp.setTotalNumber(ordSchedule.isPresent() ? ordSchedule.get().getTotalNumber() : 0);
list.add(scheduleDoctorListRsp);
}
return serviceResponse.setBody(list);
}
總體思路就是根據介面需要的返回欄位,查出返回欄位所在表的實體集合。根據關聯條件類似下面程式碼
//職稱名稱
List<String> codes = dictUserOrgInfos.stream().map(DictUserOrgInfo::getProfessionalTitle).distinct().collect(Collectors.toList());
DictDataExample dictDataExample = new DictDataExample();
DictDataExample.Criteria dictDataExampleCriteria = dictDataExample.createCriteria();
dictDataExampleCriteria.andActiveEqualTo(Boolean.TRUE).andSubCodeIn(codes);
List<DictData> dictData = dictDataService.selectByExample(dictDataExample);
主表裡存的是字典表中的編碼 需要查編碼對應的名稱。所以根據主表的list集合拿到編碼的集合 此處用到了java 8中的流特性。
這樣就能查出所有的需要的字典表物件實體了。
下面這段程式碼是在迴圈拼裝資料的時候使用。迴圈的list物件是主表。每次拿出迴圈當中一個物件,則拿主表中的編碼欄位和字典
表list中的一個相關聯的欄位(迴圈) 若這個字典表中有物件的欄位和主表物件關聯到。則在下面封裝資料的時候拿此過濾的物件取
需要的屬性就行了。
Optional<DictData> data = dictData.stream()
.filter(d -> d.getSubCode().equals(dictUserOrgInfo.getProfessionalTitle())).findFirst();
需要模糊查詢
模糊查詢條件的話也是過濾主表list資料 在for迴圈中將不符合的過濾就行了
if (StringUtil.isNotEmpty(request.getQueryStr())) {
dictUserOrgInfoExampleCriteria.andJobNumberLike(
String.format("%%%s%%", request.getQueryStr()));
dictUserBaseInfoExampleCriteria.andUserNameLike(
String.format("%%%s%%", request.getQueryStr()));
List<DictUserOrgInfo> orgInfos = dictUserOrgInfoService.selectByExample(dictUserOrgInfoExample);
List<Long> filterJobList = orgInfos.stream().map(DictUserOrgInfo::getId)
.distinct().collect(Collectors.toList());
List<DictUserBaseInfo> userBaseInfos = dictUserBaseInfoService.selectByExample(dictUserBaseInfoExample);
List<Long> filterNameList = userBaseInfos.stream().map(DictUserBaseInfo::getId)
.distinct().collect(Collectors.toList());
dictUserOrgInfos = dictUserOrgInfos.stream().filter(d -> filterJobList.contains(d.getId())
|| filterNameList.contains(d.getUserBaseId()))
.collect(Collectors.toList());
}
拿出模糊搜尋的表與主表關聯的id集合。進行過濾,(並行條件使用||)這樣就能搜尋出符合條件的主表資料了,最後在下面遍歷。
相關文章
- 34. 過濾條件、多表查詢、子查詢
- mybatis多表聯合查詢的寫法MyBatis
- 20240719資料庫關聯查詢、條件查詢資料庫
- Spring Data JPA 實現多表關聯查詢Spring
- SQL查詢的:子查詢和多表查詢SQL
- JPA多表關聯查詢
- Mybatis【15】-- Mybatis一對一多表關聯查詢MyBatis
- mysql中的多表關聯查詢MySql
- hyperf關聯模型條件查詢模型
- Mybatis實現條件IN查詢(foreach)和invalid comparison異常MyBatis
- 【mybatis-plus】條件查詢MyBatis
- mysql帶AND關鍵字的多條件查詢MySql
- gorm 使用map實現in 條件查詢用法GoORM
- MyBatisPlus怎麼多表關聯查詢?MyBatis
- 如何做多表關聯查詢
- mybatis plus 使用LambdaQueryWrapper設定複雜的條件查詢MyBatisAPP
- MySQL筆記-左連線的使用(left join有關聯的多表查詢)MySql筆記
- Mybatis-技術專區-Criteria的and和or進行聯合條件查詢MyBatis
- mybatis-plus QueryWrapper條件查詢器MyBatisAPP
- jpa動態查詢與多表聯合查詢
- mysql多表查詢如何實現MySql
- onethinkphp 如何做多表關聯查詢PHP
- ORACLE 查詢條件出現關鍵字:&Oracle
- Spring Boot入門系列(十七)整合Mybatis,建立自定義mapper 實現多表關聯查詢!Spring BootMyBatisAPP
- 實現 MyBatis 流式查詢的方法MyBatis
- mysql條件查詢MySql
- MongoDB查詢條件MongoDB
- MyBatis帶參查詢MyBatis
- 多表查詢
- 區分關聯子查詢和非關聯子查詢
- 兩表關聯查詢:sql、mybatisSQLMyBatis
- JPA 之 多表聯合查詢
- SSH:hiberate實現資料的查詢(單查詢和全查詢)
- 寫一個“特殊”的查詢構造器 – (四、條件查詢:複雜條件)
- Laravel 關聯查詢限制條數和分組查詢顯示為零的結果Laravel
- mybatis lambdaQuery 查詢條件導致空指標MyBatis指標
- Mybatis連線池_動態sql語句_多表查詢實現MyBatisSQL
- Mybatis學習筆記 3:Mybatis 多種條件查詢MyBatis筆記