Pagination problems when using pagination plug PageHelper Mybatis
1-on-1 query, normal pagination
1 to many queries, such as queries using the left and right connectors of the total number of records can lead to a result, the number of sub-records will be superimposed to the primary number of records, resulting in an asymmetric data.
Summary: When using mybatis, in many inquiries and paged through when you need to use sub-query form.
1) 主记录的resultMap
<resultMap id="artWorkMap" type="Artwork"> <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"### associated subquery
column =" id "parameter value column name> ### biographees record </ Collection> </ The resultMap>
2) sub-records ResultMap, followed by" 1 to 1 "and the "one to many" of
<resultMap id="templateRigMap" type="Template">
<id column="bt_id" jdbcType="VARCHAR" property="id"></id>
<result column="bt_template_name" jdbcType="VARCHAR" property="templateName"></result>
<result column="bt_deleted" jdbcType="BIT" property="deleted"></result>
<result column="bt_create_date" jdbcType="TIMESTAMP" property="createDate"></result>
</resultMap>
<The resultMap ID = " materialCompositionsRigMap " type = "MaterialComposition">
<ID column = "bmc_id" the jdbcType = "VARCHAR" Property = "ID"> </ ID>
<Result column = "bmc_artwork_id" the jdbcType = "VARCHAR" Property = " artworkId "> </ Result>
</ the resultMap>
. 3) the master record query (query to one together, a data write-many other queries)
<select id="selectAllOrInCompany" resultMap="artWorkMap">
SELECT
ba.*
,su.id "u_id"
,su.username "u_username"
,bc.id
,bc.company_no
,bc.update_date "c_update_date"
,bb.id "bb_id"
,bb.update_date "bb_update_date"
,bt.id "bt_id"
,bt.update_date "bt_update_date"
from biz_artwork ba
left join sys_user su on su.id = ba.user_id
left join biz_company bc on bc.id = su.company_id
left join base_brand bb on ba.brand_id = bb.id
left join base_template bt on ba.template_id = bt.id
where ba.deleted = 0
<choose>
<when test="companyNo !=null and companyNo != ''">
and ba.artwork_no like concat('', #{companyNo}, '%')
</when>
<otherwise>
and ba.commited = 1
</otherwise>
</choose>
<if test="keywords !=null and keywords.length() > 0">
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
</ the SELECT>
4) sub-records check
<select id="selectMaterialCompositions" resultMap="materialCompositionsRigMap">
select
bmc.id "bmc_id"
,bmc.create_date "bmc_create_date"
,bmc.update_date "bmc_update_date"
from biz_material_composition bmc
where bmc.artwork_id = #{id} ### 接收主记录的列名的参数值
</select>