Mybatis Example的好处
使用了面向对象的思想,其关联数据库的单表查询都能自动生成。减少了劳动量,同时将复杂查询拆分成单表查询,加快了查询速度。让其复杂的关联在service层中使用代码进行拼装。这样处理速度会比一个大查询快得多,因为电脑本身就是干这个计算的。
public ServiceResponse doInOperation(ServiceResponse serviceResponse) throws Exception {
//医生信息
DictUserOrgInfoExample dictUserOrgInfoExample = new DictUserOrgInfoExample();
dictUserOrgInfoExampleCriteria = ();
()
.andAffiliatedDeptEqualTo(())
.andOrgIdEqualTo(());
List<DictUserOrgInfo> dictUserOrgInfos = (dictUserOrgInfoExample);
//医生姓名
List<Long> doctorIds = ().map(DictUserOrgInfo::getUserBaseId).distinct().collect(());
DictUserBaseInfoExample dictUserBaseInfoExample = new DictUserBaseInfoExample();
dictUserBaseInfoExampleCriteria = ();
()
.andIdIn(doctorIds);
List<DictUserBaseInfo> dictUserBaseInfos = (dictUserBaseInfoExample);
//职称名称
List<String> codes = ().map(DictUserOrgInfo::getProfessionalTitle).distinct().collect(());
DictDataExample dictDataExample = new DictDataExample();
dictDataExampleCriteria = ();
().andSubCodeIn(codes);
List<DictData> dictData = (dictDataExample);
//科室
List<Long> deptId = ().map(DictUserOrgInfo::getAffiliatedDept).distinct().collect(());
DictDepartmentExample dictDepartmentExample = new DictDepartmentExample();
dictDepartmentExampleCriteria = ();
()
.andIdIn(deptId);
List<DictDepartment> dictDepartments = (dictDepartmentExample);
//号源
OrdScheduleExample ordScheduleExample = new OrdScheduleExample();
ordScheduleExampleCriteria = ();
(Constants.FLAG_TRUE).andUserIdIn(doctorIds);
List<OrdSchedule> ordSchedules = (ordScheduleExample);
if ((())) {
(
("%%%s%%", ()));
(
("%%%s%%", ()));
List<DictUserOrgInfo> orgInfos = (dictUserOrgInfoExample);
List<Long> filterJobList = ().map(DictUserOrgInfo::getId)
.distinct().collect(());
List<DictUserBaseInfo> userBaseInfos = (dictUserBaseInfoExample);
List<Long> filterNameList = ().map(DictUserBaseInfo::getId)
.distinct().collect(());
dictUserOrgInfos = ().filter(d -> (())
|| (()))
.collect(());
}
List<ScheduleDoctorListRsp> list = new ArrayList<>();
for (DictUserOrgInfo dictUserOrgInfo : dictUserOrgInfos) {
ScheduleDoctorListRsp scheduleDoctorListRsp = new ScheduleDoctorListRsp();
Optional<DictUserBaseInfo> dictUserBaseInfo = ()
.filter(d -> ().equals(())).findFirst();
Optional<DictData> data = ()
.filter(d -> ().equals(())).findFirst();
Optional<DictDepartment> dictDepartment = ()
.filter(d -> ().equals(())).findFirst();
Optional<OrdSchedule> ordSchedule = ()
.filter(s -> ().equals(())).findFirst();
(() ? ().getUserName() : "");
(());
(());
(() ? ().getDeptName() : "");
(());
(() ? ().getTypeDesc() : "");
(() ? ().getTotalNumber() : 0);
(scheduleDoctorListRsp);
}
return (list);
}
总体思路就是根据接口需要的返回字段,查出返回字段所在表的实体集合。根据关联条件类似下面代码
//职称名称
List<String> codes = ().map(DictUserOrgInfo::getProfessionalTitle).distinct().collect(());
DictDataExample dictDataExample = new DictDataExample();
dictDataExampleCriteria = ();
().andSubCodeIn(codes);
List<DictData> dictData = (dictDataExample);
主表里存的是字典表中的编码 需要查编码对应的名称。所以根据主表的list集合拿到编码的集合 此处用到了java 8中的流特性。
这样就能查出所有的需要的字典表对象实体了。
下面这段代码是在循环拼装数据的时候使用。循环的list对象是主表。每次拿出循环当中一个对象,则拿主表中的编码字段和字典
表list中的一个相关联的字段(循环) 若这个字典表中有对象的字段和主表对象关联到。则在下面封装数据的时候拿此过滤的对象取
需要的属性就行了。
Optional<DictData> data = ()
.filter(d -> ().equals(())).findFirst();
需要模糊查询
模糊查询条件的话也是过滤主表list数据 在for循环中将不符合的过滤就行了
if ((())) {
(
("%%%s%%", ()));
(
("%%%s%%", ()));
List<DictUserOrgInfo> orgInfos = (dictUserOrgInfoExample);
List<Long> filterJobList = ().map(DictUserOrgInfo::getId)
.distinct().collect(());
List<DictUserBaseInfo> userBaseInfos = (dictUserBaseInfoExample);
List<Long> filterNameList = ().map(DictUserBaseInfo::getId)
.distinct().collect(());
dictUserOrgInfos = ().filter(d -> (())
|| (()))
.collect(());
}
拿出模糊搜索的表与主表关联的id集合。进行过滤,(并行条件使用||)这样就能搜索出符合条件的主表数据了,最后在下面遍历。