当前位置: 首页 > news >正文

Mybatis 9种动态 sql 标签使用

MyBatis提供了9种动态SQL标签:trim、where、set、foreach、if、choose、when、otherwise、bind;

1.if 标签

<select id="getUser">select * from User<where><if test=" age != null ">and age > #{age}</if><if test=" name != null ">and name like concat(#{name},'%')</if></where>
</select>

2.choose 标签、when 标签、otherwise 标签

<select id="getUser">select * from User<where><choose><when test=" age != null ">and age > #{age}</when><when test=" name != null ">and name like concat(#{name},'%')</when><otherwise>and sex = '女'</otherwise></choose></where>
</select>

3.foreach 标签

<select id="findAll">select  * from user where ids in<foreach collection="list"item="item" index="index"open="(" separator="," close=")">#{item}</foreach>
</select>最终拼接完整的语句就变成:
select  * from user where ids in (1,2,3,...,100);当然你也可以这样编写:
<select id="findAll">select  * from user where<foreach collection="list"item="item" index="index"open=" " separator=" or " close=" ">id = #{item}</foreach>
</select>最终拼接完整的语句就变成:
select  * from user where id =1 or id =2 or id =3  ... or id = 100;

4.where 标签、set 标签

<select id="getUser">select * from User<where><if test=" age != null ">and age > #{age}</if><if test=" name != null ">and name like concat(#{name},'%')</if></where>
</select>
<update id="updateUser">update user<set><if test="age !=null">age = #{age},</if><if test="username !=null">username = #{username},</if><if test="password !=null">password = #{password},</if></set>where id =#{id}
</update>注意,set 标签之所以能够支持去除前缀逗号或者后缀逗号,
是由于其在构造 trim 标签的时候进行了前缀后缀的去除设置,而 where 标签在构造 trim 标签的时候就仅仅设置了前缀去除。

5.trim 标签

<trim prefix="SET" prefixOverrides="," >...
</trim>或者<trim prefix="SET" suffixesToOverride="," >...
</trim>由于 where 标签和 set 标签这两种 trim 标签变种方案已经足以满足我们实际开发需求,所以直接使用 trim 标签的场景实际上不太很多(其实是我自己使用的不多,基本没用过)。

6.bind 标签

平时你使用 mysql 都是如何拼接模糊查询 like 语句的 ( oracle 不支持)
select * from user where name like concat('%',#{name},'%')<select id="selecUser"><bind name="myName" value="'%' + _parameter.getName() + '%'" />SELECT * FROM userWHERE name LIKE #{myName}
</select>无论使用哪种数据库,这个模糊查询的 Like 语法都是支持的

拓展:sql标签 + include 标签

<!-- 可复用的字段语句块 -->
<sql id="userColumns">id,username,password
</sql>
查询或插入时简单复用:<!-- 查询时简单复用 -->
<select id="selectUsers" resultType="map">select<include refid="userColumns"></include>from user
</select>

相关文章:

  • Python知识点:使用Azure IoT Edge与Python进行边缘计算
  • 一文掌握Harbor镜像同步公有云镜像仓库实践
  • 从自动化到智能化:AI如何推动业务流程自动化
  • 五.海量数据实时分析-FlinkCDC+DorisConnector实现数据的全量增量同步
  • 成功使用DDNS动态域名访问我的群晖NAS(TP-link路由器)
  • 【鸿蒙学习】深入了解UIAbility组件
  • 关系型数据库和非关系型数据库的区别
  • JavaScript 反射(Reflect)和代理(Proxy)简单介绍
  • OLED移植
  • expressjs 中的mysql.createConnection,execute 怎么使用
  • MacOS配置python环境
  • Linux安装RabbitMQ安装
  • Ubuntu如何如何安装tcpdump
  • 微信小程序操作蓝牙
  • vue3 环境配置vue-i8n国际化
  • [笔记] php常见简单功能及函数
  • 【Amaple教程】5. 插件
  • php面试题 汇集2
  • Python爬虫--- 1.3 BS4库的解析器
  • ReactNativeweexDeviceOne对比
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • Zepto.js源码学习之二
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 基于遗传算法的优化问题求解
  • 聊聊hikari连接池的leakDetectionThreshold
  • 配置 PM2 实现代码自动发布
  • 微服务核心架构梳理
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 问题之ssh中Host key verification failed的解决
  • 系统认识JavaScript正则表达式
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 怎么把视频里的音乐提取出来
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • AI算硅基生命吗,为什么?
  • C# - 为值类型重定义相等性
  • MyCAT水平分库
  • ​ubuntu下安装kvm虚拟机
  • ​数据结构之初始二叉树(3)
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • #70结构体案例1(导师,学生,成绩)
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #数据结构 笔记三
  • $(function(){})与(function($){....})(jQuery)的区别
  • (007)XHTML文档之标题——h1~h6
  • (1)Jupyter Notebook 下载及安装
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)shell调试方法
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • .NET 8.0 中有哪些新的变化?