背景:使用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>标签

一、背景

二、使用

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

一、背景

二、使用

Mybatis—动态sql拼接问题的更多相关文章

  1. Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务

    第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...

  2. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  3. 自己动手实现mybatis动态sql

    发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...

  4. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  5. Mybatis动态SQL简单了解 Mybatis简介(四)

    动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ }   Mybatis应用中,S ...

  6. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...

  7. Mybatis 动态Sql语句《常用》

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  8. Mybatis动态sql及分页、特殊符号

    目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...

  9. MyBatis动态Sql 的使用

    Mapper.xml提示: 1:mapper包中新建一个文件:mybatis-3-mapper.dtd 2:在web app libraries/mybatis.jar/org.apache.ibat ...

随机推荐

  1. VC内联汇编和GCC内联汇编的语法区别

    VC: #include <stdio.h> main(){ int a = 1; int b = 2; int c; __asm{ mov eax,a mov ebx,b mov ecx ...

  2. Oracal数据库安装配置教程

    官网注册账号登录 https://www.oracle.com/technetwork/database/enterprise-edition/downloads/oracle12c-windows- ...

  3. easygui _1

    GUI---图形用户界面 什么是GUI? GUI是Graphical  User   Interface(图形用户界面)的缩写.在GUI中,并不是键入文本和返回值,用户可以看到文本框,窗口,按钮等图形 ...

  4. 正则获取a标签和a标签中的href地址

    /(<\/?a.*?>)/ a标签 /<a\b.*?</a>/ 表式以"<a "(有空格) 开始 以"</a>" ...

  5. git的clone和github的fork

    git的clone是从github上下载下来,clone到项目里面,fork是在本地修改后再提交到github上,在github上用request来进行提交,经作者确认后可以合同到mast分支上

  6. vmware哪个版本好用

    这个问题要根据你的物理机操作系统而定,如果你电脑是xp,就选择vmw7.1.6:如果你电脑是win7,win8,win8.1,就选择vmw10.0.1.不要去理会vmw8.vmw9这些都是vmw10的 ...

  7. JavaScript求取水仙花数

    一.什么是水仙花数 水仙花数也称为超完全数字不变数.自幂数.阿姆斯壮数.阿姆是特朗数. 水仙花数是指一个三位数,每个位数上数字的3次幂之和等于数字它本身. 水仙花数是自幂数的一种,三位的三次自幂数才叫 ...

  8. 6. ClustrixDB 备份恢复

    ClustrixDB备份恢复:   一.传统MySQL的备份/恢复 shell> mysqldump -u user -h clustrix host --single-transaction ...

  9. [BZOJ4237]稻草人:CDQ分治+单调栈

    分析 按\(y\)排序后CDQ分治,可以发现每个点可以影响的是\(x\)坐标的一段区间,可以使用扫描线+单调栈,在单调栈上二分即可解决,时间复杂度\(O(n \log^2 n)\). 通过归并排序可以 ...

  10. Oracle-SYSTEM表空间突然持续爆满

    一般情况下,我们建完数据库后,都会给数据库指定一个新的默认表空间,不然会占用数据库系统表空间资源,导致数据库性能下降. 我们可以同过SQL语句找出改表空间占用空间前10的对象 SELECT * FRO ...