由于业务需要,出现一对多查询,比如一个学生有多个课程 多个老师 多个成绩等
实体
public class CarrierOutputVo implements Serializable{
List<CarrierBusinessTypeDto> carrierBusinessTypeRecords;
List<CarrierCompanyDto> carrierCompanyRecords;
List<CarrierContactDto> carrierContactRecords;
List<CarrierServiceRegionDto> 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 id="query" resultMap="QueryResultMap">
SELECT distinct <include refid="Query_Column_List" />
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
<where>
<if test="shortName!=null and shortName!=''">
and t1.short_name like concat('%', #{shortName}, '%')
</if>
<if test="businessTypeCode!=null and businessTypeCode!=''">
and t2.business_type_code =#{businessTypeCode}
</if>
<if test="domain!=null and domain!=''">
and t1.domain =#{domain}
</if>
<if test="companyCodes!=null and companyCodes.size()>0">
and t3.company_code in
<foreach collection="companyCodes" item="item" open="('" close="')" separator="','">
#{item}
</foreach>
</if>
</where>
原理是先查出对应的主表信息,然后查子表信息,由于我这个需要用到子表条件,所有关联了子表一起查询
重点信息在QueryResultMap
<include refid="Query_Column_List" /> 查询需要的字段
<resultMap id="QueryResultMap" type="com.ams.vo.carrier.CarrierOutputVo" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="domain" property="domain" jdbcType="SMALLINT" />
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="short_code" property="shortCode" jdbcType="VARCHAR" />
<result column="full_name" property="fullName" jdbcType="VARCHAR" />
<result column="short_name" property="shortName" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="SMALLINT" />
<result column="legal_representative" property="legalRepresentative" jdbcType="VARCHAR" />
<result column="enterprise_nature" property="enterpriseNature" jdbcType="VARCHAR" />
<result column="establish_date" property="establishDate" jdbcType="DATE" />
<result column="cooperation_date" property="cooperationDate" jdbcType="DATE" />
<result column="bank" property="bank" jdbcType="VARCHAR" />
<result column="bank_account" property="bankAccount" jdbcType="VARCHAR" />
<result column="business_license_reg_num" property="businessLicenseRegNum" jdbcType="VARCHAR" />
<result column="organization_code" property="organizationCode" jdbcType="VARCHAR" />
<result column="province" property="province" jdbcType="VARCHAR" />
<result column="city" property="city" jdbcType="VARCHAR" />
<result column="district" property="district" jdbcType="VARCHAR" />
<result column="street" property="street" jdbcType="VARCHAR" />
<result column="province_code" property="provinceCode" jdbcType="VARCHAR" />
<result column="city_code" property="cityCode" jdbcType="VARCHAR" />
<result column="district_code" property="districtCode" jdbcType="VARCHAR" />
<result column="street_code" property="streetCode" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="lon" property="lon" jdbcType="NUMERIC" />
<result column="lat" property="lat" jdbcType="NUMERIC" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="create_user" property="createUser" jdbcType="VARCHAR" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<result column="update_user" property="updateUser" jdbcType="VARCHAR" />
<result column="is_deleted" property="isDeleted" jdbcType="SMALLINT" />
<result column="bg_type" property="bgType" jdbcType="SMALLINT" />
<result column="version_num" property="versionNum" jdbcType="SMALLINT" />
<collection property="carrierBusinessTypeRecords"
ofType="list"
select="queryBusinessType"
javaType="java.util.ArrayList"
column="{carrierId=id}" />
<collection property="carrierCompanyRecords"
ofType="list"
select="queryCompany"
javaType="java.util.ArrayList"
column="{carrierId=id}" />
<collection property="carrierContactRecords"
ofType="list"
select="queryContact"
javaType="java.util.ArrayList"
column="{carrierId=id}" />
<collection property="carrierServiceRegionRecords"
ofType="list"
select="queryServiceRegion"
javaType="java.util.ArrayList"
column="{carrierId=id}" />
</resultMap>
从collection 开始关联子表信息
property=“carrierServiceRegionRecords” 对应实体的属性
ofType=“list”
select=“queryServiceRegion”
javaType=“java.util.ArrayList”
column=“{carrierId=id}”
查询子表信息
carrierId 和 column=“{carrierId=id}” 相对应
<select id="queryServiceRegion" resultType="com.ams.dto.carrier.CarrierServiceRegionDto">
SELECT
id,carrier_id AS carrierId,
region_name AS regionName,
region_code AS regionCode,
region_id AS regionId
FROM
ams_common_carrier_service_region_t t2
WHERE
t2.carrier_id = #{ carrierId}
</select>
之所以采用这样的方法是因为要兼容分页插件,不然分页插件查询后数据又问题