欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot基于MyBatis-Plus實現(xiàn)Lambda Query查詢的示例代碼

 更新時間:2025年01月12日 08:41:41   作者:Andya  
MyBatis-Plus 是 MyBatis 的增強工具,簡化了數(shù)據(jù)庫操作,并提高了開發(fā)效率,它提供了多種查詢方式,包括常規(guī)的 SQL 查詢、Lambda Query 查詢、分頁查詢、條件查詢等,在本篇博客中,我們將詳細講解如何使用 MyBatis-Plus 的各種查詢方式,需要的朋友可以參考下

引言

MyBatis-Plus 是 MyBatis 的增強工具,簡化了數(shù)據(jù)庫操作,并提高了開發(fā)效率。它提供了多種查詢方式,包括常規(guī)的 SQL 查詢、Lambda Query 查詢、分頁查詢、條件查詢等。在本篇博客中,我們將詳細講解如何使用 MyBatis-Plus 的各種查詢方式,涵蓋以下內容:

  • 基礎環(huán)境配置
  • 表結構設計
  • 常見查詢方法
  • 普通查詢
  • Lambda 查詢
  • 條件構造器
  • 聚合查詢
  • 分頁查詢
  • 復雜查詢與多表聯(lián)查

基礎環(huán)境配置

首先,你需要確保項目已經引入了 MyBatis-Plus 相關的依賴。假設你的項目是基于 Spring Boot 的,下面是如何配置 Maven 依賴的示例。

依賴配置(Maven)

<dependencies>
    <!-- MyBatis-Plus Starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.0</version>
    </dependency>

    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>

    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

