由于业务需要,出现一对多查询,比如一个学生有多个课程 多个老师 多个成绩等
实体
public class CarrierOutputVo implements Serializable{
List carrierBusinessTypeRecords;
List carrierCompanyRecords;
List carrierContactRecords;
List carrierServiceRegionRecords;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long id;
private Short domain;
private String code;
private String shortCode;
private String fullName;
private String shortName;
private Short status;
}
查询sql
SELECT distinct from ams_common_carrier_t t1 left join ams_common_carrier_business_type_t t2 on t2.carrier_id = t1.id left join ams_common_carrier_company_t t3 on t3.carrier_id = t1.id left join ams_common_carrier_contact_t t4 on t4.carrier_id = t1.id left join ams_common_carrier_service_region_t t5 on t5.carrier_id = t1.id and t1.short_name like concat('%', #{shortName}, '%') and t2.business_type_code =#{businessTypeCode} and t1.domain =#{domain} and t3.company_code in #{item}
原理是先查出对应的主表信息,然后查子表信息,由于我这个需要用到子表条件,所有关联了子表一起查询
重点信息在QueryResultMap
查询需要的字段
从collection 开始关联子表信息
property=“carrierServiceRegionRecords” 对应实体的属性
ofType=“list”
select=“queryServiceRegion”
javaType=“java.util.ArrayList”
column=“{carrierId=id}”
查询子表信息
carrierId 和 column=“{carrierId=id}” 相对应
之所以采用这样的方法是因为要兼容分页插件,不然分页插件查询后数据又问题



