[刘阳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实体关系映射 ...
随机推荐
- iOS开发-- RunLoop的基本概念与例子分析
看了一下,上一篇貌似5个月前的
- OCP认证之Oracle的SQL语言基础(一)
一.Oracle命令类别 数据操纵语言(DML):select;insert;delete;update;merge 数据定义语言(DDL):create;alter;drop;truncate 事物 ...
- okhttp3 post 数据打包方法
import okhttp3.OkHttpClient; import okhttp3.FormBody; import okhttp3.Request; import okhttp3.Request ...
- J2EE项目中后台定时运行的程序
转自:http://www.2cto.com/kf/201311/260676.html 在开发J2EE项目中,有时候需要在后台定时执行一些代码. 比如定时对web数据建立倒排索引.定时发送邮件.定时 ...
- mongoDB在windows下安装与配置方案
首先在官网下载mongoDB的安装包: https://www.mongodb.org/downloads 百度云盘下载:http://pan.baidu.com/s/1slUSGYp (安装版 wi ...
- Xena测试仪的自动化
Xena,Xena Networks公司的网络测试仪,也能覆盖以太网L2~L7层测试仪,但功能较简单,界面也很简洁,用起来比较直观方便. 1.Xena的自动化测试场景 测试PC上的AT框架--> ...
- 深入理解javascript原型和闭包
目录: 深入理解javascript原型和闭包(1)——一切都是对象 深入理解javascript原型和闭包(2)——函数和对象的关系 深入理解javascript原型和闭包(3)——prototyp ...
- jquery change dropdownlist selected option
<select name="corporation"> <option value="1">corporation1</optio ...
- 深入浅出讲解:php的socket通信
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问:1. 什么是TCP/IP.UDP?2. Socke ...
- PHP禁止同一IP频繁访问以防止网站被防攻击或采集的代码
PHP禁止同一IP频繁访问以防止网站被防攻击或采集的代码 <?php /* *通过禁止IP频繁访问防止网站被防攻击代码*design by www.scutephp.com*/header('C ...