动态 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);

最新文章

  1. 《Linux常用命令》笔记
  2. python/matplotlib库的安装
  3. delphi 并发取数据库id问题
  4. SQLite剖析之内核研究
  5. 让dwz 在td里显示图片
  6. oracle表相关
  7. HDU 4627 The Unsolvable Problem 2013 Multi-University Training Contest 3
  8. centos 添加用户
  9. 推荐2个小工具 .NET reflector resharper
  10. javascript利用map,every,filter,some,reduce,sort对数组进行最优化处理
  11. 关于string的对象引用
  12. 【jsp 分页】mysql limit方式进行分页
  13. intellij 打开node项目 一直停留在scanning files to index....,或跳出内存不够的提示框
  14. 对象转JSON
  15. 第5章 Linux上管理文件系统
  16. django 初始命令
  17. docker 常用命令(一)
  18. 使用vs code搭建C开发环境
  19. HDU 1084:What Is Your Grade?
  20. How to Configure Eclipse for Python --- 在eclipse中如何配置pydev

热门文章

  1. elasticsearch 了解多少,说说你们公司 es 的集群架构,索 引数据大小,分片有多少,以及一些调优手段 ?
  2. Java并发机制(6)--阻塞队列
  3. 说出几条 Java 中方法重载的最佳实践?
  4. 集合流之&quot;交集(相同)和差集(区别的)&quot;的使用
  5. 一道关于压缩包的ctf题目(包括暴力破解,明文攻击,伪加密)
  6. 机器学习优化算法之EM算法
  7. 编译器如何处理C++不同类中同名函数(参数类型个数都相同)
  8. carsim2016 与 MATLAB2018 联合仿真send to simulink后编译不成功解决方法
  9. Easyx库安装教程
  10. 18个基于 HTML5 Canvas 开发的图表库