使用Mybatis分页插件PageHelper时的分页问题
1对1查询,分页正常
1对多查询,如使用左右连接查询则会导致结果的总记录条数,子记录条数会叠加到主记录条数,导致数据不对称。
总结:使用mybatis时,在一对多的查询并且需要分页的时候需要使用子查询形式。
1) 主记录的resultMap
<resultMap > <id column="id" jdbcType="VARCHAR" property="id"></id> <result column="commited" jdbcType="VARCHAR" property="commited"></result> <association property="user"> <id column="u_id" jdbcType="VARCHAR" property="id"></id> <result column="u_username" jdbcType="VARCHAR" property="username"></result> </association> <association property="template" resultMap="templateRigMap"> </association> <collection property="materialCompositions" ofType="MaterialComposition"
select="selectMaterialCompositions" ### 关联的子查询
column="id"> ### 传主记录的列名的参数值 </collection> </resultMap>
2) 子记录的ResultMap,依次为 "1对1" 的与 "1对多" 的
<resultMap ></result>
</resultMap>
<resultMap ></result>
</resultMap>
3)主记录查询 (把一对一的查询放在一起,一对多的数据另写一条查询)
<select >
and (
ba.artwork_no like concat('%', #{keywords}, '%')
or su.username like concat('%', #{keywords}, '%')
or bc.company_name like concat('%', #{keywords}, '%')
)</if>
order by ba.update_date desc
</select>
4)子记录查询
<select >#{id} ### 接收主记录的列名的参数值
</select>