一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - PageHelper插件實現一對多查詢時的分頁問題

PageHelper插件實現一對多查詢時的分頁問題

2021-07-31 12:07JouyPub Java教程

這篇文章主要介紹了PageHelper插件實現一對多查詢時的分頁問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

項目中經常會使用到一對多的查詢場景,但是pagehelper對這種嵌套查詢的支持不夠,如果是一對多的列表查詢,返回的分頁結果是不對的

參考github上的說明:https://github.com/pagehelper/mybatis-pagehelper/blob/master/wikis/zh/important.md

對于一對多的列表查詢,有兩種方式解決

1、在代碼中處理。單獨修改分頁查詢的resultmap,刪除collection標簽,然后在代碼中遍歷結果,查詢子集

2、使用mybatis提供的方法解決,具體如下

定義兩個resultmap,一個給分頁查詢使用,一個給其余查詢使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<resultmap id="basemap" type="com.xx.oo.activity">
  <id column="id" property="id" jdbctype="integer"/>
    ....
</resultmap>
 
<resultmap id="resultmap" type="com.xx.oo.activity" extends="basemap">
  <collection property="templates" oftype="com.xx.oo.template">
    <id column="pt_id" property="id" jdbctype="integer"/>
    <result column="pt_title" property="title" jdbctype="varchar"/>
  </collection>
</resultmap>
 
<resultmap id="richresultmap" type="com.xx.oo.activity" extends="basemap">
  <!--property:對應javabean中的字段-->
  <!--oftype:對應javabean的類型-->
  <!--javatype:對應返回值的類型-->
  <!--column:對應數據庫column的字段,不是javabean中的字段-->
  <!--select:對應查詢子集的sql-->
  <collection property="templates" oftype="com.xx.oo.template" javatype="java.util.list" column="id" select="querytemplatebyid">
    <id column="pt_id" property="id" jdbctype="integer"/>
    <result column="pt_title" property="title" jdbctype="varchar"/>
  </collection>
</resultmap>
 
<resultmap id="template" type="com.xx.oo.template">
  <id column="pt_id" property="id" jdbctype="integer"/>
  <result column="pt_title" property="title" jdbctype="varchar"/>
</resultmap>

需要分頁的查詢,使用richresultmap。先定義一個查詢子集的sql

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--這里的#{id}參數就是collection中定義的column字段-->
<select id="querytemplatebyid" parametertype="java.lang.integer" resultmap="template">
  select id pt_id, title pt_title
  from t_activity_template where is_delete=0 and activity_id = #{id}
  order by sort_number desc
</select>
<select id="querybypage" parametertype="com.xx.oo.activitypagerequest" resultmap="richresultmap">
  select t.*,t1.real_name creator_name
  from t_activity t
  left join user t1 on t1.user_id = t.creator
  <where>
    t.is_delete = 0
    <if test="criteria != null and criteria.length()>0">and (t.activity_name like concat("%",#{criteria},"%"))</if>
  </where>
  order by t.id desc
</select>

不需要分頁的普通查詢,使用resultmap

?
1
2
3
4
5
6
<select id="querybyid" parametertype="java.lang.integer" resultmap="resultmap">
  select t.*, t6.id pt_id, t1.title pt_title
  from t_activity t
  left join t_activity_template t1 on t.id=t6.activity_id and t1.is_delete=0
  where t.is_delete = 0 and t.id = #{id}
</select>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000018825136

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄大片影院一区二区 | 美女张开腿让我了一夜 | 亚洲 日韩 国产 制服 在线 | 草草视频在线观看 | 欧美最猛性xxxxx短视频 | 激情影院网站 | 性做久久久久免费观看 | 亚洲精彩视频在线观看 | 美女脱了内裤打开腿让你桶爽 | 亚洲国产成人超福利久久精品 | 校花被拖到野外伦小说 | 日本三级做a全过程在线观看 | 操双性人 | 色久久一个亚洲综合网 | 日韩免费在线看 | www.毛片在线观看 | www.九九| 我的妹妹最近有点怪在线观看 | 日本高清免费看 | 国产全部理论片线观看 | 无限韩国视频免费播放 | 性印度freehd | 激情影院网站 | 亚欧有色在线观看免费版高清 | 亚洲 小说 欧美 激情 另类 | 国产大片视频免费观看 | 欧美日韩一区二区中文字幕视频 | 日本欧美不卡一区二区三区在线 | 亚洲精品AV无码永久无码 | 午夜一区二区福利视频在线 | 成人影音先锋 | a及毛片 | 亚欧洲乱码视频一二三区 | 波多野给衣一区二区三区 | 办公室强行丝袜秘书啪啪 | 久久久久国产一级毛片高清片 | 国产精品亚洲精品观看不卡 | 女人麻豆国产香蕉久久精品 | 国产精品资源在线观看 | 亚洲视频一区网站 | 成人精品一区二区三区 |