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)文章
Java事務(wù)管理學(xué)習(xí)之JDBC詳解
這篇文章主要介紹了Java事務(wù)管理學(xué)習(xí)之JDBC的相關(guān)資料,文中介紹的非常詳細(xì),相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
深入學(xué)習(xí)Java 動(dòng)態(tài)代理
Java 動(dòng)態(tài)代理機(jī)制的出現(xiàn),使得 Java 開發(fā)人員不用手工編寫代理類,只要簡單地指定一組接口及委托類對象,便能動(dòng)態(tài)地獲得代理類。下面小編和大家來一起學(xué)習(xí)一下吧2019-05-05
Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享
在最近的工作開發(fā)之中,慢慢習(xí)慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個(gè)流,這篇文章主要給大家介紹了關(guān)于Java8中List轉(zhuǎn)Map(Collectors.toMap) 的相關(guān)資料,需要的朋友可以參考下2021-07-07
JVM執(zhí)行引擎和垃圾回收要點(diǎn)總結(jié)
不論是在問題現(xiàn)場還是跳槽面試,我們面對JVM性能問題,依舊會(huì)束手無辭,它需要你對Java虛擬機(jī)的實(shí)現(xiàn)和優(yōu)化,有極為深刻的理解。所以我在這里整理了一下 JVM的知識(shí)點(diǎn)。今天說說虛擬機(jī)執(zhí)行引擎和垃圾回收,都是十足的干貨,請各位看官耐心批閱!2021-06-06
Mybatis Plus 實(shí)現(xiàn)批量插入的示例代碼
本文主要介紹了Mybatis Plus 實(shí)現(xiàn)批量插入的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
解決java.sql.SQLException:?validateConnection?false問題的方法匯總(最
這篇文章主要給大家介紹了關(guān)于解決java.sql.SQLException:?validateConnection?false問題的方法匯總,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
如何在Spring Boot項(xiàng)目中使用Spring AI
Spring AI是Spring框架中用于集成和使用人工智能和機(jī)器學(xué)習(xí)功能的組件,它提供了一種簡化的方式來與AI模型進(jìn)行交互,這篇文章主要介紹了Spring Boot 在項(xiàng)目中使用Spring AI,需要的朋友可以參考下2024-05-05

