动态 SQL

目的:为了摆脱在不同条件拼接 SQL 语句的痛苦

在不同条件在生成不同的SQL语句

本质上仍然是SQL语句,不过是多了逻辑代码去拼接SQL,只要保证SQL的正确性按照格式去排列组合

可以先写好SQL语句

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

if,          where(可以自动去除多余的and)

    <select id="queryBlog_if"  parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title !=null">
and title=#{title}
</if>
<if test="author !=null">
and author=#{author}
</if>
</where>
</select>

choose(when, otherwise)

从多个条件中选择一个使用,有点像 Java 中的 switch 语句

    <select id="queryBlog_choose" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title !=null ">
title=#{title}
</when>
<when test="author!=null ">
author=#{author}
</when>
<!-- <otherwise>-->
<!-- and views=#{views}-->
<!-- </otherwise>-->
<!-- 没有otherwise的时候可以不用传值,有的话得至少传一个值--> </choose>
</where>
</select>

set(可以去除多余的逗号,  )    [update]

    <update id="updateBlog" parameterType="map" >
update mybatis.blog
<set>
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if> </set>
where id=#{id}
</update>

SQL片段(提取共有的SQL语句,达到便捷复用的作用)  [尽量只要if判断的就行]

    <sql id="oo_sb">
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if>
</sql> <!-- 然后就用下面方式,写到原有的位置--> <include refid="oo_sb"></include>
  
 foreach

    <select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open="id in (" separator="," close=")">
#{id_}
</foreach>
</where>
</select> SQL:select * from mybatis.blog where id in(1,2,3) ids=通过万能map传入的集合名称
item=遍历的每个元素的值
open 前缀
separator 分隔
close 后缀
#{iid} 前缀和后缀里面的东西 等于遍历的元素
collection取值不用加#{} 方法二: <select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open=" (" separator="or" close=")">
id=#{id_}
</foreach>
</where>
</select> SQL:select * from mybatis.blog where(id=? or id=?)

//测试代码片段

        HashMap map = new HashMap();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> blogs= mapper.queryBlogget1_3(map);

动态SQL常用标签的更多相关文章

  1. Mybatis学习笔记之---动态sql中标签的使用

    动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...

  2. 动态SQL之标签

    本节主要讲了动态SQL的几个标签:where set trim where: 检出where语句的最前面是否含有AND和一个空格 或者 or和一个空格 ,如果有的话删除 set: 检出set的最后是否 ...

  3. 动态SQL各个标签作用以及注意事项详解

    创建com.mybatis包,包含:UserMapper.xml和mybatis-config.xml UserMapper.xml代码: <?xml version="1.0&quo ...

  4. MyBatis动态SQL foreach标签实现批量插入

    需求:查出给定id的记录: <select id="getEmpsByConditionForeach" resultType="com.test.beans.Em ...

  5. Mybatis 最强大的动态sql <where>标签

    <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...

  6. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  7. [刘阳Java]_MyBatis_动态SQL标签用法_第7讲

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

  8. Mybatis:缓存,动态SQL,注解SQL以及动态标签使用

    1 转义字符 字符 转义 描述 < < 小于 <= <= 小于等于 > > 大于 >= >= 大于等于 <> <> 不等于 &a ...

  9. [Mybatis]Mybatis 常用标签及功能整理

    Mybatis中生成动态SQL的标签有四类,分别是: if choose (when, otherwise) trim (where, set) foreach 1.if 当需要动态生成where条件 ...

随机推荐

  1. spring学习四:springMVC

    ref:http://www.cnblogs.com/ysocean/tag/SpringMVC%E5%85%A5%E9%97%A8%E7%B3%BB%E5%88%97/ Spring MVC的处理流 ...

  2. 为什么Java中 wait 方法需要在 synchronized 的方法中调用?

    另一个棘手的核心 Java 问题,wait 和 notify.它们是在有 synchronized 标记的方法或 synchronized 块中调用的,因为 wait 和 modify 需要监视对其上 ...

  3. XMLBeanFactory?

    最常用的就是org.springframework.beans.factory.xml.XmlBeanFactory ,它根据XML文件中的定义加载beans.该容器从XML 文件读取配置元数据并用它 ...

  4. memcached 最大能存储多大的单个 item?

    1MB.如果你的数据大于 1MB,可以考虑在客户端压缩或拆分到多个 key 中. 为什么单个 item 的大小被限制在 1M byte 之内? 简单的回答:因为内存分配器的算法就是这样的. 详细的回答 ...

  5. synchronized使用及原理解析

    修饰静态方法.实例方法.代码块 Synchronized修饰静态方法,对类对象进行加锁,是类锁. Synchronized修饰实例方法,对方法所属对象进行加锁,是对象锁. Synchronized修饰 ...

  6. 处理器映射器(HandlerMapping)及处理器适配器(HandlerAdapter)详解(二)

    注解的 处理器映射器 和 处理器适配器 介绍 注解的映射器: 在 Spring3.1 之前使用 DefaultAnnotationHandlerMapping 注解映射器(根据 DispatcherS ...

  7. 用一个文件,实现迷你 Web 框架

    当下网络就如同空气一样在我们的周围,它以无数种方式改变着我们的生活,但要说网络的核心技术变化甚微. 随着开源文化的蓬勃发展,诞生了诸多优秀的开源 Web 框架,让我们的开发变得轻松.但同时也让我们不敢 ...

  8. 220v-5v稳压电路

    5V整流电路原理 先对电路进行整流 整流电路:利用单向导电器件将交流电转换成脉动直流电路,再用电容进行滤波 滤波电路:利用储能元件(电感或电容)把脉动直流电转换成比较平坦的直流电,然后对电路进行稳压 ...

  9. 破界!Omi生态omi-mp发布,用小程序开发生成Web

    omi-mp 是什么 Omi 框架是微信支付线研发部和 AlloyTeam 开源的通用 Web 组件化框架,基于 Web Components,用来开发 PC.手机浏览器或者微信.手Q webview ...

  10. SimpleDateForma求日期,2008-11月第6周星期日是几号?

    题目4: 巧妙利用SimpleDateFormat根据各种信息求日期.2008-11月第6周的星期日是几号? import java.text.ParseException;import java.t ...