ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Mybatis 常用条件语句,大于小于、if、for、模糊搜索、case when、choose

2026/9/22 22:14:27 拓冰建站 浏览量
Mybatis 常用条件语句,大于小于、if、for、模糊搜索、case when、choose

大于小于

方法1: > 大于 , < 小于

<if test="startTime != null ">and a.create_time &gt;= #{startTime}
</if>
<if test="endTime != null ">and a.create_time &lt;= #{endTime}
</if>

方法2(建议写这个): 

在idea输入CD应该就自动出来了

if 条件判断 

<if test="deviceId != null and deviceId != '' ">and a.device_id = #{deviceId}
</if>

for循环

<if test="partsIdList != null ">and b.id in<foreach item="item" index="index" collection="partsIdList" open="(" separator="," close=")">#{item}</foreach>
</if>

LIKE 模糊搜索

<if test='phone!=null and phone!=""' >where b.phone LIKE concat('%',#{phone},'%')</if>
<if test="dataRole == 0">AND pn.org_code LIKE CONCAT(#{orgCode},'%')
</if>

case when

choose选择语句

满足when条件走when,否则执行otherwise 

<choose><when test="dto!=null and dto.dataRole='0'">AND i.org_code like concat(#{dto.orgCode},'%')</when><otherwise>AND i.org_code = #{dto.orgCode}</otherwise>
</choose>

 

------------------------------------------与正文内容无关------------------------------------
如果觉的文章写对各位读者老爷们有帮助的话,麻烦点赞加关注呗!小弟在这拜谢了!
如果您觉得我的文章在某些地方写的不尽人意或者写的不对,从而让你对你人生观产生颠覆(概不负责),需要斧正,麻烦在评论区不吝赐教,作者看到后会根据您的提示查阅文章进行修改,还这世间一个公理一片蓝天