项目中用到了pageHelper,但是在mybatis的resultmap中使用了collection去封装一对多的映射关系。
分页查询后始终记录数和实际数据对不上,原因出在pageHelper和collection有冲突。
原因:pagehelper是基于sql查询结果的数据条数进行拦截的,但是collection会把相同结果映射为List,
所以执行顺序是先拦截条数,后封装结果,造成了数据分页后查询数量变少。
例子:
数据库:
id name course
1 zhangsan chinese
2 zhangsan math
3 zhangsan english
4 lisi chinese
5 lisi english
6 wangwu math
mybatis配置
<resultmap .....>
<result property="id " column="id " />
<result property="name" column="name" />
<collection .....>
<result property="course" column="course" />
</collection>
</resultmap>
查询语句
select id,name,course from student st
分页信息
PageHelper.startPage(1,5);
List student = select();
student类class student{
long id;
string name;
List<String> course;
get set....
}
查询结果只有zhangsan lisi wangwu三条数据