MyBatis-Plus實(shí)現(xiàn)2種分頁(yè)方法(QueryWrapper查詢分頁(yè)和SQL查詢分頁(yè))
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攔截器(用于分頁(yè))
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加MySQL的分頁(yè)攔截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2 UserPagination
用戶查詢條件類。
package com.entity;
import lombok.Data;
/**
* 查詢條件
*/
@Data
public class UserPagination {
/**
* 當(dāng)前頁(yè)號(hào)
*/
private int currentPage;
/**
* 每頁(yè)顯示條數(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查詢分頁(yè))
*
* @param page 分頁(yè)條件
* @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查詢分頁(yè))
*
* @param pagination 查詢條件
* @return
*/
Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination);
/**
* 獲取用戶信息(SQL查詢分頁(yè))
*
* @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查詢分頁(yè))
*
* @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查詢分頁(yè))
*
* @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查詢分頁(yè))
*
* @return
*/
@GetMapping("/getUserListByQueryWrapperPage")
public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
return userService.getUserListByQueryWrapperPage(pagination);
}
/**
* 獲取用戶信息(SQL查詢分頁(yè))
*
* @return
*/
@GetMapping("/getUserListBySQLPage")
public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) {
return userService.getUserListBySQLPage(pagination);
}
}
6 調(diào)試結(jié)果
6.1 QueryWrapper查詢分頁(yè)

6.2 SQL查詢分頁(yè)

注:
更多MyBatis-Plus的配置請(qǐng)查看以下博客。
Spring Boot 配置MyBatis-Plus(實(shí)現(xiàn)查詢、新增、更新、刪除)
到此這篇關(guān)于MyBatis-Plus實(shí)現(xiàn)2種分頁(yè)方法(QueryWrapper查詢分頁(yè)和SQL查詢分頁(yè))的文章就介紹到這了,更多相關(guān)MyBatis-Plus 分頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用mybatis-plus分頁(yè)查詢無(wú)效的問(wèn)題解決
- Mybatis-Plus 多表聯(lián)查分頁(yè)的實(shí)現(xiàn)代碼
- MyBatis-Plus 分頁(yè)查詢以及自定義sql分頁(yè)的實(shí)現(xiàn)
- MyBatis-Plus分頁(yè)插件不生效的解決方法
- 解決mybatis plus 一對(duì)多分頁(yè)查詢問(wèn)題
- MyBatis-Plus實(shí)現(xiàn)分頁(yè)的方法使用詳解
- MyBatis-Plus分頁(yè)時(shí)排序的實(shí)現(xiàn)方法
- Mybatis-Plus如何使用分頁(yè)實(shí)例詳解
- Mybatis-plus原生pages分頁(yè)未生效的解決方案
- mybatis-plus分頁(yè)無(wú)效問(wèn)題解決
相關(guān)文章
Java訪問(wèn)者模式實(shí)現(xiàn)優(yōu)雅的對(duì)象結(jié)構(gòu)處理
Java訪問(wèn)者模式是一種行為型設(shè)計(jì)模式,它通過(guò)將數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)操作分離,實(shí)現(xiàn)對(duì)復(fù)雜對(duì)象結(jié)構(gòu)的處理。它將數(shù)據(jù)結(jié)構(gòu)中的每個(gè)元素都轉(zhuǎn)換為訪問(wèn)者能夠識(shí)別的形式,從而使得數(shù)據(jù)操作可以在不影響數(shù)據(jù)結(jié)構(gòu)的前提下進(jìn)行擴(kuò)展和變化2023-04-04
springboot項(xiàng)目中配置redis詳細(xì)的教程
Redis是一種高性能的鍵值存儲(chǔ)數(shù)據(jù)庫(kù),而Spring Boot是一個(gè)簡(jiǎn)化了開(kāi)發(fā)過(guò)程的Java框架,這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目中配置redis詳細(xì)的教程,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
Java經(jīng)驗(yàn)點(diǎn)滴:處理沒(méi)有被捕獲的異常
Java經(jīng)驗(yàn)點(diǎn)滴:處理沒(méi)有被捕獲的異常...2006-12-12
完整詳解Java開(kāi)發(fā)學(xué)習(xí)路線指南
在本篇文章里小編給大家整理的是一篇關(guān)于Java開(kāi)發(fā)學(xué)習(xí)路線以及期中的主要知識(shí)點(diǎn)內(nèi)容,有興趣的朋友么可以學(xué)習(xí)下。2022-11-11
SpringCloud?LoadBalancerClient?負(fù)載均衡原理解析
LoadBalancerClient?是?SpringCloud?提供的一種負(fù)載均衡客戶端,Ribbon?負(fù)載均衡組件內(nèi)部也是集成了?LoadBalancerClient?來(lái)實(shí)現(xiàn)負(fù)載均衡,本文給大家深入解析?LoadBalancerClient?接口源碼,感興趣的朋友跟隨小編一起看看吧2022-02-02
利用keytools為tomcat 7配置ssl雙向認(rèn)證的方法
雙向認(rèn)證和單向認(rèn)證原理基本差不多,只是除了客戶端需要認(rèn)證服務(wù)端以外,增加了服務(wù)端對(duì)客戶端的認(rèn)證,下面這篇文章主要介紹了利用keytools為tomcat 7配置ssl雙向認(rèn)證的方法,需要的朋友可以借鑒,下面來(lái)一起看看吧。2017-02-02
Java面試題沖刺第十二天--數(shù)據(jù)庫(kù)(2)
這篇文章主要為大家分享了最有價(jià)值的三道數(shù)據(jù)庫(kù)面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07

