In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of the Mybatis paging plug-in, which is very detailed and has a certain reference value. Friends who are interested must read it!
An example of Mybatis paging plug-in
1. Foreword:
We know that in MySQL, paging sql is done using limit, and if we write sql ourselves, there must be no problem with paging. But once the model becomes more and more complex, we naturally want to use the reverse engineering of mybatis to generate the corresponding po and mapper, but it will also bring disadvantages, such as the paging problem here is not easy to solve.
Some people may say, I can modify the generated file, yes, this is feasible, but usually the files generated by reverse engineering will not touch it, so at this time, we need to use paging plug-ins to solve the problem.
If you are also using Mybatis, it is recommended to try the paging plug-in, which must be the most convenient to use.
The plug-in currently supports Oracle,Mysql,MariaDB,SQLite,Hsqldb and PostgreSQL database paging.
two。 Usage
Step 1: configure the interceptor plug-in in the Mybatis configuration xml:
Step 2: use in your code
1. Set paging information:
/ / get page 1, 10 items, the default total number of queries countPageHelper.startPage (1,10); / / the first select method that follows will be paged List list = countryMapper.selectIf (1)
2. Fetch paging information
/ / after paging, the actual returned result list type is Page. If you want to retrieve the paging information, you need to cast it to Page,Page listCountry = (Page) list;listCountry.getTotal ()
3. The second method of getting paging information
/ / get page 1, 10 items, default total number of queries countPageHelper.startPage (1,10); List list = countryMapper.selectAll (); / / wrap the results in PageInfo PageInfo page = new PageInfo (list); / / Test all properties of PageInfo / / PageInfo contains very comprehensive paging properties assertEquals (1, page.getPageNum (); assertEquals (10, page.getPageSize ()); assertEquals (1, page.getStartRow ()); assertEquals (10, page.getEndRow () AssertEquals (1883, 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"); / / get the Mapper proxy object TbItemMapper mapper = applicationContext.getBean (TbItemMapper.class) from the spring container; / / execute the query and page TbItemExample example = new TbItemExample (); / / paginate PageHelper.startPage (2,10); List list = mapper.selectByExample (example) / / fetch product list for (TbItem tbItem: list) {System.out.println (tbItem.getTitle ());} / fetch paging information PageInfo pageInfo = new PageInfo (list); long total = pageInfo.getTotal (); System.out.println ("Common goods:" + total);} above is all the content of the article "sample Analysis of Mybatis paging plug-in". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.