[刘阳Java]_MyBatis_动态SQL标签用法_第7讲
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讲的更多相关文章
- [刘阳Java]_Spring对Dao的支持_第10讲
Spring框架优秀就是在于MVC开发的时候一旦需要对底层的数据库操作,它可以很好的支持JDBC技术,还有现在主流的ORM框架(Hibernate, MyBatis)技术. 重点先介绍Spring对J ...
- [刘阳Java]_SpringMVC与Struts2的对比_第12讲
今日来具体给讲讲SpringMVC与Struts2的对比,这样方便朋友们在工作中或者是面试学习中对这两者的区别有个更好的了解 把这张图放在这里,我是想说SpringMVC和Struts2真的是不一样的 ...
- [刘阳Java]_Spring AOP注解详细介绍_第8讲
这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用 1. 要使用Spring AOP注解,必须满足如下的事项 导入Aspectj的jar.Spring3.0-AOP.jar.aopa ...
- [刘阳Java]_MyBatis_映射文件的常用标签总结_第5讲
MyBatis中常用标签的总结,简单给出自己的总结 MyBatis映射文件中的标签使用介绍1.<select>:用于编写查询语句用的标签 id:表示当前<select>标签的唯 ...
- [刘阳Java]_MyBatis_常规标签的用法_第6讲
一般MyBatis最基本标签,或者说初学者上手最快的标签就是增删改查 1.<insert>标签,在MyBatis中完成数据添加操作 <insert id="addMyUse ...
- [刘阳Java]_MyBatis_映射文件的select标签入门_第3讲
1.Mybatis映射文件的<select>标签主要帮助我们完成SQL语句查询功能,<select>标签它包含了很多属性,下面简单对<select>标签的属性做一个 ...
- [刘阳Java]_MyBatis_映射文件的resultMap标签入门_第4讲
<resultMap>:用于解决实体类中属性和表字段名不相同的问题 id:表示当前<resultMap>标签的唯一标识 result:定义表字段和实体类属性的对应关系 prop ...
- [刘阳Java]_MyBatis_注解基本用法_第10讲
MyBatis注解提出,可以说是非常好简化了MyBatis配置文件的使用.下面我们简单地来告诉大家如何使用MyBatis的注解 定义接口 package com.gxa.dao; import jav ...
- [刘阳Java]_MyBatis_实体关系映射_第8讲
MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能.下面我们就来介绍一下如何使用MyBatis的实体关系映射 1.MyBatis实体关系映射 ...
随机推荐
- NOIP2008 传纸条
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...
- java httpclient cookie
BasicCookieStore cookieStore = new BasicCookieStore();BasicClientCookie cookie = new BasicClientCook ...
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
连接器LNK是通过调用cvtres.exe完成文件向coff格式的转换的,所以出现这种错误的原因就是cvtres.exe出现了问题. 在电脑里面搜索一下cvtres.exe,发现存在多个文件,使用最新 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
- 《JavaScript高级程序设计》学习笔记(2)--JS运算符详解
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 思维导图 前面对JS的运算符的操作很多细节的东西没有提及,今天给大家分享一张网上找的思维导图,对这一部 ...
- oracle基础语法大全
-----创建序列create sequence book_idINCREMENT BY 1 -- 每次加几个 START WITH 001 -- 从1开始计数 NOMAXVALUE -- 不设置最大 ...
- For Your Dream
队名:Braveheart 队员介绍: 队长:李洋洋 队员:姚欢,杨仁波,张波,乔闯 项目名称:数据沈航 总体任务: 收集整理学校的数据,为每个想要了解沈航的人展现一份我们收集来的信息 项目分组: ( ...
- 【SSM 8】spring集成Mybatis通用Mapper
上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件. 备注:本项目通过maven管理 关键版本说明: spring:4.1.3.RELEASE ...
- java学习第14天(集合的框架和基本遍历)
今天主要是接触了集合的概念,集合简单意义上来说就是类对象的集合,我们一般用Collection 这个接口来表示,集合主要体系为: Collection |--List |--ArrayList |-- ...
- 转:MyBean的安装
1.下载MyBean源码包.可以到https://git.oschina.net/ymofen/delphi-framework-MyBean下载Zip压缩包,也可以用Git客户端下载. 2.将框架源 ...