mybatis的where标签怎么使用

发布时间:2022-03-03 14:09:15 作者:iii
来源:亿速云 阅读:193

本篇内容主要讲解“mybatis的where标签怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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'

这个&lsquo;where&rsquo;标签会知道如果它包含的标签中有返回值的话,它就会插入一个&lsquo;where&rsquo;。此外,如果标签返回的内容是以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标签的两个特性:

所以说,上面的两种写法都是可以了,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标签怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. where子句的使用
  2. 怎么在mybatis中使用if标签

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis where

上一篇:html5的新增标签keygen怎么用

下一篇:Java中Swagger技术怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》