mybatis where 标签使用

 更新时间:2022年03月03日 11:09:24   作者:潘甜甜!  
where标记的作用类似于动态sql中的set标记,本文主要介绍了mybatis where 标签使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

我们经常在动态构造sql时,为防止注入或防止语句不当时会使用where 1=1

<select id="selectGroupByEmployeeNum" resultMap="BaseResultMap" parameterType="com.dao.impl.ZcChatGroup">
    select
        *
    from 
        zc_chat_group 
    WHERE 1=1
    <if test="id!=null">
        id= #{id} 
    </if>
    <if test="leaderNum!=null">
        and leader_num = #{leaderNum} 
    </if>
    <if test="groupType!=null">
        and group_type = #{groupType} 
    </if>
  </select>

 但在使用where标签可以简化这条语句

<select id="selectGroupByEmployeeNum" resultMap="BaseResultMap" parameterType="com.dao.impl.ZcChatGroup">
    select
        *
    from 
        zc_chat_group 
    <where>
        <if test="id!=null">
            id= #{id} 
        </if>
        <if test="leaderNum!=null">
            and leader_num = #{leaderNum} 
        </if>
        <if test="groupType!=null">
            and group_type = #{groupType} 
        </if>
    </where>
  </select>

这条sql执行时,如果id这个参数为null,则这条语句的执行结果为

select * from zc_chat_group where leader_num = ‘xx' and group_type = ‘xx'

这个‘where’标签会知道如果它包含的标签中有返回值的话,它就会插入一个‘where’。此外,如果标签返回的内容是以AND 或OR开头的,则会把它去除掉。

Mybatis where标签的使用

为了能达到MySQL性能的调优,我们可以基于Mybatis的where标签来进行实现。where标签是顶层的遍历标签,需要配合if标签使用,单独使用无意义。通常有下面两种实现形式。

方式一:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

方式二:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

仔细观察会发现,这两种方式的区别在于第一if条件中的SQL语句是否有and。

这里就涉及到where标签的两个特性:

  • 第一,只有if标签有内容的情况下才会插入where子句;
  • 第二,若子句的开通为 “AND” 或 “OR”,where标签会将它替换去除;

所以说,上面的两种写法都是可以了,Mybatis的where标签会替我们做一些事情。
但需要注意的是:where标签只会 智能的去除(忽略)首个满足条件语句的前缀。所以建议在使用where标签时,每个语句都最好写上 and 前缀或者 or 前缀,否则像以下写法就会出现问题:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        id_no = #{idNo}
      </if>
    </where>
  </select>

生成的SQL语句如下:

select * from t_user      WHERE username = ?  id_no = ?

很显然,语法是错误的。
因此,在使用where标签时,建议将所有条件都添加上and或or;

进阶:自定义trim标签

上面使用where标签可以达到拼接条件语句时,自动去掉首个条件的and或or,那么如果是其他自定义的关键字是否也能去掉呢?
此时,where标签就无能为力了,该trim标签上场了,它也可以实现where标签的功能。

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <trim prefix="where" prefixOverrides="and | or ">
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </trim>
  </select>

将上面基于where标签的写改写为trim标签,发现执行效果完全一样。而且trim标签具有了更加灵活的自定义性。

where语句的坑

另外,在使用where语句或其他语句时一定要注意一个地方,那就是:注释的使用。
先来看例子:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        /* and id_no = #{idNo}*/
        and id_no = #{idNo}
      </if>
    </where>
  </select>

上述SQL语句中添加了 /**/的注释,生成的SQL语句为:

select * from t_user WHERE username = ? /* and id_no = ?*/ and id_no = ? 

执行时,直接报错。

还有一个示例:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        -- and username = #{username}
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

生成的SQL语句为:

select * from t_user WHERE -- and username = ? and username = ? and id_no = ? 

同样会导致报错。

这是因为我们使用 XML 方式配置 SQL 时,如果在 where 标签之后添加了注释,那么当有子元素满足条件时,除了 < !-- --> 注释会被 where 忽略解析以外,其它注释例如 // 或 /**/ 或 -- 等都会被 where 当成首个子句元素处理,导致后续真正的首个 AND 子句元素或 OR 子句元素没能被成功替换掉前缀,从而引起语法错误。
同时,个人在实践中也经常发现因为在XML中使用注释不当导致SQL语法错误或执行出错误的结果。强烈建议,非必要,不要在XML中注释掉SQL,可以通过版本管理工具来追溯历史记录和修改。

小结

本文基于Mybatis中where标签的使用,展开讲了它的使用方式、特性以及拓展到trim标签的替代作用,同时,也提到了在使用时可能会出现的坑。内容虽然简单,但如果能够很好地实践、避免踩坑也是能力的体现。

到此这篇关于mybatis where 标签使用的文章就介绍到这了,更多相关mybatis where标签内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 从零开始让你的Spring Boot项目跑在Linux服务器

    从零开始让你的Spring Boot项目跑在Linux服务器

    这篇文章主要给大家介绍了如何从零开始让你的Spring Boot项目跑在Linux服务器的相关资料,由于springboot是内嵌了tomcat,所以可以直接将项目打包上传至服务器上,需要的朋友可以参考下
    2021-11-11
  • Java编程实现服务器端支持断点续传的方法(可支持快车、迅雷)

    Java编程实现服务器端支持断点续传的方法(可支持快车、迅雷)

    这篇文章主要介绍了Java编程实现服务器端支持断点续传的方法,涉及Java文件传输的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • 网络爬虫案例解析

    网络爬虫案例解析

    本文主要介绍了网络爬虫的小案例。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • java 实现DES 加密解密的示例

    java 实现DES 加密解密的示例

    这篇文章主要介绍了java 实现DES 加密解密的示例代码,帮助大家更好的理解和使用Java进行加解密,感兴趣的朋友可以了解下
    2020-12-12
  • Spring bean注册到容器的总结

    Spring bean注册到容器的总结

    这篇文章主要介绍了Spring bean注册到容器的总结,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-12-12
  • Java中将File转化为MultipartFile的操作

    Java中将File转化为MultipartFile的操作

    这篇文章主要介绍了Java中将File转化为MultipartFile的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Spring bean的实例化和IOC依赖注入详解

    Spring bean的实例化和IOC依赖注入详解

    这篇文章主要介绍了Spring bean的实例化和IOC依赖注入详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Java使用BouncyCastle加密

    Java使用BouncyCastle加密

    本文主要介绍了Java使用BouncyCastle加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • springboot+camunda实现工作流的流程分析

    springboot+camunda实现工作流的流程分析

    Camunda是基于Java语言,支持BPMN标准的工作流和流程自动化框架,并且还支持CMMN规范,DMN规范,本文给大家介绍springboot+camunda实现工作流的流程分析,感兴趣的朋友一起看看吧
    2021-12-12
  • SpringBoot项目中连接SQL Server的三种方式

    SpringBoot项目中连接SQL Server的三种方式

    连接SQL Server是许多Spring Boot项目中常见的需求之一,本文主要介绍了SpringBoot项目中连接SQL Server的三种方式,具有一定的参考价值 ,感兴趣的可以了解一下
    2023-09-09

最新评论