Mybatis分页插件 - PageHelper

it2022-05-05  117

首先把PageHelper依赖的jar包添加到工程中

在Mybatis的全局文件中配置SqlMapConfig.xml中配置拦截器插件:

<plugins>     <!-- com.github.pagehelper为PageHelper类所在包名 -->     <plugin interceptor="com.github.pagehelper.PageHelper">         <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库->                 <property name="dialect" value="mysql"/>     </plugin> </plugins>

注意:紧跟着分页信息PageHelper.startPage(1, 5),的第一个select方法才会被分页

代码如下:

@Test public void testPageHelper(){ //初始化spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-dao.xml"); //获取mapper的代理对象 TbItemMapper mapper = context.getBean(TbItemMapper.class); //设置分页信息 PageHelper.startPage(1, 5);//从第1条记录开始,5行 TbItemExample e = new TbItemExample(); //注:紧跟着分页信息的第一个查询才会被分页 List<TbItem> list1 = mapper.selectByExample(e); List<TbItem> list2 = mapper.selectByExample(e); System.out.println("第一个分页的list的集合长度"+list1.size()); System.out.println("第二个分页的list的集合长度"+list2.size()); System.out.println("============================"); //PageInfo 获取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list1); System.out.println("list1的总记录数为:"+pageInfo.getTotal()); System.out.println("list1的所有页数为:"+pageInfo.getPages()); System.out.println("list1当前页数为:"+pageInfo.getPageNum()); System.out.println("list1每页显示的记录数为:"+pageInfo.getPageSize()); System.out.println("============================"); PageInfo<TbItem> pageInfo2 = new PageInfo<TbItem>(list2); System.out.println("list2的总记录数为:"+pageInfo2.getTotal()); System.out.println("list2的所有页数为:"+pageInfo2.getPages()); System.out.println("list2当前页数为:"+pageInfo2.getPageNum()); System.out.println("list2每页显示的记录数为:"+pageInfo2.getPageSize()); }

如果设置一个分页信息PageHelper.startPage(1, 5);,紧跟着分页信息的第一条select语句才会被分页, 

 

如果再次设置分页信息PageHelper.startPage(2, 10);,则紧跟的select语句也会被分页

 

@Test public void testPageHelper(){ //初始化spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-dao.xml"); //获取mapper的代理对象 TbItemMapper mapper = context.getBean(TbItemMapper.class); //设置分页信息 PageHelper.startPage(1, 5);//从第1条记录开始,5行 TbItemExample e = new TbItemExample(); //注:紧跟着分页信息的第一个查询才会被分页 List<TbItem> list1 = mapper.selectByExample(e); //再次设置分页信息,紧跟的第一个select也会被分页 PageHelper.startPage(2, 10);//从第1条记录开始,5行 TbItemExample e2 = new TbItemExample(); List<TbItem> list2 = mapper.selectByExample(e2); System.out.println("第一个分页的list的集合长度"+list1.size()); System.out.println("第二个分页的list的集合长度"+list2.size()); System.out.println("============================"); //PageInfo 获取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list1); System.out.println("list1的总记录数为:"+pageInfo.getTotal()); System.out.println("list1的所有页数为:"+pageInfo.getPages()); System.out.println("list1当前页数为:"+pageInfo.getPageNum()); System.out.println("list1每页显示的记录数为:"+pageInfo.getPageSize()); System.out.println("============================"); PageInfo<TbItem> pageInfo2 = new PageInfo<TbItem>(list2); System.out.println("list2的总记录数为:"+pageInfo2.getTotal()); System.out.println("list2的所有页数为:"+pageInfo2.getPages()); System.out.println("list2当前页数为:"+pageInfo2.getPageNum()); System.out.println("list2每页显示的记录数为:"+pageInfo2.getPageSize()); }

 

 


最新回复(0)