PageHelper在面对复杂service数据处理下的分页问题

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成功自动递归把文件集合和评论集合自动插入了,这样可以提高查询效率,但是数据库服务器的压力会比较大

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章