设为首页 加入收藏

TOP

[DB][MyBatis]利用mybatis-paginator实现分页(目前看到MyBatis下最好的分页实现)(一)
2015-07-24 11:58:13 来源: 作者: 【 】 浏览:17
Tags:MyBatis 利用 mybatis-paginator 实现 目前 看到 最好

利用mybatis-paginator实现分页

1、mybatis-paginator简介

mybatis-paginator是gethub上的一个开源项目、用于java后台获取分页数据、该开源项目还提供一个列表组件(mmgrid)用于前端展示。

该开源项目地址:https://github.com/miemiedev

2、该开源项目的使用说明:

Maven中加入依赖:


  ...
    
        com.github.miemiedev
        mybatis-paginator
        1.2.10
    
  ...

Mybatis配置文件添加分页插件:




    
        
            
        
     

创建一个查询,内容可以是任何Mybatis表达式,包括foreach和if等:


    select * from TEST_USER where city = #{city};

Dao中的方法或许是这样(用接口也是类似):

public List findByCity(String city, PageBounds pageBounds){
    Map params =new HashMap();
    params.put("city",city);
    returngetSqlSession().selectList("db.table.user.findByCity", params, pageBounds);
}

调用方式(分页加多列排序):

int page = 1; //页号
int pageSize = 20; //每页数据条数
String sortString = "age.asc,gender.desc";//如果你想排序的话逗号分隔可以排序多列

PageBounds pageBounds = newPageBounds(page, pageSize , Order.formString(sortString));

List list = findByCity("BeiJing",pageBounds);

//获得结果集条总数
PageList pageList = (PageList)list;
System.out.println("totalCount: "+ pageList.getPaginator().getTotalCount());

PageList类是继承于ArrayList的,这样Dao中就不用为了专门分页再多写一个方法。

使用PageBounds这个对象来控制结果的输出,常用的使用方式一般都可以通过构造函数来配置。

new PageBounds();//默认构造函数不提供分页,返回ArrayList
new PageBounds(int limit);//取TOPN操作,返回ArrayList
new PageBounds(Order... order);//只排序不分页,返回ArrayList
new PageBounds(int page, int limit);//默认分页,返回PageList
new PageBounds(int page, int limit, Order... order);//分页加排序,返回PageList
new PageBounds(int page, int limit, List orders,boolean containsTotalCount);//使用containsTotalCount来决定查不查询totalCount,即返回ArrayList还是PageList

=========================================

如果用的是Spring MVC的话可以把JSON的配置写成这样:

vc:annotation-driven>
    
        
                    
        
 
        
             
                 
             
         
     
 

那么在Controller就可以这样用了:

@ResponseBody
@RequestMapping(value ="/findByCity.json")
public List findByCity(@RequestParam String city,
                 @RequestParam(required =false,defaultValue ="1") intpage,
                 @RequestParam(required =false,defaultValue ="30") intlimit,
                 @RequestParam(required =false) String sort,
                 @RequestParam(required =false) String dir) {
 
    return userService.findByCity(city, newPageBounds(page, limit, Order.create(sort,dir)));
}

然后序列化后的JSON字符串就会变成这样的:
{
    "items":[
        {"NAME":"xiaoma","AGE":30,"GENDER":1,"ID":3,"CITY":"BeiJing"},
        {"NAME":"xiaoli","AGE":30,"SCORE":85,"GENDER":1,"ID":1,"CITY":"BeiJing"},
        {"NAME":"xiaowang","AGE":30,"SCORE":92,"GENDER":0,"ID":2,"CITY":"BeiJing"},
        {"NAME":"xiaoshao","AGE":30,"SCORE":99,"GENDER":0,"ID":4,"CITY":"BeiJing"}
    ],

    "slider": [1, 2, 3, 4, 5, 6, 7],
    "hasPrePage":false,
    "startRow": 1,
    "offset": 0,
    "lastPage":false,
    "prePage": 1,
    "hasNextPage":true,
    "nextPage": 2,
    "endRow": 30,
    "totalCount": 40351,
    "firstPage":true,
    "totalPages": 1346,
    "limit": 30,
    "page": 1
}

=========================================

在SpringMVC中使用JSTL的话可以参考一下步骤(懒人用法)

在Spring配置文件中加入拦截器,或则参考拦截器实现定义自己的拦截器


    
        
        
    

然后Controller方法可以这样写

@RequestMapping(value ="/userView.action")
public ModelAndView userView(@RequestParam String city,
                 @RequestParam(required =false,defaultValue ="1")intpage,
                 @RequestParam(required =false,defaultValue ="30")intlimit,
                 @RequestParam(required =false) String sort,
                 @RequestParam
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysql中insert与select的嵌套使用 下一篇11gR2RAC独占模式replacevotedisk..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)