SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐
一、分頁的原理
要實現(xiàn)分頁功能方法有很多,但最基本的實現(xiàn)原理都差不多,所以在實現(xiàn)功能之前要先把原理搞明白。正如刪除有 “邏輯刪除” 和 “物理刪除" 之分,分頁也有 “邏輯分頁” 和 “物理分頁”;
1、邏輯分頁:邏輯分頁依賴于代碼。(Mybatis自帶的分頁插件就是邏輯分頁)
邏輯分頁是先查詢出所有的數(shù)據(jù)(只需要訪問一次數(shù)據(jù)庫),再根據(jù)代碼塊的所需 (你需要拿到第幾頁,每頁幾條的數(shù)據(jù)) 篩選出合適的數(shù)據(jù)進行分頁。
2、物理分頁:物理分頁依賴于數(shù)據(jù)庫。(更側(cè)重于sql語句)
MySQL數(shù)據(jù)庫提供的分頁關(guān)鍵字"LIMIT ",程序員只需要編寫帶有關(guān)鍵字的SQL語句,數(shù)據(jù)庫返回的數(shù)據(jù)就是分頁結(jié)果。(每次都要訪問數(shù)據(jù)庫,對數(shù)據(jù)庫造成的負擔大)
但我們一般不推薦使用邏輯分頁,而使用物理分頁較多,因為 “邏輯分頁一次性將所有的數(shù)據(jù)讀取至內(nèi)存中,占用了較大的內(nèi)存空間;物理分頁每次只讀取所需的數(shù)據(jù),占用內(nèi)存比較小” 。而在使用物理分頁的時候,就要考慮到limit的用法。
詳解Mybatis-Plus中分頁插件PaginationInterceptor
二、利用 mybatis-plus 自帶分頁插件Interceptor實現(xiàn)分頁功能
MyBatis-Plus 自帶的分頁插件 PaginationInterceptor 就是基于物理分頁實現(xiàn)的。該插件會攔截 SQL 執(zhí)行過程中的分頁參數(shù)(如 Page 對象),并根據(jù)傳入的分頁參數(shù)自動生成對應(yīng)的 LIMIT 或者 ROWS 等分頁語句,從而達到物理分頁的效果。同時,PaginationInterceptor 還支持多租戶和動態(tài)表名等功能。需要注意的是,如果查詢條件中包含了 GROUP BY 子句,物理分頁可能會出現(xiàn)問題,因為 LIMIT 或者 ROWS 可能會影響 GROUP BY 的結(jié)果。如果需要進行分組查詢,建議使用邏輯分頁或者在應(yīng)用層手動處理分頁結(jié)果。
下面演示按照 “性別” 進行分頁,以下是具體步驟:
1、在pom.xml文件中添加依賴:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
2、在配置文件application.properties或application.yml中進行PageHelper的配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true server: port: 9090 mybatis-plus: mapper-locations: classpath:mapper/*.xml # 控制臺打印sql語句 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3、創(chuàng)建測試數(shù)據(jù)庫表
create table user ( id int(20) auto_increment comment 'id', username varchar(20) null comment '用戶名', gender tinyint null comment '''0未知 1男 2女', password varchar(32) null comment '密碼', age int(4) null comment '年齡', create_time datetime null comment '創(chuàng)建時間', constraint user_pk primary key (id) ) comment '測試用戶表';
4、創(chuàng)建對應(yīng)測試實體類
@Data @TableName(value = "user") public class UserEntity{ /** * id */ @TableId private Long id; /** * 用戶名 */ private String username; /** * 密碼 */ private String password; /** * 0未知 1男 2女 */ private Integer gender; /** * 年齡 */ private Integer age; }
5、設(shè)置配置類
@Configuration public class MybatisPlusConfig{ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); paginationInnerInterceptor.setOptimizeJoin(true); paginationInnerInterceptor.setDbType(DbType.MYSQL); paginationInnerInterceptor.setOverflow(true); interceptor.addInnerInterceptor(paginationInnerInterceptor); OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor(); interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor); return interceptor; } }
6、 設(shè)置xml文件(針對按條件的復(fù)雜條件分頁情況)
<?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.example.demoutils.easyexcel.mapper.UserMapper"> <sql id = "entity"> a.id,a.username,a.password,a.gender,a.age,a.create_time </sql> <select id="selectUserByGender" resultType="com.example.demoutils.easyexcel.entity.UserEntity"> select <include refid="entity"/> from user a <where> <if test="gender != null"> and a.gender = #{gender} </if> </where> </select> </mapper>
7、設(shè)置mapper 文件
8、設(shè)置 service 層文件(不用再寫service實現(xiàn)層了,一個搞定,當然你分開寫也可以)
9、設(shè)置 controller 層文件
10、運行結(jié)果
三、需要注意的地方:
MyBatis-Plus 中的 Page 和 IPage 接口都是用于封裝分頁參數(shù)和返回分頁結(jié)果的。它們的主要區(qū)別在于 Page 是 IPage 的實現(xiàn)類,而 IPage 是一個更加抽象的分頁接口。
具體來說,IPage 接口是對分頁參數(shù)進行抽象的接口,它包含了以下幾個方法:
- getCurrent():獲取當前頁碼
- getSize():獲取每頁顯示的記錄數(shù)
- getTotal():獲取總記錄數(shù)
- getRecords():獲取當前頁的結(jié)果集
- setRecords(List<T> records):設(shè)置當前頁的結(jié)果集
- setTotal(long total):設(shè)置總記錄數(shù)
而 Page 類則是 IPage 接口的默認實現(xiàn)類,它的構(gòu)造函數(shù)可以傳入當前頁碼和每頁顯示的記錄數(shù)等參數(shù),并且還包含了一些方便進行物理分頁操作的方法,如:
- setAsc(String... ascs):設(shè)置按照哪些字段升序排列
- setDesc(String... descs):設(shè)置按照哪些字段降序排列
- convert(Function<? super T, ? extends U> mapper):轉(zhuǎn)換當前頁的結(jié)果集
- optimizeCountSql(boolean isOptimize):是否啟用 count sql 最優(yōu)化(默認 true)
- optimizeJoin(boolean isOptimize):是否為 join 語句 count 優(yōu)化(默認 false)
因此,如果只需要進行簡單的物理分頁操作,可以直接使用 Page 類,而如果需要對分頁參數(shù)進行更多的自定義操作,也可以使用 IPage 接口。
如剛才的例子換用 Page 接口 也是一樣的
到此這篇關(guān)于SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐的文章就介紹到這了,更多相關(guān)SpringBoot mybatis-plus分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring中的特殊注解@RequiredArgsConstructor詳解
這篇文章主要介紹了spring中的特殊注解@RequiredArgsConstructor,包括注解注入,構(gòu)造器注入及setter注入,結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-04-04如何解決java.lang.IllegalStateException: Target host&n
文章描述了通過MocoRunner模擬接口,并使用properties文件和ResourceBundle讀取配置文件進行g(shù)et請求的過程,在執(zhí)行過程中遇到了目標主機為空的錯誤,通過檢查和修正url拼接問題解決了該錯誤2024-12-12java 中 System.out.println()和System.out.write()的區(qū)別
這篇文章主要介紹了 java 中 System.out.println()和System.out.write()的區(qū)別.的相關(guān)資料,需要的朋友可以參考下2017-04-04MyBatis執(zhí)行批處理操作的實現(xiàn)示例
在MyBatis中,批處理操作是一種高效執(zhí)行多條語句的方式,下面就來介紹一下MyBatis執(zhí)行批處理操作的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-07-07詳解五種方式讓你在java中讀取properties文件內(nèi)容不再是難題
這篇文章主要介紹了詳解五種方式讓你在java中讀取properties文件內(nèi)容不再是難題 ,非常具有實用價值,需要的朋友可以參考下。2016-12-12