十、动态SQL

根据条件的不同, SQL 语句也会随之动态的改变. MyBatis 中,提供了一组标签用于实现动态 SQL.

1. <if>

用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.

<select id="sel" resultType="user">

select * from t_user where 1=1

<if test="username != null and username != ''">

and username=#{username}

</if>

<if test="password != null and password != ''">

and password=#{password}

</if>

</select>

2. <where>

用于管理 where 子句. 有如下功能:

(1) 如果没有条件, 不会生成 where 关键字

(2) 如果有条件, 会自动添加 where 关键字

(3) 如果第一个条件中有 and, 去除之

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username != null and username != ''">

and username=#{username}

</if>

<if test="password != null and password != ''">

and password=#{password}

</if>

</where>

</select>

3. <choose><when><otherwise>

这是一套标签, 功能类似于 switch...case...

<select id="sel" resultType="user">

select * from t_user

<where>

<choose>

<when test="username != null and username != ''">

and username = #{username}

</when>

<when test="password != null and password != ''">

and password = #{password}

</when>

<otherwise>

and 1=1

</otherwise>

</choose>

</where>

</select>

4. <set>

用于维护 update 语句中的 set 子句. 功能如下:

(1) 满足条件时, 会自动添加 set 关键字

(2) 会去除 set 子句中多余的逗号

(3) 不满足条件时, 不会生成 set 关键字

int updUser(User user);

<update id="updUser" parameterType="user">

update t_user

<set>

id=#{id}, <!-- 防止所有条件不成立时的语法错误 -->

<if test="username != null and username != ''">

username=#{username},

</if>

<if test="password != null and password != ''">

password=#{password},

</if>

</set>

where id=#{id}

</update>

 

 

5. <trim>

用于在前后添加或删除一些内容

(1) prefix, 在前面添加内容

(2) prefixOverrides, 从前面去除内容

(3) suffix, 向后面添加内容

(4) suffixOverrides, 从后面去除内容

<update id="updUser" parameterType="user">

update t_user

<!--

prefix: 前缀, 表向前面添加内容

prefixOverrides: 从前面删除内容

suffix: 后缀, 表向后面添加内容

suffixOverrides: 从后面删除内容

-->

<trim prefix="set" prefixOverrides="user" suffix="hahaha"

suffixOverrides=",">

username=#{username},

</trim>

where id=#{id}

</update>

6. <bind>

用于对数据进行再加工, 用于模糊查询

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username!=null and username!=''">

<bind name="username" value="'%' + username + '%'"/>

and username like #{username}

</if>

</where>

</select>

 

7. <foreach>

用于在 SQL 语句中遍历集合参数, 在 in 查询中使用

(1) collection: 待遍历的集合

(2) open: 设置开始符号

(3) item: 迭代变量

(4) separator: 项目分隔符

(5) close: 设置结束符号

<select id="selIn" parameterType="list" resultType="user">

select * from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{item}

</foreach>

</select>

List<User> selIn(@Param("list") List<Integer> list);

8. <sql><include>

<sql>用于提取 SQL 语句, <include>用于引 SQL 语句

<sql id="mySql">

id, username, password

</sql>

<select id="selIn" parameterType="list" resultType="user">

select

<include refid="mySql"/>

from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{item}

</foreach>

</select>

转载于:https://www.cnblogs.com/kendyho/p/10847940.html

3.3.10 动态SQL相关推荐

  1. 关于 MyBatis动态sql,这里有 10 种超好用的写法

    mybatis=<>的写法 第一种写法(1): 原符号 < <= > >= & ' " 替换符号 < <= > >= &a ...

  2. MyBatis动态SQL之 set 和 trim标记的使用示例

    2019独角兽企业重金招聘Python工程师标准>>> 和之前的where一样,set和trim也是智能标记 在之前的user.xml中添加 <update id=" ...

  3. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源:cnblogs.com/homejim/p/9909657. ...

  4. SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?

    前言 之前我们已经讨论过动态SQL查询呢?这里为何再来探讨一番呢?因为其中还是存在一定问题,如标题所言,很多面试题也好或者有些博客也好都在说在执行动态SQL查询时sp_executesql的性能比ex ...

  5. PL/SQL -- 动态SQL调用包中函数或过程

    动态SQL主要是用于针对不同的条件或查询任务来生成不同的SQL语句.最常用的方法是直接使用EXECUTE IMMEDIATE来执行动态SQL语句字符串或字符串变量.但是对于系统自定义的包或用户自定的包 ...

  6. MyBatis的动态SQL详解

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

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

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

  8. PL/SQL -- 动态SQL

    --==================== -- PL/SQL --> 动态SQL --==================== 使用动态SQL是在编写PL/SQL过程时经常使用的方法之一.很 ...

  9. oracle中执行动态sql语句吗,oracle中有没有可动态执行sql语句的函数

    oracle中有没有可动态执行sql语句的函数 关注:233  答案:2  手机版 解决时间 2021-03-05 15:53 提问者祗剩寂寞 2021-03-04 22:38 oracle中有没有可 ...

最新文章

  1. PyTorch手把手自定义Dataloader读取数据
  2. Leetcode 83 删除排序链表中的重复元素 (每日一题 20210804)
  3. SAP实施的难点在哪里?
  4. 检索数据_4_从表中查询部分列
  5. 安卓 图像清晰度识别_螺柱焊位置识别算法初稿
  6. python 支付宝支付 无效根证书_Python-支付宝SDK的使用与注意事项,pythonalipaysdk,及,点...
  7. java整理软件--- Java OCR 图像智能字符识别技术,可识别中文,但是验证码不可以识别...已测识别中文效果很好
  8. Android应用开发以及设计思想深度剖析(2)
  9. Oracle怎么查看离散任务,Oracle ERP操作手册
  10. 软件开发角色知识概括
  11. 北邮 网络安全 期末复习 知识点总结之防火墙
  12. 01、winPE64位 -- UEFI+GPT启动引导分区方式 -- win10专业版(1903)64位安装 -- 数字许可永久激活
  13. uc手机浏览器 手机模拟_移动端页面调试工具——UC浏览器开发者版
  14. html中的abbr有什么作用,html中关于abbr标签的使用以及作用的详解
  15. 大数据商机VS个人隐私 车联网的攻与守
  16. intel万兆以太网网卡吞吐量测试
  17. 区块链安全————区块链技术安全讨论
  18. 日志管理logging的使用
  19. android 16进制/10进制/2进制转换
  20. AlertDialog基本使用

热门文章

  1. FastJson的常用操作
  2. 面向对象课程 - T-shirt
  3. Linux CentOS 5.5 服务器安装图文教程
  4. HTML5知识点汇总
  5. 微信小程序模仿开眼视频app(一)——视频首页、视频详情、分类
  6. 案例篇-HBase RowKey 设计指南
  7. Applications Manager Docker监控
  8. 计算机加入域 不能访问网络位置 解决办法
  9. maven project创建填充项
  10. 《黑客大曝光:移动应用安全揭秘及防护措施》一2.2 攻击与对策