前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis中where标签和if标签结合使用说明

Mybatis中where标签和if标签结合使用说明

作者头像
鳄鱼儿
发布2024-05-21 17:16:24
980
发布2024-05-21 17:16:24
举报

由于不小心将and或者or写在了语句后面,导致mybatis无法自主判别,这种问题在新上手的同学中很是常见。下面我们探讨一下,在哪些情况下Mybatis无法判断动态SQL语句中的and或者or

使用<where>标签

select筛选出视图对象的参数,用于给前端返回页面参数使用。

代码语言:javascript
复制
	<sql id="selectFileVo">
        select file_id,
               uuid,
               file_name,
               file_url,
               status,
               create_time,
               update_time
        from file
    </sql>

以下代码格式是正确,我们先观察下and或者or的位置。

代码语言:javascript
复制
    <select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="fileName != null  and fileName != ''">
                and file_name like concat('%', #{fileName}, '%')
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
        </where>
    </select>

再看一下错误的写法;

代码语言:javascript
复制
    <select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="fileName != null  and fileName != ''">
                file_name like concat('%', #{fileName}, '%') and
            </if>
            <if test="status != null  and status != ''">
                status = #{status} and 
            </if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
        </where>
    </select>

这时候运行该代码,当beginCreateTimeendCreateTime为空时,我们会发现报错SQL执行异常,原因是where多了一个and

总结

<if>标签判断失败后, <where> 标签关键字可以自动去除掉库表字段赋值前面的and,不会去掉语句后面的and关键字,即<where> 标签只会去掉<if> 标签语句中的最开始的and关键字。所以上面的写法(and写在后面)是不符合mybatis规范的。

不使用<where>标签

当不使用<where>标签时,正确的写法可以参考以下代码:

代码语言:javascript
复制
<select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        where 1=1 
        <if test="fileName != null  and fileName != ''">
            and file_name like concat('%', #{fileName}, '%')
        </if>
        <if test="status != null  and status != ''">
            and status = #{status}
        </if>
        <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
            and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
        </if>
    </select>

此时我们发现and是写在前面的,同时增加了1=1条件。

如果我们去掉1=1条件,同时去掉第一个<if>标签的and

代码语言:javascript
复制
<select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        where 
        <if test="fileName != null  and fileName != ''">
            file_name like concat('%', #{fileName}, '%')
        </if>
        <if test="status != null  and status != ''">
            and status = #{status}
        </if>
        <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
            and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
        </if>
    </select>

这种情况下,当fileName为空时,sql语句中会出现where and这种错误的语法,最终导致sql执行异常。所以正确的代码中,使用1=1条件,当fileName为空时,sql语句就会变成where 1=1 ,后面接不接and都能正确执行。

在不使用<where>标签的情况下,and写在后面,在where条件最后增加1=1判断,原理和上面一样,这里就不再赘述了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用<where>标签
    • 总结
    • 不使用<where>标签
    相关产品与服务
    标签
    标签(Tag)是腾讯云推出的云资源管理工具,您可从不同维度对具有相同特征的云资源进行分类、搜索和聚合,从而轻松管理云上资源。 标签是由标签键和标签值共同组成,您可以为云资源创建和绑定标签
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档