ssm通过模糊查询教师名对教师表进行分页查询
时间: 2023-07-20 09:44:07 浏览: 97
好的,我可以为您提供一个简单的示例代码,演示如何在SSM中使用模糊查询和分页查询来查询教师表。请参考下面的代码:
首先,在mapper接口中定义一个方法,如下所示:
```
public interface TeacherMapper {
List<Teacher> findTeachersByPage(@Param("name") String name, @Param("start") int start, @Param("pageSize") int pageSize);
int countTeachers(@Param("name") String name);
}
```
然后,在mapper.xml文件中实现该方法,如下所示:
```
<select id="findTeachersByPage" resultType="Teacher">
select *
from teacher
where name like CONCAT('%',#{name},'%')
order by id
limit #{start},#{pageSize}
</select>
<select id="countTeachers" resultType="int">
select count(*)
from teacher
where name like CONCAT('%',#{name},'%')
</select>
```
接下来,在service层中调用mapper接口中的方法,如下所示:
```
@Service
public class TeacherServiceImpl implements TeacherService {
@Autowired
private TeacherMapper teacherMapper;
@Override
public PageInfo<Teacher> findTeachersByPage(String name, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Teacher> teachers = teacherMapper.findTeachersByPage(name, (pageNum - 1) * pageSize, pageSize);
PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
return pageInfo;
}
@Override
public int countTeachers(String name) {
return teacherMapper.countTeachers(name);
}
}
```
最后,在controller层中调用service层中的方法,如下所示:
```
@RestController
@RequestMapping("/teachers")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@GetMapping
public PageInfo<Teacher> findTeachersByPage(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
return teacherService.findTeachersByPage(name, pageNum, pageSize);
}
@GetMapping("/count")
public int countTeachers(@RequestParam(value = "name", required = false) String name) {
return teacherService.countTeachers(name);
}
}
```
这样,就可以通过模糊查询教师名对教师表进行分页查询了。
阅读全文