[刘阳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实体关系映射 ...
随机推荐
- ckeditor插件
插件下载地址:http://ckeditor.com/download 1.CKeditor配置 在html页面的<head>标签中引入核心文件 ckeditor.js <scrip ...
- 2016-12-14:通过static关键字,使用类成员函数作为回调函数
#include <iostream> using namespace std; class Callee { public: void PrintInfo(int i) { cout & ...
- centos6 安装python2.7
yum -y install centos-release-scl yum -y install python27 临时生效 scl enable python27 bash 登录自动生效 cat & ...
- 浅尝辄止——在C++中调用C#的回调函数——COM方式
这种方式比较简单,给大家分享一下,同时讲一下SafeArray内定义结构体的方法 1. 需求描述 需求是这样的,C++代码和C#代码相互通信(C++一般做服务,C#做客户端),C++一侧准备好数据,然 ...
- 【MySQL】编译安装
安装所需环境: yum install cmake make glibc gcc gcc-c++ libstdc++* sysstat lrzsz libtool libxml* libtool-lt ...
- MySQL5.6安装步骤
MySQL5.6安装步骤(windows7/8_64位) 1. 下载MySQL Community Server 5.6.11 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下 ...
- 18.虚拟机linux上网问题
1.1.VMware中虚拟机网络的三种设置第一种:桥接(bridged)第二种:NAT第三种:Host only .该模式下仅主机可以上网,虚拟机不能上网. 1.2.虚拟机上网方式1:NAT方式设置步 ...
- IT在线学习网站总结
以下是我自己做软件过程中发现的一些不错的IT学习网站,个人感觉比较受用,故总结出来以供IT爱好者一起学习: www.maiziedu.com 麦子学院 www.jikexueyuan.com 极客学 ...
- rabbitMQ学习(七)反馈模式
反馈模式 在消费端接收到消息后,会反馈给服务器信息. 连接代码: import java.io.IOException; import com.rabbitmq.client.Channel; imp ...
- rabbitMQ+php
RabbitMQ与PHP(一) 项目中使用RabbitMQ作为队列处理用户消息通知,消息由前端PHP代码产生,处理消息使用Python,这就导致代码一致性问题,调整消息定义时需要PHP和Python都 ...