• mybatis的xml中<trim>标签的用法


    1. 前言

    1. 在工作中离不开跟数据库打交道,目前流行的固然是mybatis,在xml中写sql的时候,可能会出现下面情况:

      <select id="select" resultType="User">
         select * from user2
         where 
           <if test="name != null">
              name = #{name}
           if>
           <if test="pwd != null">
               and pwd = #{pwd}
           if>
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      上述写法问题,sql中可能会有where条件,但是条件的个数未定,且条件任意一条都随机成立
      1. 情况一:name与pwd 都不为空,sql正常
      2. 情况二:name不为空,pwd为空,sql正常
      3. 情况三:name为空,pwd不为空,sql异常,因为sql就变成select * from user2 where and pwd = #{pwd}
      4. 情况四:name、pwd都为空,sql异常,因为sql就变成select * from user2 where

    2. 针对上述情况,有人可能会这样子解决,确实完全解决上述出现的问题,并且简单,但是唯一就是出现 1 = 1,很不美观:

      <select id="select" resultType="User">
         select * from user2
         where 1 = 1
           <if test="name != null">
              and name = #{name}
           if>
           <if test="pwd != null">
               and pwd = #{pwd}
           if>
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    3. 为了追求更好的写法,因此则稍微研究了一下:mybatis中自带的标签完美解决这个事情,如下文。

    2. 先说结论

    1. mybatis中有标签,语法如下:

      <trim prefix="" prefixOverrides="" suffix="" suffixOverrides="">trim>
      
      prefix:若条件成立,在trim标签内sql语句前面加上前缀
      prefixOverrides:若条件成立,在trim标签内原sql语句,原sql语句,前面去掉多余前缀内容
      suffix:若条件成立,在trim标签内sql语句后面加上后缀
      suffixOverrides:若条件成立,在trim标签内原sql语句,原sql语句,后面去掉多余后缀内容
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    2. 例子:

          <select id="select" resultType="User">
               select * from user2
               <trim prefix="where" prefixOverrides="ADN|and" suffix="" suffixOverrides="">
                   <if test="name != null and name.length() > 0">
                     and name = #{name}
                   if>
                   <if test="pwd != null and pwd.length() > 0">
                    and pwd = #{pwd}
                   if>
               trim>
          select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      1. 情况一:name与pwd ,都不为空,则trim内的sql语句前加上where 并且去掉name多余的前缀and
      2. 情况二:name不为空,pwd为空,则trim内的sql语句前加上where 并且去掉name多余的前缀and
      3. 情况三:name为空,pwd不为空,则trim内的sql语句前加上where 并且去掉pwd多余的前缀and
      4. 情况四:name、pwd都为空,则trim内的sql语句直接不存在

    3. 验证

    1. 最终XML的sql如下写:

      <select id="select" resultType="User">
               select * from user2
               <trim prefix="where" prefixOverrides="ADN|and">
                   <if test="name != null">
                     and name = #{name}
                   if>
                   <if test="pwd != null">
                     and pwd = #{pwd}
                   if>
               trim>
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

    1. 情况一

    1. 当name与pwd 都不为空时,trim内sql前面加上where,并去掉name前面的前缀and
      在这里插入图片描述

    2. 情况二

    1. 当name不为空,pwd为空,则trim内的sql语句前加上where 并且去掉name多余的前缀and
      在这里插入图片描述

    3. 情况三

    1. name为空,pwd不为空,则trim内的sql语句前加上where 并且去掉pwd多余的前缀and
      在这里插入图片描述

    4. 情况四

    1. name、pwd都为空,则trim内的sql语句直接不存在
      在这里插入图片描述

    5. 验证prefixOverrides去掉的是trim内原sql内容

    在这里插入图片描述

  • 相关阅读:
    详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
    访问学者申请|面签的八大注意事项
    钢筋智能测径仪 光圆与带肋钢筋均可检测!
    Web 性能优化:TCP
    k8s部署问题及解决方法
    springcloud:四、nacos介绍+启动+服务分级存储模型/集群+NacosRule负载均衡
    STM32使用ThreadX示例以及tx_thread_create解析
    常用应用安装教程---在centos7系统上安装JDK8
    docker篇---docker-compose和dockerfile构建镜像
    SpringCloud Alibaba组件入门全方面汇总(中):服务熔断降级-Sentinel
  • 原文地址:https://blog.csdn.net/xueyijin/article/details/128105034