Use mybatis-paginator to implement paging 1. mybatis-paginator introduction mybatis-paginator is an open-source project on gethub. It is used to obtain paging data in the java background. This open-source project also provides a list component (mmgrid ).) used for front-end display. Address of this open-source project: github. commiemiedev2
Use mybatis-paginator to implement paging 1. mybatis-paginator introduction mybatis-paginator is an open-source project on gethub. It is used to obtain paging data in the java background. This open-source project also provides a list component (mmgrid ).) used for front-end display. The open source project address: https://github.com/miemiedev 2, the open source project
Use mybatis-paginator to implement Paging
1. Introduction to mybatis-paginator
Mybatis-paginator is an open-source project on gethub. It is used to obtain paging data in the java background. This open-source project also provides a list component (mmgrid) for front-end display.
The open source project address: https://github.com/miemiedev
2. Instructions for using this open-source project:
Add dependency to Maven:
...
com.github.miemiedev
mybatis-paginator
1.2.10
...
Add a paging plug-in to the Mybatis configuration file:
Create a query with any Mybatis expression, including foreach and if:
select * from TEST_USER where city = #{city};
The method in Dao may be like this (the interface is also similar ):
public List findByCity(String city, PageBounds pageBounds){ Mapparams =new HashMap(); params.put("city",city); returngetSqlSession().selectList("db.table.user.findByCity", params, pageBounds);}
Call method (paging and multi-column sorting ):
Int page = 1; // page number int pageSize = 20; // number of data entries per page String sortString = "age. asc, gender. desc "; // If You Want To sort multiple columns, separate them by commas (,). PageBounds pageBounds = newPageBounds (page, pageSize, Order. formString (sortString); List list = findByCity ("BeiJing", pageBounds); // retrieve the total number of results set PageList pageList = (PageList) list; System. out. println ("totalCount:" + pageList. getPaginator (). getTotalCount ());
The PageList class is inherited from ArrayList, so that Dao does not need to write more methods for special paging.
The PageBounds object is used to control the output of results. Common usage methods can be configured through constructors.
New PageBounds (); // The default constructor does not provide pagination and returns the ArrayListnew PageBounds (int limit); // returns the ArrayListnew PageBounds (Order... order); // sorting only without paging, return ArrayListnew PageBounds (int page, int limit); // default page, return PageListnew PageBounds (int page, int limit, Order... order); // sort by page and return PageListnew PageBounds (int page, int limit, Listorders, boolean containsTotalCount); // use containsTotalCount to determine whether to query totalCount, returns the ArrayList or PageList.
========================================================== =
If Spring MVC is used, you can write the JSON configuration as follows:
You can use it in the Controller as follows:
@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)));}
Then the serialized JSON string will become like this:
{ "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}
========================================================== =
To use JSTL in SpringMVC, refer to the following steps (lazy usage)
Add an interceptor to the Spring configuration file, or refer to the interceptor implementation to define your own interceptor.
Then the Controller method can be written like this
@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);}
This can be used in JSP. the interceptor will split PageList and add the Paginator attribute. The default naming rule is "original attribute name" + "Paginator"
$ {User ['id']} |
$ {User ['name']} |
$ {User ['age']} |
Previous Page: $ {usersPaginator. prePage} current page: $ {usersPaginator. page} next page: $ {usersPaginator. nextPage} total number of pages: $ {usersPaginator. total totalPages}: $ {usersPaginator. totalCount} for more attributes, refer to the method provided by the Paginator class.
========================================================== =
If you use the following method to set pageBounds, you can use two threads to query both list and totalCount.
pageBounds.setAsyncTotalCount(true);
If all paging queries use the asynchronous query list and totalCount method, you can add the asyncTotalCount attribute to the plug-in configuration.
However, you can still use the following code to force this query to be asynchronous.
pageBounds.setAsyncTotalCount(false);
Of course, it should be noted that, as long as you use Asynchronous query, because the thread pool is used in it, you need to add the cleaning listener when using it, so as to close the thread pool when the service is stopped. Must be added to web. xml
com.github.miemiedev.mybatis.paginator.CleanupMybatisPaginatorListener
The preceding figure shows the use of MyBatis in [DB] [mybatis]. For more information, see the PHP Chinese website (www.php1.cn )!