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中的数目来的 请教大家 怎么解决这个问题?谢谢了