mybatis 一对多 兼容 分页插件

由于业务需要,出现一对多查询,比如一个学生有多个课程 多个老师 多个成绩等

实体

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>

之所以采用这样的方法是因为要兼容分页插件,不然分页插件查询后数据又问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心系代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值