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. [leetcode] 并查集(Ⅰ)

    预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...

  2. Caused by: java.lang.ClassCastException: class java.lang.Double cannot be cast to class org.apache.hadoop.io.WritableComparable

    错误: Caused by: java.lang.ClassCastException: class java.lang.Double cannot be cast to class org.apac ...

  3. Python flask 构建可扩展的restful ap

    Flask-RESTful是flask的扩展,增加了对快速构建REST API的支持. Flask-RESTful通过最少的设置鼓励最佳的实践. pip install flask-restfulFl ...

  4. 面试官:你对Redis缓存了解吗?面对这11道面试题你是否有很多问号?

    前言 关于Redis的知识,总结了一个脑图分享给大家 1.在项目中缓存是如何使用的?为什么要用缓存?缓存使用不当会造成什么后果? 面试官心理分析 这个问题,互联网公司必问,要是一个人连缓存都不太清楚, ...

  5. Scrapy爬虫框架(2)--内置py文件

    Scrapy概念图 这里有很多py文件,分别与Scrapy的各个模块对应 superspider是一个爬虫项目 spider1.py则是一个创建好的爬虫文件,爬取资源返回url和数据 items.py ...

  6. material UI中withStyles和makeStyles的区别

      在material UI中,withStyles和makeStyles是经常使用的两个用于封装样式的函数.对于刚使用material UI的开发者而言,可能不太清楚这两者的区别.   本文简要探究 ...

  7. bdc抢夺域控

    1.运行CMD2.在 ntdsutil :提示符下输入 ntdsutil3.在 ntdsutil :提示符下输入 roles4.在 fsmo maintenance:提示符下输入 connection ...

  8. 2019 ICPC 银川网络赛 D. Take Your Seat (疯子坐飞机问题)

    Duha decided to have a trip to Singapore by plane. The airplane had nn seats numbered from 11 to nn, ...

  9. Jenkins 构建 Jmeter 项目之源代码管理(SVN)

    1.查看项目创建中是否又 svn 插件,没有的话下载插件 subversion 2.配置 svn 源代码管理,如下图(testcases 目录下包含 build.xml 和脚本文件) 3.查看 Jen ...

  10. 常用设计模式的实现,以及Netty中的设计模式

    1.观察者模式 有两个角色,观察者和被观察者.当被观察者发出消息后,注册了的观察者会收到其消息,而没有注册的观察者就不会收到. //定义观察者接口 interface Observer{ //通知观察 ...