mybatis一對多分頁資料缺少問題解決方案
阿新 • • 發佈:2021-01-20
技術標籤:Mybatis系列踩坑記技術雜記mysqlmybatis資料庫
1,通過子查詢的方式即可解決pageHelper分頁中少取資料問題
<resultMap id="BaseResultMap" type="com.tcly.mall.vo.ums.UmsMomentsVO">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column=" code" jdbcType="VARCHAR" property="code"/>
<result column="type" jdbcType="INTEGER" property="type"/>
<result column="open_id" jdbcType="VARCHAR" property="openId"/>
<result column ="nickname" jdbcType="VARCHAR" property="nickname"/>
<result column="personalized_signature" jdbcType="VARCHAR" property="personalizedSignature"/>
<result column="OpenCode" jdbcType="VARCHAR" property ="openCode"/>
<result column="MemberName" jdbcType="VARCHAR" property="memberName"/>
<!--集合內查詢方式解決,column為上面的結果對應屬性-->
<collection property="umsMomentsRelationVOS" ofType="com.tcly.mall.vo.ums.UmsMomentsRelationVO" column="id"
javaType="list"
select="selectMomentRelations">
</collection>
</resultMap>
<!--子查詢對應的map-->
<resultMap id="UmsMomentsRelationVOS" type="com.tcly.mall.vo.ums.UmsMomentsRelationVO">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="moment_id" jdbcType="BIGINT" property="momentId"/>
<result column="relation_type" jdbcType="INTEGER" property="relationType"/>
<result column="topic_id" jdbcType="BIGINT" property="topicId"/>
<result column="topic_name" jdbcType="VARCHAR" property="topicName"/>
<result column="file_url" jdbcType="VARCHAR" property="fileUrl"/>
<result column="commodity_type" jdbcType="INTEGER" property="commodityType"/>
<result column="commodity_name" jdbcType="VARCHAR" property="commodityName"/>
<result column="commodity_pic" jdbcType="VARCHAR" property="commodityPic"/>
<result column="commodity_price" jdbcType="DECIMAL" property="commodityPrice"/>
</resultMap>
<!--子查詢-->
<select id="selectMomentRelations"
resultType="com.tcly.mall.vo.ums.UmsMomentsRelationVO"
resultMap="com.tcly.mall.dao.memberSource.UmsMomentsDao.UmsMomentsRelationVOS">
select *
from ums_moments_relation
where moment_id = #{id}
</select>
<select id="page" resultMap="BaseResultMap" parameterType="com.tcly.mall.dto.param.MomentsParam">
SELECT moments.*,
u.new_name as username
,u.icon
,u.nickname
,u.personalized_signature
,u.OpenCode
,u.MemberName
FROM ums_moments moments
LEFT JOIN ums_member u ON moments.member_id = u.id
<where>
<if test="queryParam.postContent != null">
AND moments.post_content LIKE CONCAT('%',#{queryParam.postContent},'%')
</if>
<if test="queryParam.companyId != null">
AND moments.company_id = #{queryParam.companyId}
</if>
<if test="queryParam.startTime != null and queryParam.startTime != ''">
AND DATE(moments.create_time) >= #{queryParam.startTime}
</if>
<if test="queryParam.endTime != null and queryParam.endTime != ''">
AND DATE(moments.create_time) <![CDATA[ <= ]]> #{queryParam.endTime}
</if>
<if test="queryParam.username != null">
AND u.username LIKE CONCAT('%',#{queryParam.username},'%')
</if>
AND moments.is_delete = 0
</where>
</select>
- 重點是這段程式碼,集合一定要這樣
<!--集合內查詢方式解決,column為上面的結果對應屬性-->
<collection property="umsMomentsRelationVOS" ofType="com.tcly.mall.vo.ums.UmsMomentsRelationVO" column="id"
javaType="list"
select="selectMomentRelations">
</collection>