When using MyBatis, the most headache is to write a page, you need to write a query count of the SELECT statement, and then write a real paging query, when the query condition is more, will find that really do not want to spend double time to write count and select, fortunately we have Pagehelper page plug-in, Pagehelper is a powerful and practical MyBatis page plug-in that can help us quickly implement paging functionality. So, let's have a taste of it next.
Add dependency
Add a paging plug-in dependency package within the Kitty-admin pom.xml file.
Pom.xml
<!--Pagehelper -<Dependency> <groupId>Com.github.pagehelper</groupId> <Artifactid>Pagehelper-spring-boot-starter</Artifactid> <version>1.2.5</version></Dependency>
Add configuration
Add a paging plug-in configuration within the KITTY-BOO/APPLICATION.YML configuration file.
Application.yml
# pagehelper pagehelper: helperdialect:mysql true True params:count=countsql
Pagination Code
First, add a paging lookup method to the DAO layer. Because our table has only a menu with more than one data, we choose to add a paging query interface to the menu.
Sysmenumapper.java
PackageCom.louis.kitty.admin.dao;Importjava.util.List;ImportCom.louis.kitty.admin.model.SysMenu; Public InterfaceSysmenumapper {intDeletebyprimarykey (Long menuId); intInsert (Sysmenu record); intinsertselective (Sysmenu record); Sysmenu Selectbyprimarykey (Long menuId); intupdatebyprimarykeyselective (Sysmenu record); intUpdatebyprimarykey (Sysmenu record); /*** Paging Query *@return */List<SysMenu>selectpage ();}
Add a query method to the Sysmenumapper.xml, which is a normal query to find all the records, do not need to write paging SQL, the paging plug-in will intercept the query request, and read the page query from the foreground of the paging parameter re-raw page query statement.
Sysmenumapper.xml
<select id= "Selectpage" resultmap= "Baseresultmap" > Select <include refid= "Base_ Column_list "/> from sys_menu </select>
The service layer calls the DAO layer to complete the paging query, which encapsulates the request and result classes of the paging query , This avoids the need to change the paging interface of the service layer and control layer because the ORM framework is replaced, and the replacement ORM framework does not affect the paging interface above the service layer, and it is useful to understand the decoupling.
Sysmenuservice.java
PackageCom.louis.kitty.admin.sevice;Importcom.louis.kitty.admin.page.PageRequest;ImportCom.louis.kitty.admin.page.PageResult; Public InterfaceSysmenuservice {/*** Paging Query interface * This encapsulates the paging request and results, avoiding direct introduction of specific frames of paging objects, such as MyBatis or JPA paging objects * To avoid situations where the service layer, the control layer's paging interface need to be changed because the ORM framework is replaced, and the ORM The framework does not * affect the paging interface above the service layer and play a role in understanding the decoupling *@paramPagerequest Custom, unified paging query Request *@returnPageresult Custom, Unified paged Query Results*/pageresult findpage (pagerequest pagerequest);}
The service implementation class calls the paging plug-in to complete the paging query, and the key code is Pagehelper.startpage (Pagenum, PageSize), the foreground paging query parameters are passed in.
Sysmenuserviceimpl.java
PackageCom.louis.kitty.admin.sevice.impl;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.github.pagehelper.PageHelper;ImportCom.github.pagehelper.PageInfo;ImportCom.louis.kitty.admin.dao.SysMenuMapper;ImportCom.louis.kitty.admin.model.SysMenu;Importcom.louis.kitty.admin.page.PageRequest;ImportCom.louis.kitty.admin.page.PageResult;Importcom.louis.kitty.admin.page.PageUtils;ImportCom.louis.kitty.admin.sevice.SysMenuService; @Service Public classSysmenuserviceimplImplementsSysmenuservice {@AutowiredPrivateSysmenumapper Sysmenumapper; @Override Publicpageresult findpage (pagerequest pagerequest) {returnPageutils.getpageresult (Pagerequest, GetPageInfo (pagerequest)); } /*** Call the paging plugin to complete the page pagination *@paramPagequery *@return */ PrivatePageinfo<sysmenu>GetPageInfo (pagerequest pagerequest) {intPagenum =Pagerequest.getpagenum (); intPageSize =pagerequest.getpagesize (); Pagehelper.startpage (Pagenum, pageSize); List<SysMenu> Sysmenus =Sysmenumapper.selectpage (); return NewPageinfo<sysmenu>(Sysmenus); }}
Pagerequest.java
PackageCom.louis.kitty.admin.page;/*** Paging Request*/ Public classPagerequest {/*** Current Page*/ Private intPagenum; /*** Quantity per page*/ Private intpageSize; Public intGetpagenum () {returnPagenum; } Public voidSetpagenum (intpagenum) { This. Pagenum =Pagenum; } Public intgetpagesize () {returnpageSize; } Public voidSetPageSize (intpageSize) { This. pageSize =pageSize; }}
Pageresult.java
PackageCom.louis.kitty.admin.page;Importjava.util.List;/*** Pagination returns results*/ Public classPageresult {/*** Current Page*/ Private intPagenum; /*** Quantity per page*/ Private intpageSize; /*** Total Records*/ Private Longtotalsize; /*** Total page number*/ Private intTotalPages; /*** Data Model*/ PrivateList<?>content; Public intGetpagenum () {returnPagenum; } Public voidSetpagenum (intpagenum) { This. Pagenum =Pagenum; } Public intgetpagesize () {returnpageSize; } Public voidSetPageSize (intpageSize) { This. pageSize =pageSize; } Public Longgettotalsize () {returntotalsize; } Public voidSettotalsize (Longtotalsize) { This. TotalSize =totalsize; } Public intgettotalpages () {returnTotalPages; } Public voidSettotalpages (inttotalpages) { This. TotalPages =TotalPages; } PublicList<?>getcontent () {returncontent; } Public voidSetContent (list<?>content) { This. Content =content; }}
Pageutils.java
PackageCom.louis.kitty.admin.page;ImportCom.github.pagehelper.PageInfo; Public classPageutils {/*** Encapsulates paging information into a unified interface *@paramPagerequest *@paramPage *@return */ Public StaticPageresult Getpageresult (pagerequest pagerequest, pageinfo<?>pageInfo) {Pageresult Pageresult=NewPageresult (); Pageresult.setpagenum (Pageinfo.getpagenum ()); Pageresult.setpagesize (Pageinfo.getpagesize ()); Pageresult.settotalsize (Pageinfo.gettotal ()); Pageresult.settotalpages (Pageinfo.getpages ()); Pageresult.setcontent (Pageinfo.getlist ()); returnPageresult; }}
Sysmenucontroller.java
PackageCom.louis.kitty.admin.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PostMapping;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;Importcom.louis.kitty.admin.page.PageRequest;ImportCom.louis.kitty.admin.sevice.SysMenuService, @RestController @requestmapping ("Menu") Public classSysmenucontroller {@AutowiredPrivateSysmenuservice Sysmenuservice; @PostMapping (Value= "/findpage") PublicObject findpage (@RequestBody pagerequest pagequery) {returnsysmenuservice.findpage (pagequery); }}
Interface Test
Launch the application, Access: localhost:8088/swagger-ui.html, find the corresponding interface, simulation test, the results are as follows.
Parameters: Pagenum:1, Pagesize:5
Test results
Parameters: Pagenum:2, Pagesize:5
Test results
Resources
https://pagehelper.github.io/
https://pagehelper.github.io/docs/howtouse/
SOURCE download
Code Cloud: Https://gitee.com/liuge1988/kitty
The rain recalls the light dust
Source: https://www.cnblogs.com/xifengxiaoma/
All rights reserved, welcome reprint, Reprint please indicate the original author and source.
Java Backend Management System (eight): MyBatis paging function implementation