目录

  • 动态SQL if语句
  • 动态SQL if+where语句
  • 动态SQL if+set语句
  • 动态SQL choose(when,otherwise)语句
  • 动态SQL trim语句
  • 动态SQL SQL片段
  • 动态SQL foreach语句

动态SQL if语句

<select id="selectUserByUsernameAndAge" resultMap="UserMap">
select * from user where
<if test="username != null">
username=#{username}
</if> <if test="age != null">
and age=#{age}
</if>
</select>

如果 sex 等于 null,那么查询语句为 select * from user where username=#{username},但是如果usename 为空呢?那么查询语句为 select * from user where and age=#{age},这是错误的 SQL 语句

动态SQL if+where语句

<select id="selectUserByUsernameAndAge" resultMap="UserMap">
select * from user
<where>
<if test="username != null">
username=#{username}
</if> <if test="age != null">
and age=#{age}
</if>
</where>
</select>

“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

动态SQL if+set语句


<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.kdgc.wx.entity.User">
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="age != null and age != ''">
u.age = #{age}
</if>
</set>
where id=#{id}
</update>

动态SQL choose(when,otherwise)语句

<select id="selectUserByChoose" resultType="com.kdgc.wx.entity.User" parameterType="com.kdgc.wx.entity.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and age=#{age}
</otherwise>
</choose>
</where>
</select>

动态SQL trim语句

trim标记是一个格式化的标记,可以完成set或者是where标记的功能

①、用 trim 改写上面第二点的 if+where 语句

<select id="selectUserByUsernameAndAge" resultType="user" parameterType="com.kdgc.wx.entity.User">
select * from user
<!-- <where>
<if test="username != null">
username=#{username}
</if> <if test="username != null">
and sex=#{sex}
</if>
</where> -->
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
</select>

prefix:前缀

prefixoverride:去掉第一个and或者是or

②、用 trim 改写上面第三点的 if+set 语句

<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.kdgc.wx.entity.User">
update user u
<!-- <set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set> -->
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.age = #{age},
</if>
</trim> where id=#{id}
</update>

suffix:后缀

suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

动态SQL SQL片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

<!-- 定义 sql 片段 -->
<sql id="selectUserByUserNameAndAgeSQL">
<if test="username != null and username != ''">
AND username = #{username}
</if>
<if test="age != null and age != ''">
AND age = #{age}
</if>
</sql>

引用 sql 片段

<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.kdgc.wx.entity.User">
select * from user
<trim prefix="where" prefixOverrides="and | or">
<!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace -->
<include refid="selectUserByUserNameAndSexSQL"></include>
<!-- 在这里还可以引用其他的 sql 片段 -->
</trim>
</select>

注意:

①、最好基于 单表来定义 sql 片段,提高片段的可重用性

②、在 sql 片段中最好不要包括 where

动态SQL foreach语句

需求:我们需要查询 user 表中 id 分别为1,2,3的用户

sql语句:

select * from user where id=1 or id=2 or id=3

select * from user where id in (1,2,3)

  • foreach 来改写 select * from user where id=1 or id=2 or id=3
<select id="selectUserByListId" parameterType="com.kdgc.wx.entity.UserVo" resultType="com.kdgc.wx.entity.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and (id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
  • foreach 来改写 select * from user where id in (1,2,3)
<select id="selectUserByListId" parameterType="com.kdgc.wx.entity.UserVo" resultType="com.kdgc.wx.entity.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
#{id}
</foreach>
</where>
</select>

文章参考:https://www.cnblogs.com/ysocean/p/7289529.html

【Mybatis】动态SQL的更多相关文章

  1. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  2. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  3. 自己动手实现mybatis动态sql

    发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...

  4. Mybatis动态SQL单一基础类型参数用if标签

    Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...

  5. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  6. Mybatis动态SQL简单了解 Mybatis简介(四)

    动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ }   Mybatis应用中,S ...

  7. mybatis原理分析学习记录,mybatis动态sql学习记录

    以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...

  8. mybatis 动态sql和参数

    mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...

  9. MyBatis动态SQL之一使用 if 标签和 choose标签

    bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...

  10. 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 ...

随机推荐

  1. XXL-JOB定时任务框架(Oracle定制版)

    特点 xxl-job是一个轻量级.易扩展的分布式任务调度平台,能够快速开发和简单学习.开放源代码并被多家公司线上产品使用,开箱即用.尽管其确实非常好用,但我在工作中使用的是Oracle数据库,因为xx ...

  2. 人工智能AI库Spleeter免费人声和背景音乐分离实践(Python3.10)

    在视频剪辑工作中,假设我们拿到了一段电影或者电视剧素材,如果直接在剪辑的视频中播放可能会遭遇版权问题,大部分情况需要分离其中的人声和背景音乐,随后替换背景音乐进行二次创作,人工智能AI库Spleete ...

  3. 探究公众号接口漏洞:从后台登录口到旁站getshell

    探究公众号接口漏洞:从后台登录口到旁站getshell 1.入口 发现与利用公众号接口安全漏洞 某120公众号提供了一处考核平台,通过浏览器处打开该网站. 打开可以看到一处密码登录口,试了一下常用的手 ...

  4. 学习C语言的第一天

    今天学习C语言学习了三个部分: 第一个部分是软件环境的搭建,如何搭建一个项目 使用工具:visual studio 2010 搭建过程:新建项目.配置设置(主要是解决运行后一闪而过的问题) 第二部分是 ...

  5. SPI-SPI主机硬件片选功能使用说明

    SPI主机硬件片选功能使用说明 SPI协议最早的标准,是由摩托罗拉公司制定.在协议使用的过程中,根据实际需求可能会进行一些扩展和修改. 在一份由飞思卡尔半导体发布的SPI V4.01版本规范中,对片选 ...

  6. Linux下ftp常见问题总结

    Linux下ftp常见问题总结 似乎拖欠了几篇文章了@_@,来公司半年了,成长了不少!从大学毕业,直到看到http://blog.csdn.net/leixiaohua1020  雷霄骅(然而天妒英才 ...

  7. 深度相机:结构光、TOF、双目相机

    随着人工智能与机器人.无人驾驶的火热,深度相机的技术和应用也受到关注,何谓深度相机? 顾名思义,就是可以测量物体到相机的距离(深度) 传统的RGB彩色普通相机称为2D相机,只能拍摄相机视角内的物体,没 ...

  8. STM32 + RTThread + UGUI

    一.概述 开发板:STM32F103C8T6 显示器:ST7735S RT-Thread:5.0.0 玩过 GUI 的小伙伴都知道,界面的显示是一个个像素点组合起来的,那么直接构建出来炫酷的 GUI ...

  9. ssh终端工具推荐-WindTerm

    什么是WindTerm 官方github https://github.com/kingToolbox/WindTerm A Quicker and better SSH/Telnet/Serial/ ...

  10. react之todoList基础小项目

    1.项目最终成品和项目目录快照如图: 2.context.js文件 // 使用context进行多级传递数据 // 1. createContext 创建一个可以多级传递的context数据 // 2 ...