Mybatis嵌套子查詢動態(tài)SQL編寫實踐
前言
Mybatis的xml文件編寫動態(tài)SQL是從mapper中獲取傳入的參數(shù),但是如果是嵌套的子查詢中,子查詢動態(tài)SQL所需的參數(shù)不能像常規(guī)的那樣直接從mapper中獲取, 因為嵌套子查詢中能獲取的傳參僅能來源于主查詢中的結果,如下文所示,即如何去解決這一問題
一、實體類
主類
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "返回結果實體 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ù)


總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
feign的ribbon超時配置和hystrix的超時配置說明
這篇文章主要介紹了feign的ribbon超時配置和hystrix的超時配置說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
關于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題
這篇文章主要介紹了關于使用MyBatis簡化JDBC開發(fā)和解決SQL語句警告的問題,如果idea和數(shù)據(jù)庫沒有建立鏈接,idea不識別表的信息,就會出現(xiàn)SQL語句的警告,需要的朋友可以參考下2023-05-05
SpringBoot實現(xiàn)六邊形架構的三種不同方式詳解
六邊形架構,也被稱為端口與適配器架構或洋蔥架構,是一種將業(yè)務邏輯與外部依賴解耦的架構模式,本文將介紹在SpringBoot中實現(xiàn)六邊形架構的三種不同方式2025-06-06
Spring Boot Gradle發(fā)布war到tomcat的方法示例
本篇文章主要介紹了Spring Boot Gradle發(fā)布war到tomcat的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

