1. 程式人生 > >pagehelper分頁工具的使用

pagehelper分頁工具的使用

<!-- 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外掛 注意插入時,程式碼的位置

<!--外掛-->
    <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.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());
        }
     }