题记部分

一、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的更多相关文章

  1. mybatis 09: 动态sql --- part1

    作用 可以定义代码片段 可以进行逻辑判断 可以进行循环处理(批量处理),使条件判断更为简单 使用方式 通过mybatis中与动态sql有关的标签来实现 < sql >标签 + < i ...

  2. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  3. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  4. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  5. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  6. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  7. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  8. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  9. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

  10. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

随机推荐

  1. docker compose的安装

    1,安装docker ### CentOS8 默认是会读取centos.org的mirrorlist的,所以一般来说是不需要配置镜像的. # step 1: 安装必要的一些系统工具 sudo yum ...

  2. 云消息队列备份 | CKafka、TDMQ 消息队列数据备份到 COS

    前言 随着业务规模的不断扩大,企业越来越重视构建高性能.高可用的大型分布式系统.为了解决应用耦合,合理进行流量削锋,腾讯云提供了包括 CKafka 和 TDMQ 在内的一系列消息队列中间件.为了更方便 ...

  3. 6.MySQL性能优化

    参数 作用范围 全局:对实例的所有会话起作用 会话级:只对当前会话起作用 set session binlog_rows_query_log_events = on; set global binlo ...

  4. Docker目录汇总

    Docker目录汇总 Docker资料分享 Docker 教程 | 菜鸟教程 Docker入门级简易手册 Docker - 从入门到实践 Kubernetes中文手册 Kubernetes 指南 我的 ...

  5. Qt/C++实现帧同步播放器/硬解码GPU绘制/超低资源占用/支持8K16K/支持win/linux/mac/嵌入式/国产OS等

    一.前言 首先泼一盆冷水,在不同的电脑上实现完完全全的帧同步理论上是不可能的,市面上所有号称帧同步的播放器,同一台电脑不同拼接视频可以通过合并成一张图片来绘制实现完完全全的帧同步,不同电脑,受限于网络 ...

  6. WPF使用Microsoft.Toolkit.Mvvm作为Mvvm框架DryIoc作依赖注入

    背景 MVVMLight已多年未更新,Microsoft.Toolkit.Mvvm作为MVVMLight继任者,需要学习一下. Microsoft.Toolkit.Mvvm跟MVVMLight使用非常 ...

  7. Ubuntu 16.04 编译安装Python 2.7.18

    安装python 2.7.18(注)使用apt install python安装的版本是2.7.10,该版本对部分项目存在兼容性问题,因此需要手动编译安装 安装python编译环境sudo apt i ...

  8. 2024年终总结:5000 Star,10w 下载量,这是我交出的开源答卷

    你好,我是 Kagol,个人公众号:前端开源星球. 2024年,我做前端开发工作满10年啦! 这10年我一直在开发前线,做过电商项目.广告平台.项目管理系统等业务,目前主要专注于前端组件库建设和开源社 ...

  9. CDS标准视图:预期应收 I_FutureAccountsReceivables

    视图名称:预期应收 视图类型:参数 视图代码: 点击查看代码 //Documentation about annotations can be found at http://help.sap.com ...

  10. MS Webview2 拦截 interact/intercept

    https://docs.microsoft.com/en-us/microsoft-edge/webview2/how-to/webresourcerequested?tabs=dotnet 自定义 ...