mapper:

public interface BlogMapper {

    List<Blog> getBlogByIF(Map map);

}

IF

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

if标签会根据你传入的值去匹配符合条件的if语句

where标签会根据你的传入的值动态的帮助你去掉and

比如:

你传入了title="xxx",那么它的SQL语句会是 select * from blog where title = (你传入的值)

SQL片段

SQL片段是放置重复的语句,提高重用性

例如上述的代码中的

<if test="title != null">
  and title = #{title}
</if>
<if test="author != null">
   and author = #{author}
</if>

此时,我们可以用<sql></sql>标签 id是唯一标识

<sql id="select">
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>

则我们的查询语句可以简化成下面的样子

<select id="getBlogByIF" resultType="blog" parameterType="map">
select * from mybatis.blog
<include refid="select"></include>
</select>

refid就是引用上述sql标签中的唯一标识

choose  when otherwise

mapper:

public interface BlogMapper {

    List<Blog> getBlogByChoose(Map map);

}
<select id="getBlogByChoose" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<choose>
<when test="title!=null">
title = #{title}
</when>
<when test="author!=null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>

其实就是相当于java中的switch语句 

传入了 “title” 就按 “title” 查找,传入了 “author” 就按 “author” 查找的情形。若两者都没有传入,就返回标记为按“views”查找的 BLOG

Set

mapper:

public interface BlogMapper {
int updateBlog(Map map);
}
<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>

<set>标签会动态的帮你去掉逗号

ForEach

mapper:

public interface BlogMapper {
List<Blog> getBlogForEach(Map map);
} //原始的sql为
//select * from mybatis.blog where (id=1 or id=2 or id=3);
<select id="getBlogForEach" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="list" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>

测试类:

@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map map = new HashMap();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
map.put("list",list);
List<Blog> blogs = mapper.getBlogForEach(map);
for (Blog blog : blogs) {
System.out.println(blog);
} sqlSession.close();
}

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。

提示 

你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach

当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

mybatis中的动态SQL(IF Chooes When Where Set ForEach SQL片段)的更多相关文章

  1. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...

  2. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  3. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  4. mybatis 05: mybatis中的动态代理

    mybatis中动态代理存在的意义 图示 图示分析 分层说明:界面层为第一层,业务逻辑层(接口层 + 实现层)为第二层,数据访问层(接口层 + 实现层)为第三层 业务逻辑层和数据访问层:分别分两层来开 ...

  5. mybatis中的动态SQL语句

    有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...

  6. mybatis中实现动态SQL

    动态SQL语句,也就意味着SQL语句不在是一成不变的而是具有多样性. if if的用法还是跟平常差不多的(不过没有else if也没有else) <update id="modify& ...

  7. 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签

    创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...

  8. mybatis中的动态代理应用(mapper对象)

    -----------------UserMapper的配置信息--------------------- <?xml version="1.0" encoding=&quo ...

  9. mybatis中#{}与${}的差别(如何防止sql注入)

    默认情况下,使用#{}语法,MyBatis会产生PreparedStatement语句中,并且安全的设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义. # ...

随机推荐

  1. 2019-2020-1 20199325《Linux内核原理与分析》第四周作业

    start_kernel函数的执行过程 asmlinkage __visible void __init start_kernel(void) { char *command_line; char * ...

  2. [SVN] Couldn't perform atomic initialization

    svn: Commit failed (details follow): svn: Couldn't perform atomic initialization It was because the ...

  3. [Inno Setup] 退出安装程序的两种方式

    1. 完全静默的退出 procedure ExitProcess(exitCode:integer); external 'ExitProcess@kernel32.dll stdcall'; ... ...

  4. Scala教程之:Enumeration

    Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enumeration的定义: abstract class Enumeration (init ...

  5. 如何装双系统win10下装Ubuntu

    如何装双系统win10下装Ubuntu 第一步 制作启动盘 下载UItraISO软件.下载Ubuntu系统(地址:https://www.ubuntu.com/download).准备一个大于8g的U ...

  6. Vue集成tinymce富文本编辑器并实现本地化指南(2019.11.21最新)

     tinymce是一款综合口碑特别好.功能异常强大的富文本编辑器,在某些网站,甚至享有"宇宙最强富文本编辑器"的称号.那么,在Vue项目中如何集成呢?这并不困难,只需要参照官方教程 ...

  7. mysql 5.7 MGR

    最近看了一下mysql5.7的MGR集群挺不错的,有单主和多主模式,于是乎搭建测试了一下效果还不错,我指的不错是搭建和维护方面都比较简单.网上绝大多数都是单主模式,当然我这里也是,为了加深印象,特意记 ...

  8. 在服务器本地监控服务端口命令之ss

    在服务器本地监控服务端口命令之ss 当服务器的socket连接数量变得非常大时,无论是使用netstat命令还是直接cat /proc/net/tcp,执行速度都会很慢.可能你不会有 切身的感受,但当 ...

  9. Jenkins+Jmeter+Ant   生成测试报告

    <?xml version="1.0" encoding="UTF-8"?> <project name="wms_test&quo ...

  10. 小猪的Python学习之旅 —— 16.再尝Python数据分析:采集拉勾网数据分析Android就业行情...

    一句话概括本文: 爬取拉钩Android职位相关数据,利用numpy,pandas和matplotlib对招人公司 情况和招聘要求进行数据分析. 引言: 在写完上一篇<浅尝Python数据分析: ...