mybatis-plus关联查询,同表多次left join

商品表中有三级分类:classify1Id、classify2Id、classify3Id,关联分类表中id,查询商品时需要关联查询出这三级分类所对应的分类数据,所以需要多次left join。

商品表:
mybatis-plus关联查询,同表多次left join_第1张图片

分类表:
mybatis-plus关联查询,同表多次left join_第2张图片
因为是同一张表多次join,所以需要给分类表起个别名。
mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ozomall.dao.GoodsMapper">
    <resultMap id="goodsMap" type="com.ozomall.vo.GoodsVo">
        <id property="id" column="id"></id>
        <result property="goodsName" column="goods_name"></result>
        <result property="goodsPrice" column="goods_price"></result>
        <result property="commentCount" column="comment_count"></result>
        <result property="sales" column="sales"></result>
        <result property="brandId" column="brand_id"></result>
        <result property="classify1Id" column="classify1_id"></result>
        <result property="classify2Id" column="classify2_id"></result>
        <result property="classify3Id" column="classify3_id"></result>
        <result property="createTime" column="create_time"></result>
        <result property="status" column="status"></result>
        <result property="step" column="step"></result>
        <result property="cover" column="cover"></result>
        <result property="details" column="details"></result>
        <!--    一级分类对应数据    -->
        <association property="classify1" javaType="com.ozomall.entity.ClassifyDto" columnPrefix="classify1_">
            <id property="id" column="id"></id>
            <result property="name" column="name"/>
        </association>
        <!--    二级分类对应数据    -->
        <association property="classify2" javaType="com.ozomall.entity.ClassifyDto" columnPrefix="classify2_">
            <id property="id" column="id"></id>
            <result property="name" column="name"/>
        </association>
        <!--    三级分类对应数据    -->
        <association property="classify3" javaType="com.ozomall.entity.ClassifyDto" columnPrefix="classify3_">
            <id property="id" column="id"></id>
            <result property="name" column="name"/>
        </association>
    </resultMap>
    <select id="goodsList" parameterType="com.ozomall.entity.GoodsDto" resultMap="goodsMap">
        select goods.*,
        classify1.id as classify1_id,
        classify1.name as classify1_name,
        classify2.id as classify2_id,
        classify2.name as classify2_name,
        classify3.id as classify3_id,
        classify3.name as classify3_name
        from goods
        left join classify as classify1 on goods.classify1_id=classify1.id
        left join classify as classify2 on goods.classify2_id=classify2.id
        left join classify as classify3 on goods.classify3_id=classify3.id
        where goods.del=0
    </select>
</mapper>

返回结果:
mybatis-plus关联查询,同表多次left join_第3张图片

你可能感兴趣的:(mybatis,spring,boot)