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) ...
随机推荐
- 【Amadeus原创】更改docker run启动参数
经过一整天的摸索,答案: 没法直接修改.只能另外创建. 但是还好不用完全重头来,用docker commit命令可以基于当前修改的内容创建一个新的image. 执行docker 看看帮助先: Comm ...
- Axios 面试题 (2023-09-15更新)
有封装过 axios 么?封装一个 axios? import axios from 'axios' // 创建axios实例 const service = axios.create({ baseU ...
- Flutter Package: retry
Flutter package: retry 传送门 This package provides an easy way to retry asynchronous functions. This i ...
- OpenTelemetry.NET API
OpenTelemetry.NET API Status and Releases Tracing Metrics Logging 1.0 Alpha Beta 安装 dotnet add packa ...
- IdentityServer4 快速上手
IdentityServer4 是一个基于 .NET Core 的 OpenID Connect 实现框架. 基于框架创建可运行的应用,通常还需要多个步骤,添加引用.配置项目.框架初始化.按照一系列步 ...
- 【MyBatis】学习笔记09:动态设置表名
[Mybatis]学习笔记01:连接数据库,实现增删改 [Mybatis]学习笔记02:实现简单的查 [MyBatis]学习笔记03:配置文件进一步解读(非常重要) [MyBatis]学习笔记04:配 ...
- API接口请求小结
API接口请求小结 一.python: API接口请求 1.1 multipart/form-data类型请求 参数类型:数组 1.2 multipart/form-data类型请求 参数类型:文件流 ...
- 分布式全局唯一ID解决方案详解
--------------------- ID是数据的唯一标识,传统的做法是利用UUID和数据库的自增ID,在互联网企业中,大部分公司使用的都是Mysql,并且因为需要事务支持,所以通常会使用Inn ...
- w3cschool-Python3 教程
https://www.w3cschool.cn/python3/ Python 特点 1.易于学习:Python有相对较少的关键字,结构简单,和一个明确定义的语法,学习起来更加简单. 2.易于阅读: ...
- GDAL矢量数据集相关接口的资源控制问题
1. 引言 笔者在<使用GDAL读写矢量文件>这篇文章中总结了通过GDAL读写矢量的具体实现.不过这篇文章中并没有谈到涉及到矢量数据集相关接口的资源控制问题.具体来说,GDAL/OGR诞生 ...