利用mybatis-paginator實現分頁 1、mybatis-paginator簡介 mybatis-paginator是gethub上的一個開源項目、用于java后臺獲取分頁數據、該開源項目還提供一個列表組件(mmgrid)用于前端展示。 該開源項目地址:https://github.com/miemiedev 2、該開源項目的使
?
利用mybatis-paginator實現分頁
?
1、mybatis-paginator簡介
mybatis-paginator是gethub上的一個開源項目、用于java后臺獲取分頁數據、該開源項目還提供一個列表組件(mmgrid)用于前端展示。
該開源項目地址:https://github.com/miemiedev
?
2、該開源項目的使用說明:
Maven中加入依賴:
<dependencies>?? ??...?? ????<dependency>?? ????????<groupid>com.github.miemiedev</groupid>?? ????????<artifactid>mybatis-paginator</artifactid>?? ????????<version>1.2.10</version>?? ????</dependency>?? ??...?? </dependencies>
Mybatis配置文件添加分頁插件:??
<?xmlversionxmlversion ="1.0"encoding="UTF-8"?>?? nbsp;configuration??PUBLIC?"-//ibatis.apache.org//DTD?Config?3.0//EN"?? "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">?? <configuration>?? ????<plugins>?? ????????<plugin>?? ????????????<propertynamepropertyname></propertynamepropertyname>?? ????????</plugin>?? ?????</plugins>?? </configuration>
創建一個查詢,內容可以是任何Mybatis表達式,包括foreach和if等:
<selectidselectid>?? ????select?*?from?TEST_USER?where?city?=?#{city};?? </selectidselectid>
Dao中的方法或許是這樣(用接口也是類似):
public List findByCity(String city, PageBounds pageBounds){ Mapparams =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, Listorders,boolean containsTotalCount); //使用containsTotalCount來決定查不查詢totalCount,即返回ArrayList還是PageList
?
=========================================
如果用的是Spring MVC的話可以把JSON的配置寫成這樣:
<annotation-driven>?? ????<message-converters>?? ????????<bean>?? ????????????<constructor-argvalueconstructor-argvalue></constructor-argvalueconstructor-argvalue>?????????? ????????</bean>?? ??? ????????<bean>?? ?????????????<property>?? ?????????????<bean></bean>?? ?????????????</property>?? ?????????</bean>?? ?????</message-converters>?? ?</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配置文件中加入攔截器,或則參考攔截器實現定義自己的攔截器
<interceptors>?? ????<interceptor>?? ????????<mapping></mapping>?? ???????<bean></bean>?? ????</interceptor>?? </interceptors>
?
然后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(required =false) String dir) { List users = userService.findByCity(city,newPageBounds(page, limit, Order.create(sort,dir))); returnnewModelAndView("account/user","users", users); }
?
JSP中就可以這樣用了,攔截器會將PageList分拆添加Paginator屬性,默認命名規則為”原屬性名稱”+”Paginator”
${user[‘ID’]} | ${user[‘NAME’]} | ${user[‘AGE’]} |
?? ?? 上一頁:?${usersPaginator.prePage}??? 當前頁:?${usersPaginator.page}??? 下一頁:?${usersPaginator.nextPage}??? 總頁數:?${usersPaginator.totalPages}??? 總條數:?${usersPaginator.totalCount}??? ?? ?? 更多屬性參考Paginator類提供的方法
?
=========================================
如果用如下方法設置pageBounds,當前這個查詢就可以用兩個線程同時查詢list和totalCount了
pageBounds.setAsyncTotalCount(true);
如果所有的分頁查詢都是用異步的方式查詢list和totalCount,可以在插件配置加入asyncTotalCount屬性
<plugin>?? ????<property></property>?? ????<property></property>?? </plugin>
但是你仍然可以用下面代碼強制讓這個查詢不用異步
pageBounds.setAsyncTotalCount(false);
?
當然需要注意的是,只要你用到了異步查詢,由于里面使用了線程池,所以在使用時就要加入清理監聽器,以便在停止服務時關閉線程池。需要在web.xml中加入
<listener>?? ????<listener-class>com.github.miemiedev.mybatis.paginator.CleanupMybatisPaginatorListener</listener-class>?? </listener>
以上就是[DB][MyBatis]利用mybatis的內容,更多相關內容請關注PHP中文網(www.php.cn)!