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ù)庫表結構自動生成實體類、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實現(xiàn)分頁示例
- Springboot整合mybatis-plus使用pageHelper進行分頁(使用步驟)
- 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詳細代碼示例
- 全網(wǎng)最新springboot整合mybatis-plus的過程
相關文章
Spring 單元測試中如何進行 mock的實現(xiàn)
這篇文章主要介紹了Spring 單元測試中如何進行 mock的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Java 數(shù)據(jù)結構之時間復雜度與空間復雜度詳解
算法復雜度分為時間復雜度和空間復雜度。其作用: 時間復雜度是度量算法執(zhí)行的時間長短;而空間復雜度是度量算法所需存儲空間的大小2021-11-11Java項目開啟遠程調試的方法步驟(tomcat、springboot)
這篇文章主要介紹了Java項目開啟遠程調試的方法步驟(tomcat、springboot),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10Spring?Boot?整合RocketMq實現(xiàn)消息過濾功能
這篇文章主要介紹了Spring?Boot?整合RocketMq實現(xiàn)消息過濾,本文講解了RocketMQ實現(xiàn)消息過濾,針對不同的業(yè)務場景選擇合適的方案即可,需要的朋友可以參考下2022-06-06使用Post方式提交數(shù)據(jù)到Tomcat服務器的方法
這篇將介紹使用Post方式提交數(shù)據(jù)到服務器,由于Post的方式和Get方式創(chuàng)建Web工程是一模一樣的,只用幾個地方的代碼不同,這篇文章主要介紹了使用Post方式提交數(shù)據(jù)到Tomcat服務器的方法,感興趣的朋友一起學習吧2016-04-04IDEA教程創(chuàng)建SpringBoot前后端分離項目示例圖解
在使用spring、mybatis等框架時,配置文件很復雜,有時復雜的讓人想放棄Java,使用C#。springboot出現(xiàn)這一切問題就都不是問題2021-10-10