当前位置: 首页 > 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>

http://www.lryc.cn/news/448421.html

相关文章:

  • OpenHarmony(鸿蒙南向)——平台驱动开发【PIN】
  • 南平自闭症寄宿制学校:让孩子自信绽放
  • 汽车总线之---- LIN总线
  • Android开发MPAndroidChart两条折线图
  • HTML-ES6.0核心技术
  • 车间调度问题数学建模与CPLEX优化
  • < 基础物理 >
  • 【web开发】Spring Boot 快速搭建Web项目(三)
  • 无人机之战斗机的详解!
  • Verilog基础:时序调度中的竞争(四)(描述时序逻辑时使用非阻塞赋值)
  • 嵌入式边缘计算软硬件开发“1+X”考证建设方案
  • ES8的Java API client 8.0 简单示例操作 Elasticsearch
  • 多线程CompletableFuture
  • AR传送门+特定区域显示内容+放大镜 效果着色器使用
  • 设置Hadoop守护进程的JVM参数
  • 可视化大屏
  • pytest框架
  • 基于大数据的亚健康人群数据分析及可视化系统
  • 黄金短线交易策略:波动中的高效盈利之法
  • 西陆家政系统V1.0.1
  • 时间安全精细化管理平台/iapp/mobile/facereg/facereg.html接口存在未授权访问漏洞
  • 自动化测试实例:Web登录功能性测试(无验证码)
  • 【算法篇】二叉树类(3)(笔记)
  • 基于php的律所管理系统
  • MySQL 之索引详解
  • C#测试调用FreeSpire.PDFViewer浏览PDF文件
  • 又一挣钱副业:AI生成影视解说,半个月涨粉变现3.5W+!
  • R语言 基础 笔记 3
  • 【MySQL】常见的SQL优化方式(一)
  • 【重点】使用axios.request.put上传文件,报错分析