frame address: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
Reference documents address: https://blog.csdn.net/she_lock/article/details/79975907
- pom dependence
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
Using the above method in springboot is normal, but I don't know why it doesn't work
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>
- Add interceptors to mybatis's configuration file
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- To configure mybatis Cache, delay load and so on --> <settings> <!-- Global mapper enable caching --> <setting name="cacheEnabled" value="true" /> <!-- Turn off immediate loading of associated objects to improve performance when querying --> <setting name="lazyLoadingEnabled" value="true" /> <!-- Set the loading form of the associated object, here is the on-demand loading field(Load fields by SQL Appoint),All fields of the associated table are not loaded to improve performance --> <setting name="aggressiveLazyLoading" value="false" /> <!-- For unknown SQL Queries that allow different result sets to be returned for general purpose results --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- Allow column labels in place of column names --> <setting name="useColumnLabel" value="true" /> <!-- Allow custom primary key values(For example, generated by programs UUID 32 Bit code as key value),Data table PK Build policy will be overridden --> <!-- <setting name="useGeneratedKeys" value="true" /> --> <!-- Give nested resultMap In fields-Mapping support for properties --> <setting name="autoMappingBehavior" value="FULL" /> <!-- For batch update operation cache SQL To improve performance --> <setting name="defaultExecutorType" value="BATCH" /> <!-- Timeout if the database fails to respond for more than 25000 seconds --> <setting name="defaultStatementTimeout" value="25000" /> <!-- Print annotative in query statement production--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- In the configuration file, plugins location must meet the requirements as the following order: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- config params as the following --> <!-- <property name="param1" value="value1"/> --> </plugin> </plugins> </configuration>
- Configure a profile in a profile
# MyBatis configuration mybatis: mapperLocations:classpath:/mapper/*.xml configLocation:classpath:/mybatis-config.xml # Mybatis configuration (selected by the persistence layer framework) #mybatis.typeAliasesPackage=com.sinstar.cpmanage.bean mybatis.mapperLocations=classpath:mapper/*.xml
Or. yml file
# MyBatis configuration mybatis: mapperLocations: classpath*:/mapper/**/*.xml configLocation: classpath:/mybatis-config.xml
- Use in service
public List<Courseinfo> getCourseByPage(Integer rows, Integer currentPage) { PageHelper.startPage(currentPage, rows); List<Courseinfo> courseinfoList = null; courseinfoList=courseinfoMapper.getAllCourses(); return courseinfoList; }
In Controller
courseinfoList = courseService.getCourseByPage(rows, currentPage); PageInfo<Courseinfo> pageInfo = new PageInfo<>(courseinfoList);
The pageInfo obtained directly contains all the paging information. In mapper, sql statements are written as they were, without any change
This line of code will process sql statements through interceptors, so this framework is very easy to use. In PageInfo, some related values will be set automatically
PageHelper.startPage(currentPage, rows);
Return result example:
"data": { "pageNum": 1, "pageSize": 2, "size": 2, "startRow": 1, "endRow": 2, "total": 9, "pages": 5, "list": [ { "courseId": 1, "courceName": "College English", "type": "word", "srcUrl": "http://video.17yiqixiu.com/%E7%88%B1%E6%98%AF%E4%BA%BA%E9%97%B4%E6%9C%80%E4%BC%9F%E5%A4%A7%E7%9A%84%E5%8A%9B%E9%87%8F.mp4", "imageUrl": "http://img34.ddimg.cn/4/5/9198094-1_o.jpg", "createTime": 1519980188000, "author": "1", "description": "College English", "hasLearn": false, "hasCollect": false }, { "courseId": 13, "courceName": "Civil fabrication 22", "type": "Civil Engineering", "srcUrl": "http://video.17yiqixiu.com/%E7%88%B1%E6%98%AF%E4%BA%BA%E9%97%B4%E6%9C%80%E4%BC%9F%E5%A4%A7%E7%9A%84%E5%8A%9B%E9%87%8F.mp4", "imageUrl": "http://img34.ddimg.cn/4/5/9198094-1_o.jpg", "createTime": 1520326186000, "author": "1", "description": "sinstar", "hasLearn": false, "hasCollect": false } ], "prePage": 0, "nextPage": 2, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2, 3, 4, 5 ], "navigateFirstPage": 1, "navigateLastPage": 5, "firstPage": 1, "lastPage": 5 }