1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

2.MyBatis中用于实现动态SQL的元素主要有

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

可以看出MyBatis的动态SQL的标签元素和接近JSP中的JSTL语法,下面我就分别详细的介绍一下

3.动态SQL中if的用法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.mapping.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</resultMap> <select id="getStudentMultiple" resultMap="StudentResultMap" parameterType="Student">
select * from student where 1=1
<if test="id != 0">
and id = #{id}
</if>
<if test="sname != null">
and sname = #{sname}
</if>
</select>
</mapper>

4.动态SQL中choose用法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.mapping.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</resultMap> <select id="getStudentMultipleChoose" resultMap="StudentResultMap" parameterType="Student">
select * from student where 1=1
<choose>
<when test="id != 0">
and id = #{id}
</when>
<when test="sname != null">
and sname = #{sname}
</when>
</choose>
</select>
</mapper>

5.动态SQL中where语句的作用主要是简化SQL语句中where中的条件判断的

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.mapping.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</resultMap> <select id="getStudentMultipleWhere" resultMap="StudentResultMap" parameterType="Student">
select * from student
<where>
<if test="id != 0">
and id = #{id}
</if>
<if test="sname != null">
and sname = #{sname}
</if>
</where>
</select>
</mapper>

注意: where元素的作用是给SQL语句添加一个条件判断. 如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上

6.动态SQL中set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.mapping.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</resultMap> <update id="updateStudentMultipleSet" parameterType="Student">
update student
<set>
<if test="sname != null">
sname = #{sname},
</if>
</set>
where id = #{id}
</update>
</mapper>

7.动态SQL中foreach

  • 主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
  • foreach元素的属性主要有item,index,collection,open,separator,close
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.mapping.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id column="id" property="id"/>
<result column="sname" property="sname"/>
</resultMap> <select id="getStudentMultipleForeach" resultMap="StudentResultMap">
select * from student where id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>

注意:在Java代码中如何使用MyBaits的动态SQL语句

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import com.mybatis3.entity.Student; public class Test02 {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader; static {
try {
reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 动态SQL中foreach用法
*/
@Test
public void m05() {
SqlSession session = sqlSessionFactory.openSession();
String statement = "com.mybatis3.mapping.StudentMapper.getStudentMultipleForeach";
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
List<Student> studentList = session.selectList(statement, list);
session.close();
System.out.println(studentList);
}
}

8.动态SQL中trim语句

trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<trim prefix="where" prefixOverrides="and |or">
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
or owner = #{owner}
</if>
</trim>
</select>

[刘阳Java]_MyBatis_动态SQL标签用法_第7讲的更多相关文章

  1. [刘阳Java]_Spring对Dao的支持_第10讲

    Spring框架优秀就是在于MVC开发的时候一旦需要对底层的数据库操作,它可以很好的支持JDBC技术,还有现在主流的ORM框架(Hibernate, MyBatis)技术. 重点先介绍Spring对J ...

  2. [刘阳Java]_SpringMVC与Struts2的对比_第12讲

    今日来具体给讲讲SpringMVC与Struts2的对比,这样方便朋友们在工作中或者是面试学习中对这两者的区别有个更好的了解 把这张图放在这里,我是想说SpringMVC和Struts2真的是不一样的 ...

  3. [刘阳Java]_Spring AOP注解详细介绍_第8讲

    这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用 1. 要使用Spring AOP注解,必须满足如下的事项 导入Aspectj的jar.Spring3.0-AOP.jar.aopa ...

  4. [刘阳Java]_MyBatis_映射文件的常用标签总结_第5讲

    MyBatis中常用标签的总结,简单给出自己的总结 MyBatis映射文件中的标签使用介绍1.<select>:用于编写查询语句用的标签 id:表示当前<select>标签的唯 ...

  5. [刘阳Java]_MyBatis_常规标签的用法_第6讲

    一般MyBatis最基本标签,或者说初学者上手最快的标签就是增删改查 1.<insert>标签,在MyBatis中完成数据添加操作 <insert id="addMyUse ...

  6. [刘阳Java]_MyBatis_映射文件的select标签入门_第3讲

    1.Mybatis映射文件的<select>标签主要帮助我们完成SQL语句查询功能,<select>标签它包含了很多属性,下面简单对<select>标签的属性做一个 ...

  7. [刘阳Java]_MyBatis_映射文件的resultMap标签入门_第4讲

    <resultMap>:用于解决实体类中属性和表字段名不相同的问题 id:表示当前<resultMap>标签的唯一标识 result:定义表字段和实体类属性的对应关系 prop ...

  8. [刘阳Java]_MyBatis_注解基本用法_第10讲

    MyBatis注解提出,可以说是非常好简化了MyBatis配置文件的使用.下面我们简单地来告诉大家如何使用MyBatis的注解 定义接口 package com.gxa.dao; import jav ...

  9. [刘阳Java]_MyBatis_实体关系映射_第8讲

    MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能.下面我们就来介绍一下如何使用MyBatis的实体关系映射 1.MyBatis实体关系映射 ...

随机推荐

  1. Java基础(二) ---- 继承(Inheritance)

  2. 关于IE下用HTTPS无法下载/打开文件

    关于IE下用HTTPS无法下载/打开文件(ie8兼容模式下,ie7/ie6下有些问题.) http协议下运行正常,可以下载文件但放到https协议下就不好用一保存,IE提示:ie无法下载,请求站点不可 ...

  3. 二模12day2解题报告

    T1.笨笨玩糖果(sugar) 有n颗糖,两个人轮流取质数颗糖,先取不了的(0或1)为输,求先手能否必胜,能,输出最少几步肯定能赢:不能,输出-1. 一开始天真的写了一个dp,f[i]表示i颗糖最少取 ...

  4. 使用Asp.net WebAPI 快速构建后台数据接口

    现在的互联网应用,无论是web应用,还是移动APP,基本都需要实现非常多的数据访问接口.其实对一些轻应用来说Asp.net WebAPI是一个很快捷简单并且易于维护的后台数据接口框架.下面我们来快速构 ...

  5. 你还记得windows workflow foundation吗

    很多年前,windows workflow foundation还叫WWF,而直译过来的名称让很多人以为它就是用来开发工作流或者干脆就是审批流的. 博主当年还是个懵懂的少年,却也知道微软不会大力推一个 ...

  6. FreeMark学习(三)

    沉淀的心   freemarker学习笔记--设计指导   <# ... > 中存放所有freemaker的内容,之外的内容全部原样输出.<@ ... /> 是函数调用两个定界 ...

  7. MYSQL性能优化分享(分库分表)

    1.分库分表 很明显,一个主表(也就是很重要的表,例如用户表)无限制的增长势必严重影响性能,分库与分表是一个很不错的解决途径,也就是性能优化途径,现在的案例是我们有一个1000多万条记录的用户表mem ...

  8. Vuejs使用笔记 --- component内部实现

    现在来系统地学习一下Vue(参考vue.js官方文档): Vue.js是一个构建数据驱动的web界面的库,其目标是实现响应的数据绑定和组合的试图组件. Vue.js拥抱数据驱动的视图概念,这意味着我们 ...

  9. GPIB:永远不会被淘汰 (转载)

    发布时间:2014-07-02    来源:www.china-igbt.com 1994年5月出版的<测试与测量世界>中刊登了我冒险撰写的一篇名为<GPIB,时刻保持警惕>的 ...

  10. Python自动化 【第十五篇】:CSS、JavaScript 和 Dom介绍

    本节内容 CSS javascript dom CSS position标签 fixed: 固定在页面的某个位置 relative + absolute: 相对定位 opacity:0.5 设置透明度 ...