pagehelper分页结果出错
在使用pagehelper分页时
PageHelper.startPage(1,10).doSelectPageInfo(()->{
sellerDao.selectList(Wrappers.<Seller>query(condition));
});
查出的list比10条要少
原因:多表连接查询:一对多查询
<sql id="selectSql">
select*from route as r
left join category as c on r.cid=c.cid
left join seller as s on s.sid=r.sid
left join route_img as ri on r.rid=ri.rid
</sql>
<resultMap id="routeMap" type="route">
<id column="rid" property="rid"></id>
<result property="rname" column="rname"/>
<result property="price" column="price"/>
<result property="routeIntroduce" column="route_introduce"/>
<result property="rdate" column="rdate"/>
<result property="isThemeTour" column="is_theme_tour"/>
<result property="count" column="count"/>
<result property="cid" column="cid"/>
<result property="rimage" column="rimage"/>
<result property="sid" column="sid"/>
<result property="sourceId" column="sourceId"/>
<collection property="routeImgList" ofType="routeImg">
<id column="rgid" property="rgid"></id>
<result property="rid" column="rid"/>
<result property="bigpic" column="bigpic"/>
<result property="smallpic" column="smallpic"/>
</collection>
</resultMap>
<select id="findById" resultMap="routeMap">
<include refid="selectSql"></include>
</select>
pagehelper根据“多“(即conllection)的数量来查询了
解决方案:将原本1条sql的嵌套结果集查询,改为collection的select子查询来实现
<sql id="selectSql">
SELECT * FROM route
</sql>
<resultMap id="routeMap" type="route">
<id column="rid" property="rid"></id>
<association property="category" javaType="category" select="com.wzy.dao.CategoryDao.findById" column="cid">
<id column="cid" property="cid"></id>
</association>
<association property="seller" javaType="seller" select="com.wzy.dao.SellerDao.findById" column="sid">
<id column="sid" property="sid"></id>
</association>
<collection property="routeImgList" javaType="java.util.List" ofType="RouteImg" select="com.wzy.dao.RouteImgDao.findByRid" column="rid">
<id column="rgid" property="rgid"></id>
</collection>
</resultMap>
<select id="findById" resultMap="routeMap">
<include refid="selectSql"></include>
</select>