请教关于mybatis 一对多查询 分页错误

zhuailhw123 2019-12-28 08:44:21
ssm框架 用PageHelper来分页

一个teacher表

一个course表 字段teacherId外键关联teacher表主键id


一个student表

course和student是多对多关系

用表s_c_m关联 student 和course表


s_id外键关联student的id
c_id外键关联course的id

现在我是想查询出某个教师的所有课程以及选择这些课程的学生


public ResultUtil getStudentCourses(Integer page, Integer limit, Integer teacherId)
{

PageHelper.startPage(page,limit);
List<Course> courses=courseDao.getStudentCourseByTeacherId(teacherId);
PageInfo<Course> pageInfo=new PageInfo<>(courses);


ResultUtil resultUtil=new ResultUtil();
resultUtil.setCode(0);
resultUtil.setCount(pageInfo.getTotal());
resultUtil.setData(pageInfo.getList());
return resultUtil;
}

mapper中:

<resultMap id="courseResultMap1" type="com.mecol.bookshop_ssm.entity.Course">
<id property="id" column="cid"/>
<result property="courseName" column="courseName"/>
<result property="place" column="place"/>

<collection property="studentList" ofType="com.mecol.bookshop_ssm.entity.Student">
<result property="username" column="username"/>
</collection>
</resultMap>

<select id="getStudentCourseByTeacherId" resultMap="courseResultMap1">
SELECT c.id AS cid ,s.username ,c.courseName,c.place
FROM student s
INNER JOIN
s_c_m m ON s.id=m.s_id
INNER JOIN course c ON m.c_id=c.id
where c.teacherId=#{teacherId}
order by s.id
</select>

我这样可以查询出来结果 但是PageHelper分页是错误的:

{"id":13,"courseName":"口述历史","place":"A102","studentList":[{"id":0,"username":"王五"},{"id":0,"username":"王安石"},{"id":0,"username":"李白"]},


{"id":18,"courseName":"汽车构造原理","place":"C303","studentList":[{"id":0,"username":"王五"},{"id":0,"username":"王安石"},{"id":0,"username":"李白"}]},


{"id":14,"courseName":"MATLAB仿真应用","place":"B201""studentList":[{"id":0,"username":"王安石"},{"id":0,"username":"李白"]},


{"id":19,"courseName":"疯狂文言文","place":"C302","studentList":[{"id":0,"username":"王安石",},{"id":0,"username":"李白",]}


我想得到的是四条数据 但是pageInfo里面显示的是10条数据 是按照studentList中的数目来的 请教大家 怎么解决这个问题?谢谢了


...全文
185 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuailhw123 2019-12-29
  • 打赏
  • 举报
回复
解决了 先查主表 然后再查子表 谢谢
iceSnowPear 2019-12-29
  • 打赏
  • 举报
回复
你直接把 PageHelper.startPage(page,limit); 改成数字 PageHelper.startPage(pageNum,4);

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