application.yml 配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  mybatis-plus:
    # 配置 MyBatis-Plus
    mapper-locations: classpath:/mappers/*.xml
    typeAliasesPackage: com.example.demo.entity

表結構設計

假設有以下兩個表:

  • demo_student:學生信息
  • demo_class:班級信息

demo_student 表結構

CREATE TABLE demo_student (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  age INT,
  class_id INT,
  gender ENUM('M', 'F') DEFAULT 'M',
  FOREIGN KEY (class_id) REFERENCES demo_class(id)
);

demo_student 表存儲學生信息,其中 class_id 是外鍵,指向 demo_class 表。

demo_class 表結構

CREATE TABLE demo_class (
  id INT AUTO_INCREMENT PRIMARY KEY,
  class_name VARCHAR(50) NOT NULL
);

常見查詢方法

普通查詢(selectOne, selectList, selectById)

  • selectOne:查詢單條記錄。
  • selectList:查詢多條記錄。
  • selectById:通過主鍵查詢一條記錄。
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DemoStudentService {

    @Autowired
    private DemoStudentMapper demoStudentMapper;

    // 查詢單個學生
    public DemoStudent getStudentById(Long id) {
        return demoStudentMapper.selectById(id);
    }

    // 查詢所有學生
    public List<DemoStudent> getAllStudents() {
        return demoStudentMapper.selectList(new QueryWrapper<>());
    }
}

Lambda Query 查詢(LambdaQueryWrapper)

MyBatis-Plus 提供了 LambdaQueryWrapper,可以通過 Lambda 表達式來避免字段名硬編碼。

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

public List<DemoStudent> getStudentsByAge(int age) {
    LambdaQueryWrapper<DemoStudent> queryWrapper = new LambdaQueryWrapper<>();
    // 查詢年齡大于指定值的學生
    queryWrapper.gt(DemoStudent::getAge, age);  
    return demoStudentMapper.selectList(queryWrapper);
}

條件構造器(QueryWrapper)

QueryWrapper 允許通過構建條件來查詢。它支持 eq、ne、lt、gt、like 等各種條件。

public List<DemoStudent> getStudentsByClassId(Long classId) {
    QueryWrapper<DemoStudent> queryWrapper = new QueryWrapper<>();
    // 根據(jù)班級ID查詢
    queryWrapper.eq("class_id", classId);  
    return demoStudentMapper.selectList(queryWrapper);
}

條件鏈式查詢

LambdaQueryWrapper 和QueryWrapper支持鏈式調用,可以將多個條件組合成一個查詢。

public List<DemoStudent> getFemaleStudentsOver18() {
    LambdaQueryWrapper<DemoStudent> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(DemoStudent::getGender, "F")
                .gt(DemoStudent::getAge, 18);  // 查找性別為女性,且年齡大于18歲的學生
    return demoStudentMapper.selectList(queryWrapper);
}

聚合查詢(selectCount, selectMax, selectMin 等)

MyBatis-Plus 支持基本的聚合查詢,比如統(tǒng)計、求最大值、最小值、平均值等。

// 查詢學生總數(shù)
public int getTotalStudents() {
    return demoStudentMapper.selectCount(new QueryWrapper<>());
}

// 查詢年齡最大值
public Integer getMaxAge() {
    return demoStudentMapper.selectMax(DemoStudent::getAge);
}

// 查詢年齡最小值
public Integer getMinAge() {
    return demoStudentMapper.selectMin(DemoStudent::getAge);
}

分頁查詢

分頁查詢在實際開發(fā)中非常常見,MyBatis-Plus 提供了內置的分頁功能,可以非常簡單地實現(xiàn)。

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public IPage<DemoStudent> getStudentsPage(int pageNum, int pageSize) {
    Page<DemoStudent> page = new Page<>(pageNum, pageSize);  // 創(chuàng)建分頁對象
    LambdaQueryWrapper<DemoStudent> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.gt(DemoStudent::getAge, 18);  // 查詢年齡大于18的學生
    return demoStudentMapper.selectPage(page, queryWrapper);  // 返回分頁結果
}

分頁插件配置

分頁功能需要在 application.yml 中配置分頁插件:

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  plugins:
    - com.baomidou.mybatisplus.extension.plugins.pagination.PageInterceptor

復雜查詢與多表聯(lián)查

在 MyBatis-Plus中,雖然不直接支持JOIN操作,但我們可以通過自定義 SQL 來實現(xiàn)復雜的聯(lián)表查詢。
示例:查詢學生和班級信息(聯(lián)查)

import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface DemoStudentMapper {
    
    @Select("SELECT s.id AS student_id, s.name AS student_name, c.class_name " +
            "FROM demo_student s LEFT JOIN demo_class c ON s.class_id = c.id")
    List<StudentWithClass> selectStudentsWithClass();
}

實現(xiàn)自定義查詢與復雜查詢

對于一些需要自定義 SQL 的場景,可以直接使用 @Select 或 @Update 注解來編寫 SQL。

@Select("SELECT * FROM demo_student WHERE age > #{age} AND gender = #{gender}")
List<DemoStudent> selectByAgeAndGender(int age, String gender);

總結

MyBatis-Plus提供了極為豐富的查詢功能,通過簡潔的 API 和靈活的查詢構造器,可以非常方便地進行數(shù)據(jù)庫查詢操作。通過以下幾點,可以更好地理解和應用 MyBatis-Plus 的查詢功能:

  • 普通查詢:通過selectOne, selectList, selectById方法可以快速進行數(shù)據(jù)查詢。
  • Lambda查詢:通過LambdaQueryWrapper構建條件查詢,避免硬編碼字段名,提高代碼可維護性。
  • 條件構造器:QueryWrapperLambdaQueryWrapper提供了多種條件構建方式,支持鏈式調用。
  • 分頁查詢:MyBatis-Plus提供了內置的分頁支持,可以輕松進行分頁查詢。
  • 聚合查詢:支持常見的聚合操作,如selectCount, selectMax, selectMin等。
  • 復雜查詢:通過自定義 SQL 支持聯(lián)表查詢,處理更復雜的查詢需求。

以上就是SpringBoot基于MyBatis-Plus實現(xiàn)Lambda Query查詢的示例代碼的詳細內容,更多關于MyBatis-Plus Lambda Query查詢的資料請關注腳本之家其它相關文章!

相關文章

  • JCrontab簡單入門實例詳解

    JCrontab簡單入門實例詳解

    這篇文章主要為大家詳細介紹了JCrontab簡單入門實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java實現(xiàn)網絡文件下載以及下載到指定目錄

    Java實現(xiàn)網絡文件下載以及下載到指定目錄

    在Spring框架中,StreamUtils和FileCopyUtils兩個工具類提供了方便的文件下載功能,它們都屬于org.springframework.util包,可以通過簡單的方法調用實現(xiàn)文件流的復制和下載,這些工具類支持多種參數(shù)傳遞,涵蓋了文件下載的多種場景
    2024-09-09
  • 解決RestTemplate第一次請求響應速度較慢的問題

    解決RestTemplate第一次請求響應速度較慢的問題

    這篇文章主要介紹了解決RestTemplate第一次請求響應速度較慢的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 將ResultSet中得到的一行或多行結果集封裝成對象的實例

    將ResultSet中得到的一行或多行結果集封裝成對象的實例

    這篇文章主要介紹了將ResultSet中得到的一行或多行結果集封裝成對象的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • IDEA項目如何取消git版本管控并添加svn版本控制

    IDEA項目如何取消git版本管控并添加svn版本控制

    在公司內部服務器環(huán)境下,將代碼倉庫從Gitee的Git遷移到SVN可以避免外部版本控制的風險,遷移過程中,先刪除項目的.git文件夾,再通過Eclipse的設置界面刪除原Git配置并添加SVN配置,之后,將項目提交到SVN倉庫,確保使用ignore列表過濾不必要的文件
    2024-10-10
  • 詳解springboot項目啟動時如何排除用不到的bean

    詳解springboot項目啟動時如何排除用不到的bean

    使用springboot開發(fā)項目,我們有時候會排除一些項目里面用不到的bean,不然的話項目啟動會報錯,這種情況通常是發(fā)生在什么場景里呢,以及如何解決呢,今天咱們就聊一聊
    2024-01-01
  • 流式圖表拒絕增刪改查之kafka核心消費邏輯上篇

    流式圖表拒絕增刪改查之kafka核心消費邏輯上篇

    這篇文章主要為大家介紹了流式圖表拒絕增刪改查之kafka核心消費邏輯詳解的上篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Springboot 限制IP訪問指定的網址實現(xiàn)

    Springboot 限制IP訪問指定的網址實現(xiàn)

    本文主要介紹了Springboot 限制IP訪問指定的網址實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • 詳解SpringBoot中的統(tǒng)一異常處理

    詳解SpringBoot中的統(tǒng)一異常處理

    這篇文章主要介紹了詳解SpringBoot中的統(tǒng)一異常處理,在獨立的某個地方,比如單獨一個類,定義一套對各種異常的處理機制,需要的朋友可以參考下
    2023-05-05
  • java 實現(xiàn)回調代碼實例

    java 實現(xiàn)回調代碼實例

    本文主要介紹Java的回調機制,并附實例代碼以供大家參考學習,有需要的小伙伴可以看下
    2016-07-07

最新評論