PageHelper preliminary use

PageHelper

1. Import dependencies

<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.10</version>
    </dependency>

2. Write a configuration file

Configured in the core configuration file of mybatis, pageHelper belongs to the plug-in

<!--配置PageHelper插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--老版本需要配置方言,新版本则不需要-->
            <!--<property name="dialect" value="mysql"/>-->
        </plugin>
    </plugins>

3. Test

@Test
    public void findAll(){
    
    
        //设置分页信息:pageNum:第几页  pageSize:每页条数
        //pageHelper.startPage();只能对下一个查询做分页,也就是不管中间隔了多少代码,只要遇见查询,就会分页
        //Preparing: SELECT count(0) FROM person
        //Preparing: SELECT id,p_name,p_age FROM person LIMIT ?
        PageHelper.startPage(2,3);
        List<Person> list = personDao.selectAll();
        //直接输出返回的list:Page{count=true, pageNum=2, pageSize=3, startRow=3, endRow=6, total=4, pages=2, reasonable=false, pageSizeZero=false}[Person(id=8, pName=哈哈, pAge=null)]
        //里面封装了一些分页数据(同我们自己封装的pageBean)
        System.out.println(list);
        //pageHelper帮我们把返回结果封装了,pageInfo里面的信息更全面
        PageInfo<Person> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
        /*for (Person person : list) {
            System.out.println(person);
        }*/
    }

End (be sure to note that PageHelper.startPage() is the next line to query the SQL to take effect!!!)

Guess you like

Origin blog.csdn.net/weixin_43431123/article/details/112158526