之前写的项目都是在前端进行分页,最近涉及到后台分页查询,回看自己之前练习的项目里发现自己写了分页给忘了,作为初级程序员拿来记录一下
引入pagehelper的pom依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
书写pagehelper的配置类
/** * mybatis分页插件配置 * @author xWang * @Date 2019-07-12 */ @Configuration public class MyBatisConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum","true"); p.setProperty("rowBoundsWithCount","true"); p.setProperty("reasonable","true"); pageHelper.setProperties(p); return pageHelper; } }
mapper层
@Select("select * from video") List<Video> findAll();
service层没东西就不贴了,根据实际需要书写
controller层
@GetMapping("/page") public Object findAll(@RequestParam(value = "page",defaultValue = "1")int page, @RequestParam(value = "size",defaultValue = "10")int size){ PageHelper.startPage(page, size);//分页 List<Video> list = videoService.findAll(); PageInfo<Video> pageInfo = new PageInfo<Video>(list); Map<String,Object>data=new HashMap<>();//装载分页数据 data.put("toatl_page",pageInfo.getTotal());//总条数 data.put("toatl_size",pageInfo.getPages());//总页数 data.put("currrent_page",page);//当前页 data.put("toatl_page",pageInfo.getList());//数据 return data; }
这里需要注意传参调用pagehelper的方法语句
PageHelper.startPage(page, size);//分页 List<Video> list = videoService.findAll(); PageInfo<Video> pageInfo = new PageInfo<Video>(list);
致此,分页查询书写完成,步骤还是比较简单的,作为知识点记录一下,同时加深下印象,免得自己又忘了,哈哈
原文地址:https://www.cnblogs.com/xiaowangxiao/p/11334466.html
时间: 2024-10-02 21:38:05