Mybatis—动态sql拼接问题

背景:使用Mybatis的最近半年,经常发现一些小坑,现在总结回顾下,记个小本本,不让它再来欺负我!

 百度了许久,才留心到官网文档,比我的全,我很菜的!

*************<if>判断语句

一、注意⚠️事项

1、不支持 && , 用 and  or  || 来做逻辑与或的判断 

2、支持以下操作符

  == (对应特殊操作符 eq) 

  != (对应特殊操作符 neq) 

  >  (对应特殊操作符 gt) 

  >= (对应特殊操作符 gte) 

  < (对应特殊操作符 lt)

  <= (对应特殊操作符 lte)

 

二、数据类型

1、字符串类型

1.1 如果不需要过滤空串的情况 仅仅判断null即可

例如:

<if test="username != null"></if>

1.1 如果需要过滤空串,添加空串判断即可

例如:

<if test="username != null and '' != username"></if>

 1.2 支持String的JDK自带方法:如果判断字符串是否已某个特俗字符开头,结尾等

例如:

<!-- 是否以什么开头 -->
<if test="username != null and username.indexOf('ji') == 0"> </if> 
<!-- 是否包含某字符 -->
<if test="username != null and username.indexOf('ji') >= 0"> </if> 
<!-- 是否以什么结尾 -->
<if test="username != null and username.lastIndexOf('ji') > 0"></if>  

1.3* 是否是某个特定字符串

例如:

<if test="username != null and 'hello' == username"></if>
或者
<if test="username != null and 'hello' eq username"></if>
或者
<if test="username != null and 'hello'.toString() == username.toString()"></if>
或者
<if test="'xiaohong' eq username or 'xiao' eq username ">

 2、数字类型

2.1 仅作null判断

例如:

<if test='id != null'>

 2.2 数字的大小于判断

例如:

<if test='id != null and id > 27 '> 
或者
 <if test='id != null and id gt 27 '>

3、集合类型

3.1 判断list是否为空

例如:

<if test="userList != null and userList.isEmpty()"></if> 
或者
<if test="userList != null and userList.size()>0"></if>

4、传入单一对象可以默认,传入多个对象必须加前缀

例如:

<if test="author != null and author.name != null">
    AND author_name like #{author.name}
</if>

 *************<choose>其他判断标签

一、使用背景

需要在where条件语句或者在查询字段中中进行判断,当type == x1 时和type == x2时条件不同;

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。

当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。

二、choose, when, otherwise

例如:

<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
    SELECT * FROM User u   
    <where>  
        <choose>  
            <when test="username !=null ">  
                u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
            </when >  
            <when test="sex != null and sex != '' ">  
                AND u.sex = #{sex, jdbcType=INTEGER}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday, jdbcType=DATE}  
            </when >  
            <otherwise>  
            </otherwise>  
        </choose>  
    </where>    
</select>  

*************<where>标签

一、背景

自己写sql需要在where后加1=1或者首先写必判断的sql,否则where and * 会报错

例如:错误例子

SELECT * FROM BLOG
WHERE 
AND title like ‘someTitle’

<where>标签:简化sql语句中where条件判断的书写的;自动将其后第一个条件的and或者是or给忽略掉;

二、where使用

例如:

<select id="selectByParams" parameterType="map" resultType="user">
  select * from user
  <where>
    <if test="id != null ">
      id=#{id}
    </if>     <if test="name != null and name.length()>0" >
      and name=#{name}
    </if>     <if test="gender != null and gender.length()>0">
      and gender = #{gender}
    </if>   </where> </select>  

 *************<set>标签

一、背景

因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留;

 <set>标签:set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号;

二、使用

例如:

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">
          username=#{username},
      </if>
      <if test="password != null">
          password=#{password},
     </if>
      <if test="email != null">
      email=#{email},
   </if>
      <if test="bio != null">
      bio=#{bio}
  </if>
    </set>
  where id=#{id}
</update>

*************<trim>标签

一、背景

<trim>标签:格式化的标记,可以完成set或者是where标记的功能

二、使用

移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容,注意此例中的空格也是必要的

prefix:前缀      

prefixoverride:去掉第一个and或者是or

suffix:后缀

select * from user 
<trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> 
       AND name=#{name}
    </if>
    <if test="gender != null and gender.length()>0">
       AND gender=#{gender}
    </if>
</trim>
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
  <if test="name != null and name.length()>0"> 
    name=#{name} , 
  </if>
  <if test="gender != null and gender.length()>0"> 
    gender=#{gender} ,  
  </if>
</trim>

*************<foreach>标签

一、背景

二、使用

*************多数据库类型问题

一、背景

二、使用

 

原文地址:https://www.cnblogs.com/huasky/p/10150652.html