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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

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

PageHelper插件實現(xiàn)一對多查詢時的分頁問題

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

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

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

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

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

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

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:對應(yīng)javabean中的字段-->
  <!--oftype:對應(yīng)javabean的類型-->
  <!--javatype:對應(yīng)返回值的類型-->
  <!--column:對應(yīng)數(shù)據(jù)庫column的字段,不是javabean中的字段-->
  <!--select:對應(yīng)查詢子集的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}參數(shù)就是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>

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩激情视频在线观看 | 高h辣h双处全是肉军婚 | 精灵之森高清在线 | 亚洲精品国产一区二区在线 | 久草在线福利资站免费视频 | 日韩精品久久不卡中文字幕 | 天天狠天天天天透在线 | 国产一区在线 | 欧美ⅹxxxhd3d| 国产精品女同久久免费观看 | 高跟丝袜麻麻求我调教 | 男人和女人上床 | 999精品视频在线观看热6 | 吉川爱美与黑人解禁 | 日本一卡二卡3卡四卡网站精品 | 91高清免费国产自产 | 明星ai人脸替换造梦在线播放 | 成人人免费夜夜视频观看 | 关晓彤被调教出奶水 | 古代翁熄乩伦小说h | 三体动漫在线观看免费完整版2022 | 国产欧美日韩精品高清二区综合区 | 亚洲精品国产国语 | 非洲黑人又大粗gay 非洲黑人bbwbbwbbw | 99国产精品免费观看视频 | 成人免费毛片一区二区三区 | 黑人巨大和日本娇小中出 | 日本精品一区二区三区 | 人禽l交视频在线播放 视频 | 日韩二三区 | 国产一区日韩二区欧美三 | 亚洲成色www久久网站 | 亚洲欧美日韩综合在线 | 久久这里都是精品 | 四虎精品成人免费视频 | 亚洲天堂一区二区在线观看 | 国产拍拍拍免费专区在线观看 | 久久视频在线视频观看天天看视频 | 国产99视频精品免费视频免里 | 婷婷激情综合五月天 | 青青草原手机在线视频 |