Mybatis嵌套子查询动态SQL编写实践

 更新时间:2025年05月30日 14:25:30   作者:振宇i  
这篇文章主要介绍了Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

前言

Mybatis的xml文件编写动态SQL是从mapper中获取传入的参数,但是如果是嵌套的子查询中,子查询动态SQL所需的参数不能像常规的那样直接从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 = "创建时间")
    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 = "创建时间"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,  <!-- 将外部参数作为常量列 -->
        #{userId} as userId        <!-- 将外部参数作为常量列 -->
        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中需要传入子查询中的动态SQL参数,放到主查询的查询列表中去,取别名,别名即是传入到子查询中的动态SQL参数

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java事务管理学习之JDBC详解

    Java事务管理学习之JDBC详解

    这篇文章主要介绍了Java事务管理学习之JDBC的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • Spring cloud config 配置文件加密方式

    Spring cloud config 配置文件加密方式

    这篇文章给大家介绍了Spring cloud config 配置文件加密方式,非常不错,具有一定的参考借鉴价值,感兴趣的朋友跟随脚步之家小编一起学习吧
    2018-05-05
  • 深入学习Java 动态代理

    深入学习Java 动态代理

    Java 动态代理机制的出现,使得 Java 开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类。下面小编和大家来一起学习一下吧
    2019-05-05
  • springboot如何使用MybatisPlus

    springboot如何使用MybatisPlus

    MyBatisPlus是一个强大的数据库操作框架,其代码生成器可以快速生成实体类、映射文件等,本文介绍了如何导入MyBatisPlus相关依赖,创建代码生成器,并配置数据库信息以逆向生成代码,感兴趣的朋友跟随小编一起看看吧
    2024-09-09
  • Java8中List转Map(Collectors.toMap) 的技巧分享

    Java8中List转Map(Collectors.toMap) 的技巧分享

    在最近的工作开发之中,慢慢习惯了很多Java8中的Stream的用法,很方便而且也可以并行的去执行这个流,这篇文章主要给大家介绍了关于Java8中List转Map(Collectors.toMap) 的相关资料,需要的朋友可以参考下
    2021-07-07
  • JVM执行引擎和垃圾回收要点总结

    JVM执行引擎和垃圾回收要点总结

    不论是在问题现场还是跳槽面试,我们面对JVM性能问题,依旧会束手无辞,它需要你对Java虚拟机的实现和优化,有极为深刻的理解。所以我在这里整理了一下 JVM的知识点。今天说说虚拟机执行引擎和垃圾回收,都是十足的干货,请各位看官耐心批阅!
    2021-06-06
  • Mybatis Plus 实现批量插入的示例代码

    Mybatis Plus 实现批量插入的示例代码

    本文主要介绍了Mybatis Plus 实现批量插入的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 解决java.sql.SQLException: validateConnection false问题的方法汇总(最全)

    解决java.sql.SQLException: validateConnection false问题的方法汇总(最

    这篇文章主要给大家介绍了关于解决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框架中用于集成和使用人工智能和机器学习功能的组件,它提供了一种简化的方式来与AI模型进行交互,这篇文章主要介绍了Spring Boot 在项目中使用Spring AI,需要的朋友可以参考下
    2024-05-05

最新评论