MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。

动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

if

mapper中编写sql,使用<if test = ' '> </if>,可以使你的接口很便捷

举个栗子:

select * from student
<if test = " id != null ">
where student.id =#{id}
</if>

一个<if>标签还是不够用的,你单独使用<if>的时候肯定还会遇到这样的问题

select * from student
where
<if test = " id != null ">
student.id = #{id}
</if>
<if test = " name != null and name != '' ">
and student.name = #{name}
</if>

如果当你的id为空时,name前面的and是没有必要的,运行会抛异常

或者当这两个<if>都为空时,只剩一个空的where,还是会报错

where

select * from student
<where>
<if test = " id != null ">
and student.id = #{id}
</if>
<if test = " name != null and name != '' ">
and student.name = #{name}
</if>
</where>
  • where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入WHERE子句。而且,若语句的开头为ANDORwhere 元素也会将它们去除。

if-else =>> choose, when, otherwise

首先,在myBatis中是不支持if-else的,想要是用if-else的话,可以使用choose代替。

choose,when,otherwise有点像Java中的switch

栗子:


<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose></select>

关于mybatis的动态sql,建议查看,中文哦官方文档

myBatis xml if、where、if-else?、foreach 心得的更多相关文章

  1. Java-MyBatis:MyBatis XML 文件

    ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...

  2. Mybatis xml mapper 特殊写法总结

    项目告一段落,业务代码也写得差不多了,框架仍然用的是 ssm ,为了省去单表数据库操作的代码编写,继续用 mybatis generator 生成单表对应的实体类.dao层映射关联及配置文件,私下还尝 ...

  3. mybatis xml配置文件要点说明

    mapper映射方式: 1 一一具体列举的方式 2扫描package 如: <mappers> <!-- 告知映射文件方式1,一个一个的配置 <mapper resource= ...

  4. 如果拷贝项目出现各种找不到文件的时候,基本就是没有标记,或者文件名的问题,Could not find resource mybatis.xml,解决方法

    Could not find resource mybatis.xml

  5. mybatis xml配置文件模版

    mybatis xml配置文件模版 1.mybatis核心配置文件书写(SqlMapConfig.xml) <?xml version="1.0" encoding=&quo ...

  6. mybatis.xml和mapper.xml的配置

    mybatis.xml和mapper.xml的配置 1.创建一个Source Folder 2.完成分包mapper和mybatis 3.创建mybatis.xml文档 4xml文档名 5.名字规范 ...

  7. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  8. 聊聊、Mybatis XML

    引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...

  9. MyBatis从入门到精通(2):MyBatis XML方式的基本用法

    本章将通过完成权限管理的常见业务来学习 MyBatis XML方式的基本用法 2.1一个简单的权限控制需求 权限管理的需求: 一个用户拥有若干角色,一个角色拥有若干权限,权限就是对某个模块资源的某种操 ...

  10. (二十一)SpringBoot之集成mybatis:使用mybatis xml

    一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

随机推荐

  1. 给COCO数据集的json标签换行

    #include <iostream> #include <fstream> #include <string> #include <vector> u ...

  2. SAP如何修改表的数据

     修改表: 事务代码:se16n  输入表名字   输入 /h 进入维护模式  修改 GD-EDIT 和 GD-SAPEDIT  内容为大写X.                          se ...

  3. vue 学习链接地址

    使用Vue.js构建Web应用程序 http://www.jdon.com/48545# 一步步带你做vue后台管理框架(一)——介绍框架 http://www.cnblogs.com/herozho ...

  4. (转)AutoML for Data Augmentation

    AutoML for Data Augmentation 2019-04-01 09:26:19 This blog is copied from: https://blog.insightdatas ...

  5. 什么时候使用“RCC_APBXPeriph_AFIO”

    什么时候需要开启复用时钟: (1)使用EXTI (2)重映射(用到外设的重映射功能时才需要使能AFIO的时钟) 举例:重映射USART2 USART2的TX/RX在PA.2/3.但是,PA.2已经被T ...

  6. mybatis逆向工程失败

    [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate ( ...

  7. 13_文件系统访问列表_case语句及脚本选项

    FACL:Filesystem Access Control List利用文件扩展保存额外的访问控制权限 setfacl: -m:设定 u:UID:perm g:GID:perm root@kali: ...

  8. [Ubuntu] 运行.AppImage格式文件

    右键Properties, Permissions勾选Allow executing file as program,如图

  9. PCA和PCoA

    讲解很详细:http://blog.genesino.com/2016/10/PCA/ PCA分析一般流程: 中心化(centering, 均值中心化,或者中位数中心化),定标(scale,如果数据没 ...

  10. spring boot2.0(二 ) lettcute访问redis

    前言 此处已经省略redis的安装,请自行百度查找redis的服务端安装过程. 1.pom文件配置: <project xmlns="http://maven.apache.org/P ...