Mybatis-trim标签

今天遇到一个场景需要写一个这样的查询语句:
用户对象userInfo包含下面几个字段:
userName phone email qqId weiboId wxId

欢迎访问本人博客:http://wangnan.tech

现在新注册用户,传过来一个注册userInfo对象,现在要到数据库中验证状态status=1 (表示激活的用户)的用户中,是否存在一个用户,只要它这些字段中至少有一个与新注册的对象对应的字段内容相同,那就说明重复注册。

翻译成sql语句表示一下的意思大概就是:
select * from tablename where
(
userName=”xxx”
or phone =”xxx”
or …
)
and status=1

一开始我是这样写的,在mybatis中的代码就是这样:

<select id="selectBySelective" resultType="xxx.UserInfo">
   select 
    <include refid="Base_Column_List" />
    from uc_user 
    <where>
    (<if test="userName != null" >
        user_name = #{userName}
      </if>
      <if test="email != null" >
        or email = #{email}
      </if>
      <if test="phone != null" >
        or phone = #{phone}
      </if>
      <if test="weiboId != null" >
        or weibo_id = #{weiboId}
      </if>
      <if test="wxId != null" >
        or wx_id = #{wxId}
      </if>
      <if test="qqId != null" >
        or qq_id = #{qqId}
      </if>)
     </where>
     and status = 1
</select>

这样代码看似没有什么问题但是其实是有问题的。为什么呢?
如果userName 为空,后面某字段不为空,最后的sql语言会成为这样:

select * from uc_user where(or email = "xxx") and status = 1

使用mybatis < where > 标签就是为了防止这种情况,mybatis会在第一个
userName 为空的情况下,帮我们去掉后面的语句的第一个”or”

但是我加了where标签中加入()后,语句会报错。因为自动去掉”or”会失效。

查看了mybatis官方文档发现了另一个标签 < trim >可以通过自定义 trim 元素来定制我们想要的功能

trim标签包围的内容可以设置几个属性:
prefix :内容之前加的前缀
suffix :内容之后加的后缀
prefixOverrides: 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的,多个忽略序列用“|”隔开)。它带来的结果就是所有在 prefixOverrides 属性中指定的内容将被移除。

所以我修改后的代码是:

<select id="selectBySelective" resultType="xxx.UserInfo">
   select 
    <include refid="Base_Column_List" />
    from uc_user 
    <trim prefix="WHERE ("  suffix=")" prefixOverrides="AND |OR "> 
    <if test="userName != null" >
        user_name = #{userName}
      </if>
      <if test="email != null" >
        or email = #{email}
      </if>
      <if test="phone != null" >
        or phone = #{phone}
      </if>
      <if test="weiboId != null" >
        or weibo_id = #{weiboId}
      </if>
      <if test="wxId != null" >
        or wx_id = #{wxId}
      </if>
      <if test="qqId != null" >
        or qq_id = #{qqId}
      </if>  
     </trim>
     and status = 1
</select>

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 4,785评论 0 4
  • 官方文档 简介 入门 XML配置 XML映射文件 动态SQL Java API SQL语句构建器 日志 一、 JD...
    拾壹北阅读 3,297评论 0 52
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 132,642评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 30,944评论 18 399
  • 明天是520,刚好最近一直看到这个关于恋爱的东西,今天来说说好了。 后知后觉的迷上看《欢乐颂》的我,最近在看《欢乐...
    梦夕梦阅读 452评论 4 5