广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis中where标签与if标签结合使用详细说明
  • 839
分享到

Mybatis中where标签与if标签结合使用详细说明

mybatis where标签mybatis wheremybatis if标签 2023-03-03 11:03:43 839人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录前言使用<where>标签总结不使用<where>标签总结前言 由于不小心将and或者or写在了语句后面,导致mybatis无法自主判

前言

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

使用<where>标签

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

	<sql id="selectFileVo">
        select file_id,
               uuid,
               file_name,
               file_url,
               status,
               create_time,
               update_time
        from file
    </sql>

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

    <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>

再看一下错误的写法;

    <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>标签时,正确的写法可以参考以下代码:

<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

<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判断,原理和上面一样,这里就不再赘述了。

总结

到此这篇关于Mybatis中where标签与if标签结合使用的文章就介绍到这了,更多相关Mybatis使用where标签与if标签内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis中where标签与if标签结合使用详细说明

本文链接: https://www.lsjlt.com/news/198331.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作