题记部分

一、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. 利用Catalina快速重新指定tomcat的代码路径

    思路: 在/tomcat/conf/Catalina/localhost目录下,建立对应的xml文件,来定义. 方法: 比如:想在 Http://localhost/test-api 显示,且代码放在 ...

  2. openEuler欧拉安装指定版本的nodejs

    1. 安装nodejs dnf -y install nodejs npm config set registry https://registry.npmmirror.com -g npm conf ...

  3. vue3和elements创建应用

    一. 创建环境 1. 创建D:\code\vue 文件夹 2. vscode打开文件夹 3. 打开终端,输入 npm install -g @vue/cli 4. 配置环境变量 终端输入:npm co ...

  4. Excel VLOOKUP 函数入门教程

    一.引言 在日常工作中,Excel 是处理数据的强大工具之一,而 VLOOKUP 函数作为 Excel 中最常用的查找函数之一,能够帮助用户在表格或数值数组中快速定位并返回所需的数据.它的工作原理是在 ...

  5. Gmssl编译问题: error while loading shared libraries: libssl.so.1.1: cannot open shared object file

    在Centos7上编译安装Gmssl后,运行gmssl version出现如下错误: gmssl: error while loading shared libraries: libssl.so.1. ...

  6. HarmonyOS Next 入门实战 - 关系型数据库、smartdb

    SQLite数据库 HarmonyOS的关系型数据库基于SQLite 导入模块 import { relationalStore } from '@kit.ArkData'; 实现步骤: 获取RdbS ...

  7. Qt编写的项目作品37-安卓综合应用示例

    一.功能特点 封装了通用的Qt安卓组件,打通了常规与java交互机制. 动态切换横屏竖屏及获取当前横屏竖屏状态. 支持手机震动.拨打电话.发送短信. 支持moketoast临时消息.notify顶部任 ...

  8. SHA256 64 位加密

    /// <summary> /// SHA256 64位加密 /// </summary> /// <param name="input">&l ...

  9. kubernetes系列(七) - Pod生命周期

    目录 1. pod生命周期 2. initC 2.1 initC介绍 2.2 initC的作用 2.3 initC的模版 2.4 initC的一些其他补充 3. Pod健康性检查(liveiness) ...

  10. 百度高效研发实战训练营-Step3

    .# 百度高效研发实战训练营-Step3 3.1 质量意识 关于本部分,将从以下几点进行介绍: 3.1.1 质量的基本概念 意识的树立,源于认识的提高. 首先介绍质量的基本概念,主要包括以下内容: 1 ...