Mybatis嵌套子查詢動(dòng)態(tài)SQL編寫實(shí)踐
前言
Mybatis的xml文件編寫動(dòng)態(tài)SQL是從mapper中獲取傳入的參數(shù),但是如果是嵌套的子查詢中,子查詢動(dòng)態(tài)SQL所需的參數(shù)不能像常規(guī)的那樣直接從mapper中獲取, 因?yàn)榍短鬃硬樵冎心塬@取的傳參僅能來源于主查詢中的結(jié)果,如下文所示,即如何去解決這一問題
一、實(shí)體類
主類
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "返回結(jié)果實(shí)體 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class MainDataRespVO extends MainDataBaseVO {
@Schema(description = "主鍵ID")
private Long id;
@Schema(description = "創(chuàng)建時(shí)間")
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 = "管理后臺(tái) - 子類實(shí)體信息 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SubDataRespVO extends SubDataBaseVO {
@Schema(description = "主鍵ID")
private Long subDataId;
@Schema(description = "創(chuàng)建時(shí)間"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中需要傳入子查詢中的動(dòng)態(tài)SQL參數(shù),放到主查詢的查詢列表中去,取別名,別名即是傳入到子查詢中的動(dòng)態(tài)SQL參數(shù)


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
feign的ribbon超時(shí)配置和hystrix的超時(shí)配置說明
這篇文章主要介紹了feign的ribbon超時(shí)配置和hystrix的超時(shí)配置說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
關(guān)于使用MyBatis簡(jiǎn)化JDBC開發(fā)和解決SQL語句警告的問題
這篇文章主要介紹了關(guān)于使用MyBatis簡(jiǎn)化JDBC開發(fā)和解決SQL語句警告的問題,如果idea和數(shù)據(jù)庫沒有建立鏈接,idea不識(shí)別表的信息,就會(huì)出現(xiàn)SQL語句的警告,需要的朋友可以參考下2023-05-05
SpringCloud Nacos作為配置中心超詳細(xì)講解
這篇文章主要介紹了Springcloud中的Nacos作為配置中心,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
SpringBoot實(shí)現(xiàn)六邊形架構(gòu)的三種不同方式詳解
六邊形架構(gòu),也被稱為端口與適配器架構(gòu)或洋蔥架構(gòu),是一種將業(yè)務(wù)邏輯與外部依賴解耦的架構(gòu)模式,本文將介紹在SpringBoot中實(shí)現(xiàn)六邊形架構(gòu)的三種不同方式2025-06-06
Java中RabbitMQ消息隊(duì)列的交換機(jī)詳解
這篇文章主要介紹了Java中的RabbitMQ交換機(jī)詳解,消息隊(duì)列是指利用高效可靠的消息傳遞機(jī)制進(jìn)行與平臺(tái)無關(guān)的數(shù)據(jù)交流,并基于數(shù)據(jù)通信來進(jìn)行分布式系統(tǒng)的集成,是在消息的傳輸過程中保存消息的容器,需要的朋友可以參考下2023-07-07
Spring Boot Gradle發(fā)布war到tomcat的方法示例
本篇文章主要介紹了Spring Boot Gradle發(fā)布war到tomcat的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
IntelliJ IDEA導(dǎo)入Gradle項(xiàng)目的方法
這篇文章主要介紹了IntelliJ IDEA導(dǎo)入Gradle項(xiàng)目的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java Collection 體系與使用場(chǎng)景多角度分析
本文從抽象層級(jí)、接口設(shè)計(jì)和實(shí)際使用場(chǎng)景三個(gè)角度,系統(tǒng)介紹了Java Collection框架,文章詳細(xì)說明了Collection的基本概念、Collection在體系中的位置、為什么要有Collection接口以及在不同場(chǎng)景下的使用建議,感興趣的朋友跟隨小編一起看看吧2026-01-01

