MyBatis-Plus實(shí)現(xiàn)2種分頁方法(QueryWrapper查詢分頁和SQL查詢分頁)
1 MyBatisPlusConfig
MyBatisPlus配置類。
package com.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.*; /** * MyBatisPlus配置類 */ @Configuration public class MyBatisPlusConfig { /** * MyBatisPlus攔截器(用于分頁) */ @Bean public MybatisPlusInterceptor paginationInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //添加MySQL的分頁攔截器 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
2 UserPagination
用戶查詢條件類。
package com.entity; import lombok.Data; /** * 查詢條件 */ @Data public class UserPagination { /** * 當(dāng)前頁號 */ private int currentPage; /** * 每頁顯示條數(shù) */ private int pageSize; }
3 Mapper
3.1 UserMapper.java
package com.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.entity.UserEntity; import com.entity.UserPagination; import org.apache.ibatis.annotations.Mapper; /** * 用戶信息dao層 */ @Mapper public interface UserMapper extends BaseMapper<UserEntity> { /** * 獲取用戶信息(SQL查詢分頁) * * @param page 分頁條件 * @return */ Page<UserEntity> getUserListBySQLPage(Page<UserEntity> page); }
3.2 UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mapper.UserMapper"> <select id="getUserListBySQLPage" resultType="com.entity.UserEntity"> SELECT * from users </select> </mapper>
4 Service
4.1 UserService
package com.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.entity.*; public interface UserService extends IService<UserEntity> { /** * 獲取用戶信息(QueryWrapper查詢分頁) * * @param pagination 查詢條件 * @return */ Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination); /** * 獲取用戶信息(SQL查詢分頁) * * @param pagination 查詢條件 * @return */ Page<UserEntity> getUserListBySQLPage(UserPagination pagination); }
4.2 UserServiceImpl
package com.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.entity.*; import com.mapper.UserMapper; import com.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService { @Autowired private UserMapper userMapper; /** * 獲取用戶信息(QueryWrapper查詢分頁) * * @param pagination 查詢條件 * @return */ public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) { QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>(); Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); return this.page(page, queryWrapper); } /** * 獲取用戶信息(SQL查詢分頁) * * @param pagination 查詢條件 * @return */ @Override public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) { Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); return userMapper.getUserListBySQLPage(page); } }
5 UserController
調(diào)試代碼。
package com.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.entity.*; import com.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class UserController { @Autowired private UserService userService; /** * 獲取用戶信息(QueryWrapper查詢分頁) * * @return */ @GetMapping("/getUserListByQueryWrapperPage") public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) { return userService.getUserListByQueryWrapperPage(pagination); } /** * 獲取用戶信息(SQL查詢分頁) * * @return */ @GetMapping("/getUserListBySQLPage") public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) { return userService.getUserListBySQLPage(pagination); } }
6 調(diào)試結(jié)果
6.1 QueryWrapper查詢分頁
6.2 SQL查詢分頁
注:
更多MyBatis-Plus的配置請查看以下博客。
Spring Boot 配置MyBatis-Plus(實(shí)現(xiàn)查詢、新增、更新、刪除)
到此這篇關(guān)于MyBatis-Plus實(shí)現(xiàn)2種分頁方法(QueryWrapper查詢分頁和SQL查詢分頁)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用mybatis-plus分頁查詢無效的問題解決
- Mybatis-Plus 多表聯(lián)查分頁的實(shí)現(xiàn)代碼
- MyBatis-Plus 分頁查詢以及自定義sql分頁的實(shí)現(xiàn)
- MyBatis-Plus分頁插件不生效的解決方法
- 解決mybatis plus 一對多分頁查詢問題
- MyBatis-Plus實(shí)現(xiàn)分頁的方法使用詳解
- MyBatis-Plus分頁時排序的實(shí)現(xiàn)方法
- Mybatis-Plus如何使用分頁實(shí)例詳解
- Mybatis-plus原生pages分頁未生效的解決方案
- mybatis-plus分頁無效問題解決
相關(guān)文章
Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào)
這篇文章主要介紹了Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào),CompletableFuture是Java?8?中新增的一個類,它是對Future接口的擴(kuò)展,下文關(guān)于其更多相關(guān)詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04啟動 Eclipse 彈出 Failed to load the JNI shared library jvm.dll
這篇文章主要介紹了有時候,新電腦上回碰到打開Eclipse時,彈出提示“Failed to load the JNI shared library jvm.dll”錯誤,這里給大家分享解決方案2016-08-08Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解
這篇文章主要為大家介紹了Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09java并發(fā)編程synchronized底層實(shí)現(xiàn)原理
這篇文章主要介紹了java并發(fā)編程synchronized底層實(shí)現(xiàn)原理2022-02-02java異常處理機(jī)制示例(java拋出異常、捕獲、斷言)
這篇文章主要介紹了java異常處理機(jī)制示例(java拋出異常、捕獲、斷言),需要的朋友可以參考下2014-05-05Java中的日期時間類實(shí)例詳解(Date、Calendar、DateFormat)
在JDK1.0中,Date類是唯一的一個代表時間的類,但是由于Date類不便于實(shí)現(xiàn)國際化,所以從JDK1.1版本開始,推薦使用Calendar類進(jìn)行時間和日期處理,這篇文章主要介紹了Java中的日期時間類詳解(Date、Calendar、DateFormat),需要的朋友可以參考下2023-11-11Java中的main方法調(diào)用非靜態(tài)方法處理
這篇文章主要介紹了Java中的main方法調(diào)用非靜態(tài)方法處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06RxJava2.x+ReTrofit2.x多線程下載文件的示例代碼
本篇文章主要介紹了RxJava2.x+ReTrofit2.x多線程下載文件的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09