Mybatis Paginator分页
博客专区 > _Roger_ 的博客 > 博客详情
Mybatis Paginator分页
_Roger_ 发表于2年前
Mybatis Paginator分页
  • 发表于 2年前
  • 阅读 103
  • 收藏 0
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

摘要: mybatis分页插件,如何传递分页数据

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

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

参考博客:http://blog.csdn.net/szwangdf/article/details/27859847

1、maven添加依赖

<dependencies>
  ...
    <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-paginator</artifactId>
        <version>1.2.10</version>
    </dependency>
  ...
</dependencies>


2、mybatis-config.xml配置插件

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE configuration  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
            <propertyname="dialectClass"value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"/>
        </plugin>
     </plugins>
</configuration>


3、spring mvc传递分页的数据

3.1、使用Json

需要配置spring-mvc.xml

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-argvalue="UTF-8"/>        
        </bean>
 
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
             <property name="objectMapper">
                 <bean class="com.github.miemiedev.mybatis.paginator.jackson2.PageListJsonMapper"/>
             </property>
             <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
         </bean>
     </mvc:message-converters>
 </mvc:annotation-driven>


controller传递方式

(利用@ResponseBody注解传递Json数据,需要添加相关jar包:jackson-core-asl.jar和jackson-mapper-asl.jar)

@ResponseBody
@RequestMapping(value ="/findByCity")
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
}


3.2、在spring mvc的jsp中用JSTL取分页数据的值

spring-mvc.xml配置

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.github.miemiedev.mybatis.paginator.springmvc.PageListAttrHandlerInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>


controller方法传值方式

@RequestMapping(value ="/userView")
public ModelAndView userView(@RequestParam String city,
                 @RequestParam(required =false,defaultValue ="1")int page,
                 @RequestParam(required =false,defaultValue ="30")int limit,
                 @RequestParam(required =false) String sort,
                 @RequestParam(required =false) String dir) {
    List users = userService.findByCity(city,newPageBounds(page, limit, Order.create(sort,dir)));
    return newModelAndView("account/user","users", users);
}


jsp取值(拦截器会将PageList分拆添加Paginator属性,默认命名规则为"原属性名称"+"Paginator")

<table>
    <c:forEach items="${users}"var="user">
        <tr>
            <td>${user['ID']}</td>
            <td>${user['NAME']}</td>
            <td>${user['AGE']}</td>
        </tr>
    </c:forEach>
</table>

上一页: ${usersPaginator.prePage} 
当前页: ${usersPaginator.page} 
下一页: ${usersPaginator.nextPage} 
总页数: ${usersPaginator.totalPages} 
总条数: ${usersPaginator.totalCount} 


更多属性参考Paginator类提供的方法


共有 人打赏支持
粉丝 26
博文 82
码字总数 39915
×
_Roger_
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: