当前位置:网站首页>PageHelper在面对复杂service数据处理下的分页问题
PageHelper在面对复杂service数据处理下的分页问题
2022-06-23 15:27:00 【豆趣编程】
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成功自动递归把文件集合和评论集合自动插入了,这样可以提高查询效率,但是数据库服务器的压力会比较大
边栏推荐
- freemark 使用ftl文件 生成word
- MySQL transactions and locks
- The idea and method of MySQL master-slave only synchronizing some libraries or tables
- A transformer can only convert alternating current. How can I convert direct current?
- Explain in detail the principle and implementation of redis distributed lock
- VIM backup history command
- Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
- 自监督学习(SSL)Self-Supervised Learning
- ABP框架之——数据访问基础架构(下)
- 生成二叉搜索平衡树[利用树递归特性]
猜你喜欢

Important knowledge of golang: atomic atomic operation

Half wave loss equal thickness and equal inclination interference

Quartz

mysql 系列:总体架构概述

The running rabbit fell

SQL窗口函数怎么使用

golang 重要知识:context 详解

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)

A transformer can only convert alternating current. How can I convert direct current?

Stone from another mountain - Intelligent Question and answer technology in wechat search
随机推荐
中大人脸素描FERET数据库(CUFSF)
Android kotlin collaboration Async
图片读取:Image.open(ImgPath)
Horizon development board commissioning
139. Séparation des mots
医学影像分割的网站
How can genetic testing help patients fight disease?
Important knowledge of golang: sync Cond mechanism
139. 單詞拆分
力扣每日一题-第25天-495.提莫攻击
golang 重要知识:context 详解
一文看懂经典BUCK-BOOST负电压电路
Slice() and slice() of JS
Nfnet: extension of NF RESNET without BN's 4096 super batch size training | 21 year paper
解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误
golang 重要知识:mutex
Origin of sectigo (Comodo) Certificate
Variable declaration of go language
Which platform is a good place to open a futures account? Is it safe to open an online futures account?
Unshift() and shift() of JS