mybatis主查询给子查询传递参数

本文探讨了在SQL查询中如何将主查询的参数传递给子查询,利用CASE-WHEN语句处理可能的NULL值,并在ResultMap中定义映射规则,以便在Java应用中正确处理查询结果。这种方法对于处理复杂查询和多层数据结构尤其有用。
摘要由CSDN通过智能技术生成

当需要将主查询的参数传递给子查询时可以加虚拟列映射
在sql 里 加上 case when (#{selectTagId} IS NULL) then NULL else #{selectTagId} end as selectTagId

<resultMap type="KlbDocument" id="KlbDocumentResult">
        <id     property="documentId"       column="document_id"  />
        <result property="docName"       column="doc_Name"   />
        <collection property="tags" javaType="java.util.ArrayList" ofType="KlbDocTag"
                    select="com.chinaunicom.system.mapper.base.KlbDocumentMapper.selectTagList" column="{documentId=document_id,selectTagId=selectTagId" />
        <collection property="roles" javaType="java.util.ArrayList" ofType="KlbDocRole"
                    select="com.chinaunicom.system.mapper.base.KlbDocumentMapper.selectRoleList" column="{documentId=document_id" />
    </resultMap>


 SELECT d.*,case when (#{selectTagId} IS NULL) then NULL else #{selectTagId} end as selectTagId 
 from ........
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值