Mybatis使用pagehelper分页插件和一对多嵌套结果映射导致得到的分页数据少于pageSize

Mybatis使用pagehelper分页插件和一对多嵌套结果映射导致得到的分页数据少于pageSize

参考网上的其他博客,使用嵌套结果子查询的方法。会遇到N+1的问题,慎用。
小菜鸟 总结下 ,以防以后再遇到。

原blogMapper.xml 写法

 <resultMap type="com.liuhaoqi.raletiveblog.entity.dto.BlogDto" id="BlogDtoResultMap">
    <id property="id" column="id"/>
    <result property="title" column="title"/>
    <result property="content" column="content"/>
    <result property="firstPicture" column="first_picture"/>
    <result property="flag" column="flag"/>
    <result property="views" column="views"/>
    <result property="appreciation" column="appreciation"/>
    <result property="shareStatement" column="share_statement"/>
    <result property="commentabled" column="commentabled"/>
    <result property="published" column="published"/>
    <result property="recommend" column="recommend"/>
    <result property="updateTime" column="update_time"/>
    <result property="createTime" column="create_time"/>
    <result property="description" column="description"/>
    <result property="typeId" column="type_id"/>
    <result property="userId" column="user_id"/>
    <result property="tagIds" column="tag_ids"/>
    <result property="commentnum" column="commentnum"/>
    <association property="user" column="blog_user_id" javaType="com.liuhaoqi.raletiveblog.entity.User">
      <id property="id" column="userid"/>
      <result property="username" column="username"/>
      <result property="nickname" column="nickname"/>
      <result property="email" column="email"/>
      <result property="avatar" column="avatar"/>
    association>
    <association property="category" column="blog_category_id" javaType="com.liuhaoqi.raletiveblog.entity.Category">
      <id property="categoryid" column="categoryid"/>
      <result property="categoryname" column="categoryname"/>
    association>

    <collection property="tags" javaType="java.util.List" ofType="com.liuhaoqi.raletiveblog.entity.Tag" >
      <id property="tagid" column="tagid"/>
      <result property="tagname" column="tagname"/>
      <result property="blogclassId" column="blogclassId"/>
    collection>
  resultMap>



  <select id="getBlogList" resultMap="BlogDtoResultMap">
    select
    b.id,
    b.title,b.content,b.first_picture,b.flag,b.views,b.appreciation,b.share_statement,
    b.commentabled,b.published,b.recommend,b.update_time,b.create_time,b.description,b.type_id as blog_category_id,
    b.user_id as blog_author_id,
    b.tag_ids,b.commentnum,u.userid,u.username,u.nickname,u.email,u.avatar,t_type.categoryid,t_type.categoryname
    t_tag.*
    from t_blog b
    left outer join t_user u on b.user_id =u.userid
    left outer join t_type on b.type_id=t_type.categoryid
    left outer join t_tag on b.id = t_tag.blogclass_id
    order by b.update_time asc
  select>

  <select id="getDetailBlogById" parameterType="Long" resultMap="BlogDtoResultMap">
    select
    b.id,b.title,b.content,b.first_picture,b.flag,b.views,b.appreciation,b.share_statement,
    b.commentabled,b.published,b.recommend,b.update_time,b.create_time,b.description,b.type_id,
    b.user_id,b.tag_ids,b.commentnum,u.userid,u.username,u.nickname,u.email,u.avatar,t_type.categoryid,t_type.categoryname
    from t_blog b
    left outer join t_user u on b.user_id =u.userid
    left outer join t_type on b.type_id=t_type.categoryid
    left outer join t_tag on b.id = t_tag.blogclass_id
    where b.id=#{id}
    order by b.update_time asc
  select>

使用嵌套结果集子查询 。可以解决分页问题 但是会产生n+1问题


  <resultMap type="com.liuhaoqi.raletiveblog.entity.dto.BlogDto" id="BlogDtoResultMap">
    <id property="id" column="id"/>
    <result property="title" column="title"/>
    <result property="content" column="content"/>
    <result property="firstPicture" column="first_picture"/>
    <result property="flag" column="flag"/>
    <result property="views" column="views"/>
    <result property="appreciation" column="appreciation"/>
    <result property="shareStatement" column="share_statement"/>
    <result property="commentabled" column="commentabled"/>
    <result property="published" column="published"/>
    <result property="recommend" column="recommend"/>
    <result property="updateTime" column="update_time"/>
    <result property="createTime" column="create_time"/>
    <result property="description" column="description"/>
    <result property="typeId" column="type_id"/>
    <result property="userId" column="user_id"/>
    <result property="tagIds" column="tag_ids"/>
    <result property="commentnum" column="commentnum"/>
    <association property="user" column="blog_user_id" javaType="com.liuhaoqi.raletiveblog.entity.User">
      <id property="id" column="userid"/>
      <result property="username" column="username"/>
      <result property="nickname" column="nickname"/>
      <result property="email" column="email"/>
      <result property="avatar" column="avatar"/>
    association>
    <association property="category" column="blog_category_id" javaType="com.liuhaoqi.raletiveblog.entity.Category">
      <id property="categoryid" column="categoryid"/>
      <result property="categoryname" column="categoryname"/>
    association>

    <collection property="tags" javaType="java.util.List" ofType="com.liuhaoqi.raletiveblog.entity.Tag" select="getBlogTags" column="id">
      <id property="tagid" column="tagid"/>
      <result property="tagname" column="tagname"/>
      <result property="blogclassId" column="blogclassId"/>
    collection>
  resultMap>



  <select id="getBlogList" resultMap="BlogDtoResultMap">
    select
    b.id,
    b.title,b.content,b.first_picture,b.flag,b.views,b.appreciation,b.share_statement,
    b.commentabled,b.published,b.recommend,b.update_time,b.create_time,b.description,b.type_id as blog_category_id,
    b.user_id as blog_author_id,
    b.tag_ids,b.commentnum,u.userid,u.username,u.nickname,u.email,u.avatar,t_type.categoryid,t_type.categoryname
--     t_tag.*
    from t_blog b
    left outer join t_user u on b.user_id =u.userid
    left outer join t_type on b.type_id=t_type.categoryid
--     left outer join t_tag on b.id = t_tag.blogclass_id
    order by b.update_time asc
  select>

  <select id="getDetailBlogById" parameterType="Long" resultMap="BlogDtoResultMap">
    select
    b.id,b.title,b.content,b.first_picture,b.flag,b.views,b.appreciation,b.share_statement,
    b.commentabled,b.published,b.recommend,b.update_time,b.create_time,b.description,b.type_id,
    b.user_id,b.tag_ids,b.commentnum,u.userid,u.username,u.nickname,u.email,u.avatar,t_type.categoryid,t_type.categoryname
    from t_blog b
    left outer join t_user u on b.user_id =u.userid
    left outer join t_type on b.type_id=t_type.categoryid
--     left outer join t_tag on b.id = t_tag.blogclass_id
    where b.id=#{id}
    order by b.update_time asc
  select>

  <select id="getBlogTags" parameterType="Long" resultType="com.liuhaoqi.raletiveblog.entity.Tag">
    select * from t_tag where t_tag.blogclass_id=#{id};
  select>

你可能感兴趣的:(JavaEE)