1.ssm中mybatis的pagehelper中的分页
方式一:业务和是视图层分别处理(不建议)
serviceimpl
@Override
public PageInfo findAll(Integer page, Integer limit) {
PageHelper.startPage(page,limit);
List<Good> all = goodDao.findAll();
PageInfo<Good> of = PageInfo.of(all);
return of;
}
goodcontroller
@ResponseBody
@RequestMapping("/findAll")
public R findAll(int page,int limit){
// System.out.println(page);
// System.out.println(limit);
PageInfo all = goodService.findAll(page,limit);
R r = new R();
r.setCode(0);
r.setCount(all.getTotal());
r.setData(all.getList());
return r;
}
方式二:只在业务中进行处理
@Override
public R findAllCourse(Integer page,Integer limit) {
PageHelper.startPage(page,limit);
List<Course> allCourse = courseDao.findAllCourse();
PageInfo<Course> of = PageInfo.of(allCourse);
R r = new R();
r.setCode(0);//0代表成功
r.setMsg("查找成功");
r.setData(of.getList());
r.setCount(of.getTotal());
return r;
}
2.jpa中的分页
@Test
public void testFindAll(){
//分页 page从0开始,但是在pagehelp中page 是从1开始
PageRequest of = PageRequest.of(0, 1);
Page<Games> all = gamesRepository.findAll(of);
System.out.println(all.getTotalElements());
System.out.println(all.getContent());
}
jpa @Autowired UserRepository userRepository; @Override public R findAll(Integer page, Integer limit) { PageRequest of = PageRequest.of(page-1, limit); Page<TbUser> all = userRepository.findAll(of); R r = new R(); r.setCode(0);; r.setMsg("查询成功"); r.setCount(all.getTotalElements()); r.setData(all.getContent()); return r; }
mybatis @Override public R findAllTeacher(Integer page, Integer limit) { PageHelper.startPage(page,limit); List<Teacher> all = teacherDao.findAll(); PageInfo<Teacher> of = PageInfo.of(all); R r = new R(); r.setCode(0); r.setMsg("查找所有老师成功"); r.setCount(of.getTotal()); r.setData(of.getList()); return r; }