天天看点

pagehelper分页工具的使用

1.在pom.xml文件中添加依赖

在maven远程仓库找

https://mvnrepository.com/

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.6</version>
        </dependency>
           

2.在mybatis.xml文件中使用pagehelper插件

注意插入时,代码的位置

在MyBatis 的总体文件中配置插件

放到environments default="development标签之前

<plugins>  
      <!-- PageHelper4.1.6 -->   
      <plugin interceptor="com.github.pagehelper.PageHelper">  
           <property name="dialect" value="oracle"/>        
     </plugin>  
  </plugins>  
           

插件5.1以后interceptor不同,并且不需要指定数据库名字

<!--插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
           

测试类使用pagehelper

public static void main(String[] args) {

        SessionUtil su=new SessionUtil();
        SqlSession session= su.getsession();
        StudentDao dao= session.getMapper(StudentDao.class);
        //pageindex,pagesize
        PageHelper.startPage(5,5);//设置查看的页码和显示条数
        Map m=new HashMap();
        m.put("uname","a");
        List list=dao.findall(m);
        PageInfo p=new PageInfo(list);
        System.out.println("总条数:"+p.getTotal());
        System.out.println("总页数:"+p.getPages());
        System.out.println("当前页:"+p.getPageNum());
        System.out.println("上一页:"+p.getPrePage());
        System.out.println("下一页:"+p.getNextPage());
        List<Student> stus=p.getList();
        for (Student stu : stus) {
            System.out.println(stu.getUserid()+","+stu.getUser_name()+","+
                    stu.getAddress()+","+stu.getGrade().getGradename());
        }
     }