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 ...
随机推荐
- 在vue组件中使用vuex的state状态对象的5种方式
下面是store文件夹下的state.js和index.js内容 //state.js const state = { headerBgOpacity:0, loginStatus:0, count: ...
- 【shell】awk的next用法
awk的next相当于循环中continue的作用,next后面的语句将不再执行. 例如,下面的例子中,包含数字3的那行的print语句没有被执行. [root]$ seq | awk '{print ...
- @ENABLECACHING 基于注解的缓存
@EnableCaching• @Cacheable指定一个或多个Cache名字,同属性cacheNamesSpring Cache 使用 ---@EnableCaching @Cacheable 注 ...
- 带 like 的字符串匹配查询
1.百分号通配符 '%' ,匹配任意长度的字符,甚至包括零字符 例:查询所有以 'a' 字母开头的水果,sql 语句如下 select f_id,f_name from fruits wher ...
- 字符串:StringBuilder()
String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) [1]String和StringBuffer String ...
- 【Leetcode】位1的个数
解题方案:位操作的技巧 整数 n 和 n-1(n>0) 做与运算,从其二进制形式来看,可以消掉 n 的二进制数值中最后1个 “1” .循环进行,每次消掉1个 “1” .整数 n 的二进制数值中有 ...
- Module——模块加载语法
简介:标准module用法: 模块功能主要由两个命令构成:export和import. export有三种写法: // profile.js // 写法一 export var m = 1; // 写 ...
- c++结构体、共用体和枚举
结构体类型 c++中的结构体成员既可以是数据,也可以是函数 c语言中定义结构体变量必须加struct(这也是很多时候和typedef),但是在c++里面,可以不加 结构体和类的不同在于,结构体中的变量 ...
- spring cloud:gateway-eureka
gateway-server-eureka 1. File-->new spring starter project 2.add dependency <dependency> &l ...
- 第五周课程总结&试验报告 (三)
课程总结 一,类的继承格式 1.在 Java 中通过 extends 关键字可以申明一个类是从另外一个类继承而来的,一般形式如下: class 父类 {} class 子类 extends 父类 {} ...