用ssm框架分页查询教师表的教师
时间: 2023-08-15 15:38:58 浏览: 117
好的,您可以参考下面的代码实现:
1. 在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>
```
2. 在Dao层中定义方法:
```
public List<Teacher> findTeachersByPage(int start, int pageSize, String teacherName, String subject);
```
3. 在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;
```
4. 在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是一个自定义的分页实体类,用于封装分页数据。
阅读全文