mybatis collection 一對多關聯查詢,單邊分頁的問題總結!


若想直接通過sql實現多級關聯查詢表結構得有2 個必不可少的字段:id ,parentId,levelId

id:主鍵id,

parentId:父id

levelId:表示第幾級(表本身關聯查詢的時候需要用到,不然會有重復數據)

 

利用mybatis collection 實現一對多關聯查詢

Dto:(一級)

public class ProvinceInfoDTO implements Serializable {
   private String id;
   private String name;
   private String pinyin;
   private String firstLetter;
   private List<CitysInfoDTO> cities;
}

Dto:(二級)

public class CitysInfoDTO implements Serializable {
    private String id;
    private String name;
    private String pinyin;
    private String firstLetter;
    private String acronym;
    private List<RegionInfoDTO> regions;
}

Dto(三級)

public class RegionInfoDTO implements Serializable {
    private String id;
    private String name;
    private String pinyin;
    private String firstLetter;
}

 

 

resultMap
<resultMap id="unloadAreaQueryResultMap" type="com.haoyunhu.res.ProvinceInfoDTO">   // 一級
       <result column="aid" property="id"/>
       <result column="aname" property="name"/>
       <result column="apinyin" property="pinyin"/>
       <result column="aletter" property="firstLetter"/>
      <collection property="cities" ofType="com.haoyunhu.res.CitysInfoDTO">         //二級
            <result column="bid" property="id"/>
            <result column="bname" property="name"/>
            <result column="bpinyin" property="pinyin"/>
            <result column="bletter" property="firstLetter"/>
            <result column="bacronym" property="acronym"/>
            <collection property="regions" ofType="com.haoyunhu.res.RegionInfoDTO">  // 三級
                <result column="cid" property="id"/>
                <result column="cname" property="name"/>
                <result column="cpinyin" property="pinyin"/>
                <result column="cletter" property="firstLetter"/>
            </collection>
      </collection>
</resultMap>

 

省市區級聯查詢sql:

SELECT a.PROV_NAME,


b.PROV_NAME,


c.PROV_NAME


FROM T_DATA_AREAS a


LEFT JOIN T_DATA_AREAS b


ON a.id=b.PROV_PARENTID


LEFT JOIN T_DATA_AREAS C


ON b.id =C.PROV_PARENTID


WHERE a.PROV_LEVELTYPE=1


AND b.PROV_LEVELTYPE =2


AND c.PROV_LEVELTYPE =3


ORDER BY a.PROV_NAME;

 

 

以上mybatis的操作最終得到的json是:

{

    "id":"310000",
    "name":"上海",
    "pinyin":"Shanghai",
    "firstLetter":"",
    "cities":[
        {
            "id":"310100",
            "name":"上海市",
            "pinyin":"Shanghai",
            "firstLetter":"",
            "acronym":"",
            "regions":[
                {
                    "id":"230506",
                    "name":"寶山區",
                    "pinyin":"Baoshan",
                    "firstLetter":""
                }
            ]
        }
    ]

}

 

一對多關聯查詢單邊分頁:

上述的這種關聯查詢時沒法直接使用pagehelper分頁插件的,因為pagehelper分頁插件是針對真個sql 截取,所以只能手動分頁(簡單,分頁無非就是截取sql +查詢總條數),

所以若一對多關聯查詢想分頁得針對主表(案例中的A表)截取sql(pageindex pagesize) ,再另外寫條sql查詢主表(案例中的A表)的記錄數 ------注意mysql 的偏移量的概念

 

還需要注意的坑:

1:這種需要做級聯查詢,表結構又不滿足的坑:若表結構不滿足,但是又想省事,就得寫sql 把原表整成這種固定格式的臨時表結構(子查詢)

2:電商領域的商品類目的級聯查詢和這個省市區的查詢一樣(都需要levelid,用在查詢條件處:where a.levelid=0 and b.levelid=1  and  c.levelid=2;  當然這種級聯查詢可以在java代碼中可以實現,三條sql,分別查詢 level  =0  1  2的,然后java代碼里面循環!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM