subject是什么:在Mapper.xml文件中定义查询语句: 复制 <select id="findTeachersByPage" resultMap="teacherMap"> select * from teacher <where> <if test="teacherName != null and teacherName != ''"> and teacher_name like concat('%', #{teacherName}, '%') </if> <if test="subject != null and subject != ''"> and subject = #{subject} </if> </where> order by id desc limit #{start}, #{pageSize} </select> 在Dao层中定义方法: 复制 public List<Teacher> findTeachersByPage(int start, int pageSize, String teacherName, String subject); 在Service层中调用Dao层方法,并进行分页处理: 复制 int totalCount = teacherDao.getTotalCount(teacherName, subject); // 获取总记录数 int totalPage = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1); // 计算总页数 List<Teacher> teachers = teacherDao.findTeachersByPage(start, pageSize, teacherName, subject); // 获取当前页的数据 PageBean pageBean = new PageBean(currentPage, pageSize, totalCount, totalPage, teachers); // 封装分页数据 return pageBean; 在Controller层中获取请求参数,并调用Service层方法: 复制 @RequestMapping("/findTeachersByPage") public String findTeachersByPage(@RequestParam(defaultValue = "1") int currentPage, String teacherName, String subject, Model model) { int pageSize = 10; // 每页显示的记录数 int start = (currentPage - 1) * pageSize; // 当前页的起始记录索引 PageBean pageBean = teacherService.findTeachersByPage(start, pageSize, teacherName, subject); // 获取分页数据 model.addAttribute("pageBean", pageBean); model.addAttribute("teacherName", teacherName); model.addAttribute("subject", subject); return "teacherList"; } 其中,PageBean是一个自定义的
时间: 2024-04-03 12:34:34 浏览: 209
subject是指教师所教授的学科。在这个例子中,subject是作为一个查询条件,用于过滤查询结果,只返回指定学科的教师信息。在SQL语句中,如果subject有值,则会加上一个条件:and subject = #{subject},否则不加条件。在Dao层的findTeachersByPage方法中,subject作为一个参数传入,用于构造查询语句。在Service层的getTotalCount和findTeachersByPage方法中也都需要用到subject参数。
阅读全文