mybatis-paginator是gethub上的一个开源项目、用于java后台获取分页数据、该开源项目还提供一个列表组件(mmgrid)用于前端展示。
该开源项目地址:https://github.com/miemiedev
参考博客:http://blog.csdn.net/szwangdf/article/details/27859847
<dependencies> ... <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>1.2.10</version> </dependency> ... </dependencies>
<?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>
需要配置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 }
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类提供的方法