mybatis - [09] 动态SQL
题记部分
一、if & test
如果id,name,age不为空,则按照指定的值进行查询。如果这三者都是空(null和空字符串),则该sql执行结果为全表查询的结果集。
<select id="getUserByUser" parameterTytpe="vo.User" resultMap="userResultMap">
select
in,name,age
from
tb_user
where 1=1
<if test="id != null">
and id = #{id}
</if>
<if test="name != null and name !=''">
and name = #{name}
</if>
<if test="age != null">
and age = #{age}
</if>
</select>
二、choose & when & otherwise
<select id="getUserByUser" parameterTytpe="vo.User" resultMap="userResultMap">
select
in,name,age
from
tb_user
where 1=1
<choose>
<when test="id != null">
and id = #{id}
</when>
<when test="name != null and name !=''">
and name = #{name}
</when>
<otherwise>
and age is not null
</otherwise>
</choose>
</select>
三、where
<select id="getUserByUser" parameterTytpe="vo.User" resultMap="userResultMap">
select
in,name,age
from
tb_user
<where>
<if test="id != null">
and id = #{id}
</if>
</where>
</select>
四、trim
有时候,需要去掉一些特殊的SQL语法,比如:and、or。则可以使用trim元素。使用方法如下:
① prefix: 在trim标签内sql语句加上前缀
② suffix: 在trim标签内sql语句加上后缀
③ prefixOverrides: 指定去除多余的前缀内容
④ suffixOverrides: 指定去除多余的后缀内容
<select id="getUserByUser4" parameterType="vo.user" resultMap="userResultMap">
select
id,name,age
from
tb_user
<trim prefix="where" prefixOverrides="and">
and id = #{id}
</trim>
</select>
五、set
set元素会默认把最后一个逗号去掉
<update id="updateUserByUser" parameterType="vo.user">
update
tb_user
<set>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age != null">
age = #{age}
</if>
</set>
where
id = #{id}
</update>
六、foreach
foreach语句用于循环遍历传入的集合数据。使用该方法涉及如下参数:
① collection:传递进来的参数名称,可以是数组、List、Set等集合。
② index: 当前元素在集合的下标位置
③ item: 循环中当前的元素
④ open和close: 使用什么符号包装集合元素。
⑤ separator: 每个元素的间隔符号
<select id="getUserByIds" resultMap="userResultMap">
select id,name,age from tb_user
where id in
<foreach collection="idList" index="index" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
接口定义
public interface UserMapper{
List<User> getUserByIds(@Param("idList")List<Long> idList);
}
七、concat & bind
可以使用这两种方式进行拼装和绑定
<select id="getUserByName" parameterType="string" resultMap="userResultMap">
select
id,name,age
from
tb_user
where
name like concat('%',#{name},'%')
</select>
等同于
<select id="getUserByName" parameterType="string" resultMap="userResultMap">
<bind name="namePattern" value="'%' + name + '%'"/>
select
id,name,age
from
tb_user
where
name like #{namePattern}
</select>
— 业精于勤荒于嬉,行成于思毁于随 —
mybatis - [09] 动态SQL的更多相关文章
- mybatis 09: 动态sql --- part1
作用 可以定义代码片段 可以进行逻辑判断 可以进行循环处理(批量处理),使条件判断更为简单 使用方式 通过mybatis中与动态sql有关的标签来实现 < sql >标签 + < i ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
随机推荐
- 服务拆分之《Dubbo服务跨云通信》
2022年10月开始,公司从阿里请来的架构师将全力推进服务拆分这个计划.实际上这个计划早就提上日程了,只是没有一个带头大哥带着把这个事情搞起来,因为这个系统太庞大了,还非常的复杂,当时就没有哪一个人是 ...
- CVE-2023-31436 数组越界漏洞
CVE-2023-31436 数组越界漏洞 drawio: CVE-2023-31436.drawio 漏洞分析 在 qfq_change_class 里面如果用户态没有提供 TCA_QFQ_LM ...
- codeforces1849 D. Array Painting
题目链接 https://codeforces.com/problemset/problem/1849/D 题意 输入 \(n(1 \leq n \leq 2e5)\) 和长为 \(n\) 的数组 \ ...
- 02C++顺序结构(1)
1.C++程序的样子 2.流 输出流 COUT<< 3.一个实例及解析 4.小结 头文件的解释 头文件是C++程序对其他程序的引用,就是让编译器的预处理器把这个输入输出流的标准文件iost ...
- less 常用方法
介绍 Less 是 CSS 的预处理语言之一,为 CSS 增添了变量.Mixin.函数等特性,使CSS更易于维护扩展. 嵌套(Nesting) .header { .navgation: { font ...
- P6474 [NOI Online #2 入门组] 荆轲刺秦王 题解
荆轲将会臭名昭著 首先 $15$ 做法很简单,那就是直接 `cout<<-1` 考虑用 BFS 来解思路很简单,但是怎么求每个士兵的控制范围呢? 直接暴力时间复杂度是 $O(nma^2)$ ...
- server.error.include-message
使用的thymeleaf模板引擎,默认前端无法获取message和exception 想要在前端获取到message和exception,配置一下配置 server.error.include-exc ...
- Socat 命令总结
事以密成,语以泄败. 导航 介绍 基本语法 用法示例 回显输入 回显输入 over TCP/UDP 正向连接 shell 反向连接 shell 端口转发 网络服务 文件传输 管道传输 加密传输 TUN ...
- Qt血的教训/细数Qt开发的各种坑/又爱又恨/欢迎围观留言评论
一.吐槽总结 搞Qt开发十几年了,最初从Qt4.6开始,一直追新到现在的6.7版本,中间经历过无数的血的教训,简直是又爱又恨.其实Qt挺好的,但是还是要忍不住吐槽一下,本人还是希望Qt发展的越来越好, ...
- 在linux上Git配置多个SSH-Key
Git配置多个SSH-Key SSH Key 背景 当有多个git账号时,比如: a. 一个gitee,用于公司内部的工作开发: b. 一个github,用于自己进行一些开发活动: c.一个gitla ...