SpringBoot+MyBatis-Plus實現(xiàn)分頁示例
一、簡介
MyBatis-Plus官網(wǎng):MyBatis-Plus ?? 為簡化開發(fā)而生
MyBatis-Plus是一種基于MyBatis框架的強大持久層增強工具,它在MyBatis的基礎上提供了許多便捷的功能和增強的特性,用于簡化開發(fā)。它是一個開源項目,可以與MyBatis無縫集成。
MyBatis-Plus提供了以下主要功能和特性:
- 簡化CRUD操作:提供了通用的Mapper接口和通用的Service接口,通過繼承這些接口可以省去大量的編碼工作。
- 代碼生成器:可以根據(jù)數(shù)據(jù)庫表結(jié)構(gòu)自動生成實體類、Mapper接口和XML映射文件,減少手動編寫重復代碼的工作量。
- 條件構(gòu)造器:提供了方便靈活的條件查詢封裝,可以通過鏈式調(diào)用的方式構(gòu)建查詢條件。
- 分頁插件:提供了簡單易用的分頁查詢功能,支持多種數(shù)據(jù)庫。
- 樂觀鎖插件:為數(shù)據(jù)庫的樂觀鎖機制提供了便捷的支持。
- 自動填充插件:為實體類的字段自動填充值。
- SQL注入器:可以自定義SQL注入方法,增強SQL的靈活性。
總之,MyBatis-Plus是一個功能強大的持久層增強工具,可以大大簡化開發(fā),提高開發(fā)效率。

