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

Mybatis嵌套子查詢動態(tài)SQL編寫實踐

 更新時間:2025年05月30日 14:25:30   作者:振宇i  
這篇文章主要介紹了Mybatis嵌套子查詢動態(tài)SQL編寫方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

前言

Mybatis的xml文件編寫動態(tài)SQL是從mapper中獲取傳入的參數(shù),但是如果是嵌套的子查詢中,子查詢動態(tài)SQL所需的參數(shù)不能像常規(guī)的那樣直接從mapper中獲取, 因為嵌套子查詢中能獲取的傳參僅能來源于主查詢中的結(jié)果,如下文所示,即如何去解決這一問題

一、實體類

主類

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import java.util.List;

@Schema(description = "返回結(jié)果實體 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class MainDataRespVO extends MainDataBaseVO {

    @Schema(description = "主鍵ID")
    private Long id;

    @Schema(description = "創(chuàng)建時間")
    private LocalDateTime createTime;

    @Schema(description = "子類詳情列表")
    private List<SubDataRespVO> subDataList;
}

子類

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;

@Schema(description = "管理后臺 - 子類實體信息 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SubDataRespVO extends SubDataBaseVO {

    @Schema(description = "主鍵ID")
    private Long subDataId;

    @Schema(description = "創(chuàng)建時間"D)
    private LocalDateTime createTime;
}

二、Mapper

List<MainDataRespVO> getMainDataList( @Param("localDateStart") String localDateStart,
                                            @Param("localDateEnd") String localDateEnd,
                                            @Param("shiftType") String shiftType,
                                            @Param("userId") Long userId);

三、XML

<?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="xxx.MainDataMapper">

    <resultMap id="selectShiftDateList" type="xxx.MainDataRespVO">
        <id property="id" column="id"/>
        <result property="workDate" column="work_date"/>
        <result property="createTime" column="create_time"/>
        <collection property="subDataList"
                    javaType="list"
                    ofType="xxx.vo.SubDataRespVO"
                    select="selectSubDataList"
                    column="{id=id, shiftType=shiftType, userId=userId}">
        </collection>
    </resultMap>

    <resultMap id="selectSubDataListMap" type="xxx.vo.SubDataRespVO">
        <result property="subDataId" column="id"/>
        <result property="createTime" column="create_time"/>
        <result property="userName" column="userName"/>
        <result property="shiftType" column="shift_type"/>
        <result property="userId" column="user_id"/>
        <result property="shiftDateId" column="shift_date_id"/>
    </resultMap>

    <select id="selectSubDataList" resultMap="selectSubDataListMap">
        select
        t2.id,
        t2.shift_date_id,
        t2.shift_type,
        t2.create_time,
        t2.user_id
        from sub_data t2
        where t2.main_data_id = #{id} and t2.deleted = 0
        <if test="shiftType!=null and shiftType != ''">
            and t2.shift_type = #{shiftType}
        </if>
        <if test="userId!=null and userId != ''">
            and t2.user_id =  #{userId}
        </if>
        order by t2.create_time asc
    </select>

    <select id="getMainDataList" resultMap="selectMainDataList">
        select
        t1.id,
        t1.work_date,
        t1.create_time,
        #{shiftType} as shiftType,  <!-- 將外部參數(shù)作為常量列 -->
        #{userId} as userId        <!-- 將外部參數(shù)作為常量列 -->
        from main_data t1
        where t1.deleted = 0
        <if test="localDateStart!=null and localDateStart != ''">
            and t1.work_date >=  #{localDateStart}
        </if>
        <if test="localDateEnd!=null and localDateEnd != ''">
            and #{localDateEnd} >= t1.work_date
        </if>
        order by t1.work_date asc
    </select>

</mapper>

四、詳解

如下圖所示,將mapper中需要傳入子查詢中的動態(tài)SQL參數(shù),放到主查詢的查詢列表中去,取別名,別名即是傳入到子查詢中的動態(tài)SQL參數(shù)

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    這篇文章主要介紹了Java事務(wù)管理學(xué)習(xí)之JDBC的相關(guān)資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Spring cloud config 配置文件加密方式

    Spring cloud config 配置文件加密方式

    這篇文章給大家介紹了Spring cloud config 配置文件加密方式,非常不錯,具有一定的參考借鑒價值,感興趣的朋友跟隨腳步之家小編一起學(xué)習(xí)吧
    2018-05-05
  • 深入學(xué)習(xí)Java 動態(tài)代理

    深入學(xué)習(xí)Java 動態(tài)代理

    Java 動態(tài)代理機制的出現(xiàn),使得 Java 開發(fā)人員不用手工編寫代理類,只要簡單地指定一組接口及委托類對象,便能動態(tài)地獲得代理類。下面小編和大家來一起學(xué)習(xí)一下吧
    2019-05-05
  • springboot如何使用MybatisPlus

    springboot如何使用MybatisPlus

    MyBatisPlus是一個強大的數(shù)據(jù)庫操作框架,其代碼生成器可以快速生成實體類、映射文件等,本文介紹了如何導(dǎo)入MyBatisPlus相關(guān)依賴,創(chuàng)建代碼生成器,并配置數(shù)據(jù)庫信息以逆向生成代碼,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享

    Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享

    在最近的工作開發(fā)之中,慢慢習(xí)慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個流,這篇文章主要給大家介紹了關(guān)于Java8中List轉(zhuǎn)Map(Collectors.toMap) 的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • JVM執(zhí)行引擎和垃圾回收要點總結(jié)

    JVM執(zhí)行引擎和垃圾回收要點總結(jié)

    不論是在問題現(xiàn)場還是跳槽面試,我們面對JVM性能問題,依舊會束手無辭,它需要你對Java虛擬機的實現(xiàn)和優(yōu)化,有極為深刻的理解。所以我在這里整理了一下 JVM的知識點。今天說說虛擬機執(zhí)行引擎和垃圾回收,都是十足的干貨,請各位看官耐心批閱!
    2021-06-06
  • Mybatis Plus 實現(xiàn)批量插入的示例代碼

    Mybatis Plus 實現(xiàn)批量插入的示例代碼

    本文主要介紹了Mybatis Plus 實現(xiàn)批量插入的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 解決java.sql.SQLException:?validateConnection?false問題的方法匯總(最全)

    解決java.sql.SQLException:?validateConnection?false問題的方法匯總(最

    這篇文章主要給大家介紹了關(guān)于解決java.sql.SQLException:?validateConnection?false問題的方法匯總,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • GitLab在IDEA中回滾主分支問題

    GitLab在IDEA中回滾主分支問題

    這是工作中遇到的問題,記錄下來,也方便自己后面查看操作步驟,也方便各位遇到這個問題,不至于卡太久,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 如何在Spring Boot項目中使用Spring AI

    如何在Spring Boot項目中使用Spring AI

    Spring AI是Spring框架中用于集成和使用人工智能和機器學(xué)習(xí)功能的組件,它提供了一種簡化的方式來與AI模型進行交互,這篇文章主要介紹了Spring Boot 在項目中使用Spring AI,需要的朋友可以參考下
    2024-05-05

最新評論