隐藏

mybatis_collection用法,懒加载,一对多

发布:2022/3/24 9:37:56作者:管理员 来源:本站 浏览次数:671

mybatis_collection用法,懒加载,一对多

需求:li_store商城主表,li_banner,li_goods商品表,li_store_hit_call打call记录表

接口:查询获取列表,需要根据打call记录表排序,展示主表信息,商品list , banner list

封装返回对象

@Data

public class HitCallListVO implements Serializable {

   private String storeId;

   private String logo;

   private String storeName;

   private Integer count;

   private List<StoreBanner> bannerList;

   private List<GoodsRecommend> recommendList;

}

mybatis 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="cn.lili.modules.store.mapper.StoreHitCallMapper">


   <resultMap id="resultMap" type="cn.lili.modules.store.entity.vos.HitCallListVO">

       <result property="storeId" column="id"/>

       <result property="storeName" column="store_name"/>

       <result property="logo" column="store_logo"/>

       <result property="count" column="count"/>

       <collection property="bannerList"

                   javaType="list"

                   ofType="cn.lili.modules.store.entity.dos.StoreBanner"

                   select="cn.lili.modules.store.mapper.StoreHitCallMapper.getBannerListByStoreId"

                   column="id">

       </collection>

       <collection property="recommendList"

                   javaType="list"

                   ofType="cn.lili.modules.recommend.entity.GoodsRecommend"

                   select="cn.lili.modules.store.mapper.StoreHitCallMapper.getGoodsByStoreId"

                   column="id">

       </collection>

   </resultMap>

   

   <select id="selectCallPage" resultMap="resultMap">

       select

           case WHEN a.count is null then 0 else a.count end as `count`,

           d.id,

           d.store_logo,

           d.store_name

       from li_store d

           left join li_store_hit_call a

       on a.store_id = d.id

       order by `count` desc

   </select>

   <select id="getBannerListByStoreId" resultType="cn.lili.modules.store.entity.dos.StoreBanner">

       select

           *

       from li_store_banner

       where

             store_id = #{storeId}

         and delete_flag = 0

   </select>

   <select id="getGoodsByStoreId" resultType="cn.lili.modules.recommend.entity.GoodsRecommend">

       select

           *

       from li_goods_recommend

       where store_id = #{storeId}

         and delete_flag = 0

   </select>

</mapper>

collection不执行问题:

  • 检查主查询
    <select id="selectCallPage" resultMap="resultMap">
    <resultMap id="resultMap" type="cn.lili.modules.store.entity.vos.HitCallListVO">
    resultMap是否对应
  • column字段是否存在,是否正确