Examples of the Mybatis paging plug-in are described in detail.
Examples of Mybatis paging plug-ins
1. Preface:
We know that in MySQL, paging SQL is implemented using limit. If we write SQL by ourselves, there is certainly no problem with paging. However, once the model gets more complicated, we naturally think of using the reverse engineering of mybatis to generate the corresponding po and mapper, but it also brings disadvantages, for example, the paging problem is hard to solve.
Some people may say that I can modify the generated file. That's right. This is feasible, but we generally do not touch the file generated by Reverse Engineering. So at this time, you need to use the paging plug-in.
If you are using Mybatis, we recommend that you try this paging plug-in, which must be the most convenient to use.
This plug-in currently supports six database tabs: Oracle, Mysql, MariaDB, SQLite, Hsqldb, and PostgreSQL.
2. Usage
Step 1: configure the interceptor plug-in Mybatis configuration xml:
<Plugins> <! -- Com. github. pagehelper indicates the package name of the PageHelper class --> <plugin interceptor = "com. github. pagehelper. PageHelper"> <! -- Set the database type Oracle, Mysql, MariaDB, SQLite, Hsqldb, six PostgreSQL databases --> <property name = "dialect" value = "mysql"/> </plugin> </plugins>
Step 2: Use
1. Set paging information:
// Get the first page and 10 items. The total number of countPageHelper queries by default. startPage (1, 10); // The first select method that follows will be paged by List <Country> list = countryMapper. selectIf (1 );
2. Retrieve paging information
// After paging, the actual returned result list type is Page <E>. If you want to retrieve the paging information, you need to forcibly convert it to Page <E>, page <Country> listCountry = (Page <Country>) list; listCountry. getTotal ();
3. The second method for retrieving paging information
// Get the first page and 10 items. The total number of countPageHelper queries by default. startPage (1, 10); List <Country> list = countryMapper. selectAll (); // use PageInfo to wrap the result PageInfo page = new PageInfo (list ); // test all attributes of PageInfo // PageInfo contains a very comprehensive list of attributes assertEquals (1, page. getPageNum (); assertEquals (10, page. getPageSize (); assertEquals (1, page. getStartRow (); assertEquals (10, page. getEndRow (); assertEquals (183, page. getTotal (); assertEquals (19, page. getPages (); assertEquals (1, page. getFirstPage (); assertEquals (8, page. getLastPage (); assertEquals (true, page. isFirstPage (); assertEquals (false, page. isLastPage (); assertEquals (false, page. isHasPreviousPage (); assertEquals (true, page. isHasNextPage ());
3. TestPageHelper
@ Testpublic void testPageHelper () {// create a spring container ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("classpath: spring/applicationContext -*. xml "); // obtain the Mapper proxy object TbItemMapper mapper = applicationContext from the spring container. getBean (TbItemMapper. class); // execute the query and pagination TbItemExample example = new TbItemExample (); // PageHelper. startPage (2, 10); List <TbItem> list = mapper. selectByExample (example); // obtain the item list for (TbItem tbItem: list) {System. out. println (tbItem. getTitle ();} // retrieve the page information PageInfo <TbItem> pageInfo = new PageInfo <> (list); long total = pageInfo. getTotal (); System. out. println ("total products:" + total );}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!