pagehelper是mybatis配合一個很好用的插件,但是使用有一些侷限性
這是使用方式:
會在設置PageHelper.startPage((start / length + 1), length)後的第一個查詢自動做分頁
這樣處理比較簡單的數據就比較方便了,但是如果這個service中涉及到了多條mapper查詢,並且最後對數據做封裝,分頁就會出問題了。
理論上pagehelper只會對第一條產生分頁作用,可以在service中的第一個查詢中得到分頁結果,然後再進行遍歷查詢子集拼裝,但是頻繁的遍歷查詢需要不斷連接數據庫,導致查詢效率變慢
解決辦法:
我認爲最好的解決辦法就是儘量把複雜的業務邏輯用一條mapper查詢出來,實在不行再考慮程序拼接
案例:
我想做一個微信朋友圈接口,分爲3個表(這裏吐槽一下,因爲關係型數據庫的嚴謹性和數據易管理性,有時候一組數據需要多張表聯合查詢,不夠靈活,所以現在資訊類,微博類這種需要大量訪問的數據都主張noSql,查詢起來會更靈活快捷),朋友圈主題信息表,信息文件表,評論表
我需要將朋友圈文件集合和評論集合都嵌套進主體信息表中,來返回這種格式的數據:
難點就是如果將commentList和fileList塞進去
思路:mybaits中有集合的概念,可以通過在resultMap中設置集合使其自動遞歸查詢需要的集合,最好是配合實體類比較好
具體關於mybatis的collection可以看這個:https://blog.csdn.net/lianzhang861/article/details/86243532
1.新建三個實體類:主體類、文件類、評論類
然後再mapper.xml中的resultMap中設置好collection就行了,具體可以看我上面的鏈接
<resultMap id="Moment" type="com.bomc.recordLife.entry.Moment" >
<id column="MOMENT_ID" property="momentId"/>
<result column="MOMENT_TEXT" property="momentText" jdbcType="VARCHAR" />
<result column="MOMENT_USER" property="momentUser" jdbcType="VARCHAR" />
<result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP" />
<result column="MOMENT_STATUS" property="momentStatus" jdbcType="VARCHAR" />
<result column="MOMENT_TYPE" property="momentType" jdbcType="VARCHAR" />
<result column="IS_LIKE" property="isLike" jdbcType="VARCHAR" />
<result column="LONGITUDE" property="longitude" jdbcType="VARCHAR" />
<result column="LATITUDE" property="latitude" jdbcType="VARCHAR" />
<result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
<result column="PORTRAIT_IMG" property="portraitImg" jdbcType="VARCHAR" />
<collection column="{momentId=MOMENT_ID}" property="fileList"
ofType="com.bomc.recordLife.entry.MomentFile"
select="selectMomentFiles">
</collection>
<collection column="{momentId=MOMENT_ID}" property="commentList"
ofType="com.bomc.recordLife.entry.MomentComment"
select="selectMomentComments">
</collection>
</resultMap>
<resultMap id="MomentFile" type="com.bomc.recordLife.entry.MomentFile" >
<id column="FILE_ID" property="fileId"/>
<result column="MOMENT_ID" property="momentId" jdbcType="VARCHAR" />
<result column="FILE_TYPE" property="fileType" jdbcType="VARCHAR" />
<result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP" />
<result column="FILE_NAME" property="fileName" jdbcType="VARCHAR" />
<result column="FILE_URL" property="fileUrl" jdbcType="VARCHAR" />
<result column="FILE_SIZE" property="fileSize" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="MomentComment" type="com.bomc.recordLife.entry.MomentComment" >
<id column="COMMENT_ID" property="commentId"/>
<result column="MOMENT_ID" property="momentId" jdbcType="VARCHAR" />
<result column="COMMENT_TYPE" property="commmentType" jdbcType="VARCHAR" />
<result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP" />
<result column="COMMENT_CONTENT" property="commentContent" jdbcType="VARCHAR" />
<result column="CREATE_USER" property="createUser" jdbcType="VARCHAR" />
</resultMap>
<select id="getMoments" resultMap="Moment">
select
t.*,t3.USER_NAME,T3.PORTRAIT_IMG,
(select
case when count(t2.comment_id)>0 then '1' else '0' end from moments_comment t2
where t.moment_id = t2.moment_id
) IS_LIKE
from moments t
left join SYS_USER t3
on t.moment_user = t3.user_id
where
t.moment_status = '1'
order by t.create_time desc
</select>
<select id="selectMomentFiles" resultMap="MomentFile">
select t.* from moments_file t
where t.moment_id = #{momentId,jdbcType=VARCHAR}
</select>
<select id="selectMomentComments" resultMap="MomentComment">
select t.* from moments_comment t
where t.moment_id = #{momentId,jdbcType=VARCHAR}
</select>
這樣就可以mybatis成功自動遞歸把文件集合和評論集合自動插入了,這樣可以提高查詢效率,但是數據庫服務器的壓力會比較大