二、SpringBoot集成MyBatis-Plus
1.導入依賴包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>2.編寫配置文件
spring:
#配置數(shù)據(jù)源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/maven?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false
username: root
password: 123456
mybatis-plus:
configuration:
#開啟駝峰映射
map-underscore-to-camel-case: true
#開啟日志,方便觀察SQL執(zhí)行語句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.使用注解標識實體類
@TableName(value = "student")//對應數(shù)據(jù)庫表名 @TableId(type = IdType.AUTO)//對應數(shù)據(jù)庫主鍵,并設置類型為自增 @TableField(value = "id") //對應數(shù)據(jù)庫字段名 @TableField(exist = false)//設置表中不存在的字段 @TableLogic(value = "默認值",delval = "刪除后默認值")//邏輯刪除時用的字段 @Version//作用:在使用 MyBatis-Plus 樂觀鎖插件時使用
4.
MyBatis-Plus 中用于構(gòu)建查詢條件的方法:
1.eq 用于設置單個字段的相等條件。
2.allEq 通過一個 Map 來設置多個字段的相等條件
3.ne 設置單個字段的不相等條件。
4.gt 設置單個字段的大于條件。
5.ge 設置單個字段的大于等于條件。
6.lt 設置單個字段的小于條件。
7.le 設置單個字段的小于等于條件。
8.between 設置單個字段的 BETWEEN 條件。
9.notBetween 設置單個字段的 NOT BETWEEN 條件。
10.like 設置單個字段的 LIKE 條件。
三、分頁實現(xiàn)
1.首先設置分頁攔截器作為Spring管理的Bean
package com.apesource.spring_mybatis_plus_01.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 崔世博
* @version 1.0
* @since 2024/9/20
*/
@Configuration
public class PageConfig {
//注入mp攔截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//1.實例化攔截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2.添加分頁攔截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
2.Junit測試
(1)查找第一頁前三條數(shù)據(jù)
對應SQL語句:
SELECT stu_id,stu_name,nick_name,stu_age,is_delete FROM student WHERE is_delete=0 LIMIT 3
@Test
public void show6() {
//分頁
Page<Student> page = new Page<Student>();
//1.定義分頁規(guī)則
page.setSize(3); //頁面容量
page.setCurrent(1); //當前頁碼
Page<Student> studentPage = studentMapper.selectPage(page, null);
//分頁結(jié)果
List<Student> list = studentPage.getRecords();
System.out.println("總頁數(shù):" + studentPage.getPages());
System.out.println("總記錄數(shù):" + studentPage.getTotal());
list.forEach(System.out::println);
}(2)使用QueryWrapper

專門用于構(gòu)造查詢條件,支持基本的等于、不等于、大于、小于等各種常見操作。它允許你以鏈式調(diào)用的方式添加多個查詢條件,并且可以組合使用 and 和 or 邏輯。
例子:
找出student表中年齡等于20,分頁顯示第一頁的三條數(shù)據(jù)
對應SQL語句:
SELECT stu_id,stu_name,nick_name,stu_age,is_delete FROM student WHERE is_delete=0 AND (stu_age = 20) LIMIT 3
@Test
public void show6() {
//分頁
Page<Student> page = new Page<Student>();
//1.定義分頁規(guī)則
page.setSize(3); //頁面容量
page.setCurrent(1); //當前頁碼
//查詢條件(選填)
QueryWrapper<Student> qr = new QueryWrapper<Student>();
qr.eq("stu_age", 20);
Page<Student> studentPage = studentMapper.selectPage(page, qr);
//分頁結(jié)果
List<Student> list = studentPage.getRecords();
System.out.println("總頁數(shù):" + studentPage.getPages());
System.out.println("總記錄數(shù):" + studentPage.getTotal());
list.forEach(System.out::println);
}(3)LambdaQueryWrapper:
這是一個基于 Lambda 表達式的查詢條件構(gòu)造器,它通過 Lambda 表達式來引用實體類的屬性,從而避免了硬編碼字段名。這種方式提高了代碼的可讀性和可維護性,尤其是在字段名可能發(fā)生變化的情況下。
例子:
找出student表中年齡等于20,分頁顯示第一頁的三條數(shù)據(jù)
對應SQL語句:
SELECT stu_id,stu_name,nick_name,stu_age,is_delete FROM student WHERE is_delete=0 AND (stu_age > 18) LIMIT 3
@Test
public void show6() {
//分頁
Page<Student> page = new Page<Student>();
//1.定義分頁規(guī)則
page.setSize(3); //頁面容量
page.setCurrent(1); //當前頁碼
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.gt(Student::getStuAge, 18);
Page<Student> studentPage = studentMapper.selectPage(page, lambdaQueryWrapper);
//分頁結(jié)果
List<Student> list = studentPage.getRecords();
System.out.println("總頁數(shù):" + studentPage.getPages());
System.out.println("總記錄數(shù):" + studentPage.getTotal());
list.forEach(System.out::println);
}到此這篇關于SpringBoot+MyBatis-Plus實現(xiàn)分頁示例的文章就介紹到這了,更多相關SpringBoot MyBatis-Plus分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決微服務下Mybatis?xml無效綁定問題及分析Invalid?bound?statement
這篇文章主要介紹了解決微服務下Mybatis?xml無效綁定問題及分析Invalid?bound?statement,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例
本文主要介紹了SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
springboot啟動加載CommandLineRunner @PostConstruct問題
這篇文章主要介紹了springboot啟動加載CommandLineRunner @PostConstruct問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
DTO 實現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot AOP控制Redis自動緩存和更新的示例
今天小編就為大家分享一篇關于SpringBoot AOP控制Redis自動緩存和更新的示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
基于Mybatis實現(xiàn)動態(tài)數(shù)據(jù)源切換的示例代碼
在當今的互聯(lián)網(wǎng)應用中,微服務大行其道,隨著業(yè)務的發(fā)展和擴展,單一的數(shù)據(jù)庫無法滿足日益增長的數(shù)據(jù)需求,本文將基于 JDK17 + Spring Boot 3 和 MyBatis 框架實現(xiàn)動態(tài)切換數(shù)據(jù)源功能,需要的朋友可以參考下2024-09-09

