-
目录
引入介绍
回顾MySQL的limit实现简易分页
问题的产生与解决
分页插件实现步骤
实例演示
分页插件相关参数
实例演示
-
引入介绍
- 分页可以将很多条结果进行分页显示
- 如果当前在第一页,则没有上一页
- 如果当前在最后一页,则没有下一页
- 需要明确当前是第几页,这一页中显示多少条结果
-
回顾MySQL的limit实现简易分页
- 可以用limit进行
- 而limit是mysql数据库特有的,其他数据库中没有
-
问题的产生与解决
- 在企业级开发中,分页也是一种常见的技术
- 而目前使用的Mybatis是不带分页功能的
- 如果想实现分页的功能,需要我们手动编写limit语句
- 但是不同的数据库实现分页的SQL语句也是不同的
- 所以手写分页成本较高,这个时候就可以借助分页插件来帮助我们实现分页功能
- PageHelper
- 第三方分页助手,将复杂的分页操作进行封装,从而让分页功能变得非常简单
-
分页插件实现步骤
- 1.导入jar包
- 2.在核心配置文件中集成分页助手插件
- 3.在测试类中使用分页助手相关API实现分页功能
-
实例演示

-
<!--集成分页助手插件--><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"/></plugins> 



-
@Testpublic void selectplugin() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSessionSqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//第一页,显示1条数据//PageHelper.startPage(1,1);//第二页,显示1条数据PageHelper.startPage(2,1);List<Student> list = mapper.selectAll();for(Student student : list){System.out.println(student);}sqlSession.close();is.close();} -
分页插件相关参数
- PageInfo
- 封装分页相关参数的功能类
- 核心方法
- getTotal()
- 获取总条数
- 返回值:long
- getPages()
- 获取总页数
- 返回值:int
- getPageNum()
- 获取当前页
- 返回值:int
- getPageSize()
- 获取每页显示条数
- 返回值:int
- getPrePage()
- 获取上一页
- 返回值:int
- getNextPage()
- 获取下一页
- 返回值:int
- isIsFirstPage()
- 获取是否是第一页
- 返回值:boolean
- isIsLastPage()
- 获取是否是最后一页
- 返回值:boolean
-
实例演示


-
@Testpublic void selectplugin() throws Exception{//1.加载核心配置文件InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");//2.获取SqlSession工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通过工厂对象获取SqlSessionSqlSession sqlSession = sqlSessionFactory.openSession(true);//4.获取StudentMapper接口的实现类对象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//第一页,显示1条数据//PageHelper.startPage(1,1);//第二页,显示1条数据PageHelper.startPage(2,1);List<Student> list = mapper.selectAll();for(Student student : list){System.out.println(student);}//获取分页相关参数PageInfo<Student> info = new PageInfo<>(list);System.out.println("总条数:"+info.getTotal());System.out.println("总页数:"+info.getPages());System.out.println("当前页:"+info.getPageNum());System.out.println("每页显示条数:"+info.getPageSize());System.out.println("上一页:"+info.getPrePage());System.out.println("下一页:"+info.getNextPage());System.out.println("是否是第一页:"+info.isIsFirstPage());System.out.println("是否是最后一页:"+info.isIsLastPage());sqlSession.close();is.close();}







![[一起学习pytorch吧]之torch.sign函数](https://img-blog.csdnimg.cn/20200316211101489.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21pc3N5b3VkYWlzeQ==,size_16,color_FFFFFF,t_70)










