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>标签
一、背景
二、使用
*************多数据库类型问题
一、背景
二、使用
Mybatis—动态sql拼接问题的更多相关文章
- Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务
第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- 自己动手实现mybatis动态sql
发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
- Mybatis动态SQL简单了解 Mybatis简介(四)
动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ } Mybatis应用中,S ...
- 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 ...
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- Mybatis动态sql及分页、特殊符号
目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...
- MyBatis动态Sql 的使用
Mapper.xml提示: 1:mapper包中新建一个文件:mybatis-3-mapper.dtd 2:在web app libraries/mybatis.jar/org.apache.ibat ...
随机推荐
- Apache服务器配置https
https://startssl.com这个网站可以给我们免费提供可信任的https证书,这里简单介绍一下配置的过程. 首先服务器需要安装openssl和apache的mod_ssl.so模块,并且需 ...
- shell练习--PAT题目1008:数组元素循环右移问题 (失败案例,运行超时)
一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A ...
- Lambda表达式匿名类实现接口方法
Lamb表达式匿名类实现接口方法 import java.util.ArrayList; public class HandlerDemo{ public static void main(Strin ...
- Prim算法和Kruskal算法的正确性证明
今天学习了Prim算法和Kruskal算法,因为书中只给出了算法的实现,而没有给出关于算法正确性的证明,所以尝试着给出了自己的证明.刚才看了一下<算法>一书中的相关章节,使用了切分定理来证 ...
- Python语言中enumerate()及zip()函数的使用例子
在Python编程语言中,enumerate()及zip()是两个常用的内置函数,这两个函数功能类似,但又有所区别,下面通过两个例子分别进行说明. enumerate()函数 该函数在字面上是枚举.列 ...
- 极验验证码在php5.6.27下不显示
PHP5.6需要改php.ini 去掉;always_populate_raw_post_data = -1的 :
- 分布式架构基石-TCP通信协议
为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样 ...
- 【转】C语言中数组名和指针的区别
注:本文转自http://www.cnblogs.com/furaibo/archive/2010/03/19/1689710.html 魔幻数组名 请看程序(本文程序在WIN32平台下编译): #i ...
- Oracle-执行./runInstaller报错,弹不出图形界面
[root@Apexmic Packages]# xhost + 无论是root用户还是oracle用户均执行了xhost + 但还是报以下错误 [oracle@Apexmic database]$ ...
- ACM ICPC 2011-2012 Northeastern European Regional Contest(NEERC)A ASCII Area
A: 给你一个矩阵求'/' 和 '\' 围成的图形,简单签到题,有一些细节要考虑. 题解:一行一行的跑,遇到'/'和'\' 就加0.5, 在面积里面的'.' 就加1.用一个flag来判断是否在围住的图 ...