SpringBoot+MyBatis-Plus實現(xiàn)分頁示例
一、簡介
MyBatis-Plus官網:MyBatis-Plus ?? 為簡化開發(fā)而生
MyBatis-Plus是一種基于MyBatis框架的強大持久層增強工具,它在MyBatis的基礎上提供了許多便捷的功能和增強的特性,用于簡化開發(fā)。它是一個開源項目,可以與MyBatis無縫集成。
MyBatis-Plus提供了以下主要功能和特性:
- 簡化CRUD操作:提供了通用的Mapper接口和通用的Service接口,通過繼承這些接口可以省去大量的編碼工作。
- 代碼生成器:可以根據(jù)數(shù)據(jù)庫表結構自動生成實體類、Mapper接口和XML映射文件,減少手動編寫重復代碼的工作量。
- 條件構造器:提供了方便靈活的條件查詢封裝,可以通過鏈式調用的方式構建查詢條件。
- 分頁插件:提供了簡單易用的分頁查詢功能,支持多種數(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 中用于構建查詢條件的方法:
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); //分頁結果 List<Student> list = studentPage.getRecords(); System.out.println("總頁數(shù):" + studentPage.getPages()); System.out.println("總記錄數(shù):" + studentPage.getTotal()); list.forEach(System.out::println); }
(2)使用QueryWrapper
專門用于構造查詢條件,支持基本的等于、不等于、大于、小于等各種常見操作。它允許你以鏈式調用的方式添加多個查詢條件,并且可以組合使用 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); //分頁結果 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 表達式的查詢條件構造器,它通過 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); //分頁結果 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分頁內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot同時集成Mybatis和Mybatis-plus框架
- Springboot整合mybatis-plus使用pageHelper進行分頁(使用步驟)
- SpringBoot+MyBatis-Plus實現(xiàn)分頁的項目實踐
- springboot集成mybatis-plus全過程
- Springboot集成Mybatis-plus、ClickHouse實現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能
- springboot項目中mybatis-plus@Mapper注入失敗問題
- springboot+mybatis-plus實現(xiàn)自動建表的示例
- SpringBoot中使用MyBatis-Plus實現(xiàn)分頁接口的詳細教程
- SpringBoot?mybatis-plus使用json字段實戰(zhàn)指南
- springboot3.2整合mybatis-plus詳細代碼示例
- 全網最新springboot整合mybatis-plus的過程
相關文章
- 在這篇文章中給大家繼續(xù)講解包裝類的裝箱和拆箱問題。你可能會很好奇,做java開發(fā),怎么還裝起箱子來了?那么就請大家?guī)е苫笸驴窗?/div> 2023-04-04
在springboot中實現(xiàn)個別bean懶加載的操作
這篇文章主要介紹了在springboot中實現(xiàn)個別bean懶加載的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10SpringBoot+docker環(huán)境變量配置詳解
這篇文章主要介紹了SpringBoot+docker環(huán)境變量配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10SpringBoot 使用Mongo的GridFs實現(xiàn)分布式文件存儲操作
這篇文章主要介紹了Spring Boot 使用Mongo的GridFs實現(xiàn)分布式文件存儲操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10最新評論