myBatis中传入的参数是map,map里面存了字符串也存了对象,* mapper.xml文件中的sql该怎么写

本文详细解析了在MyBatis框架中如何正确使用Map作为参数进行SQL动态拼接,包括时间戳和字符串的处理,强调了使用#与$的区别及重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)map中字符串的使用

        <if test="startInvokeStartTime != null">
and invoke_start_time >=
#{startInvokeStartTime,jdbcType=TIMESTAMP}
</if>
<if test="endInvokeStartTime != null">
    <![CDATA[ 
       and invoke_start_time <= #{endInvokeStartTime,jdbcType=TIMESTAMP}
     ]]>
</if>

(2)map对象的使用

<if test="interfaceInvokeLog != null">

    <if test="interfaceInvokeLog.title != null and interfaceInvokeLog.title != '' ">
        and title like concat('%',#{interfaceInvokeLog.title},'%')
</if>
<if
test="interfaceInvokeLog.startSys != null and interfaceInvokeLog.startSys != ''">
and start_sys like concat('%',#{interfaceInvokeLog.startSys},'%')
</if>
<if
test="interfaceInvokeLog.targetSys != null and interfaceInvokeLog.targetSys != '' ">
and target_sys like concat('%',#{targetSys},'%')
</if>
<if
test="interfaceInvokeLog.operatorMark != null and interfaceInvokeLog.operatorMark != '' ">
and operator_mark like
concat('%',#{interfaceInvokeLog.operatorMark},'%')
</if>
<if
test="interfaceInvokeLog.interfaceUrl != null and interfaceInvokeLog.interfaceUrl != '' ">
and interface_url like
concat('%',#{interfaceInvokeLog.interfaceUrl},'%')
</if>

</if>

说明:

(1)上文中标红处时map里面放的对象,用对象的属性值之前,首先要判断该对象是否存在,存在则继续判断对象的属性。

(2)这里的拼接一定要用#,而不是$

    #是将传入的值当作字符串的形式

   $是将传入的数据直接显示生成SQL语句

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值