1. 程式人生 > >pageHelper的使用步驟,省略sql語句中的limit

pageHelper的使用步驟,省略sql語句中的limit

1.引架包。注意版本問題

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

2.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>

3.在service層返回值型別是pageInfo,

4.在實現類中

@Override
    public PageInfo getall(String classname, String classnum, int pageindex, int size,int[] ids,String classstate) {
        //封裝模糊查條件
        Map map =new HashMap();
        map.put("classname",classname);
        map.put(
"classnum",classnum); map.put("ids",ids); map.put("classstate",classstate); //使用PageInfo工具實現分頁,1、導架包2、配置mybatis.xml PageHelper.startPage(pageindex,size);//在執行查詢之前設定 List list = classesMapper.getall(map); //將插入的集合存在PageInfo分頁工具中 PageInfo pi = new PageInfo(list); return pi; }

5.獲得其他分頁的資訊

 System.out.println("總條數="+pi.getTotal());  
         System.out.println("總頁數="+pi.getPages());  
         System.out.println("下一頁="+pi.getNextPage());  
         System.out.println("當前頁碼="+pi.getPageNum());  
         System.out.println("每頁顯示條數:"+pi.getPageSize());