MyBatis-12-动态SQL
12、动态SQL
什么事动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句
利用动态SQL这一特性可以彻底摆脱这种痛苦
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
搭建环境
CREATE TABLE `bolg`(
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) not null comment '博客标题',
`author` VARCHAR(30) not null comment '博客作者',
`creat_time` datetime not null comment '创建时间',
`views` int(30) not null comment '浏览量'
)ENGINE=InnoDB DEFAULT CHARSET=utf8
创建一个基础工程
导包
编写配置文件
编写实体类
@Data
public class Blog {
private int id;
private String title;
private String author;
private Date creatTime;
private int views;
}
编写实体类对应的Mapper接口和Mapper.xml
IF
<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
@Test
public void queryBlogIF(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("author","尹锐");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
choose (when, otherwise)
<select id="queryBlogChoose" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<choose>
<when test="title != null">
title=#{title}
</when>
<when test="author!=null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
trim, (where, set)
select * from mybatis.bolg
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
<update id="updateBlog" parameterType="map">
update mybatis.bolg
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author},
</if>
</set>
where id = #{id}
</update>
所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,去执行一些逻辑代码
if
Where,set,choose,when
SQL片段
有的时候,我们可能会将一些公共的部分抽取出来,方便复用!
使用SQL标签抽取公共的部分
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
在需要使用的地方使用Include标签引用即可
<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<include refid="if-title-author"></include>
</where>
</select>
注意事项:
- 最好基于单表来定义SQL片段!
- 不要存在where或者set标签,片段里尽量只有if就好了
Foreach
select * from user where 1=1 and
<foreach item="id" index="index" collection="ids"
open="(" separator="or" close=")">
#{id}
</foreach> (id=1 or id=2 or id=3)
1.
<!--
select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)
我们现在传递一个万能的map,这个map中可以存在一个map
-->
<select id="queryBlogForeach" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了
建议:
- 先在Mysql中写出完整的SQL,在对应的去修改称为我们的动态SQL
MyBatis-12-动态SQL的更多相关文章
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
随机推荐
- C/C++编程
基本的数据类型: 整型 浮点型(单精度.双精度) 在c语言中,所有的变量声明必须在任何执行语句之前(对当前域来说), 否则编译的时候会出现变量是未声明的标识符的错误. main 入口参数:argc 和 ...
- 【VS开发】QueryPerformanceFrequency与QueryPerformanceCounter的使用
LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 Q ...
- NDK学习笔记-使用现有so动态库
前面将的都是如何使用C/C++文件生成so动态库,那么在使用别人的so动态库的时候应该怎么做呢?这篇文章就是使用一个变声功能的动态库,完成对于以有so动态库的说明. 动态库来源 在互联网中,有着许许多 ...
- 【转】GMM与K-means聚类效果实战
原地址: GMM与K-means聚类效果实战 备注 分析软件:python 数据已经分享在百度云:客户年消费数据 密码:lehv 该份数据中包含客户id和客户6种商品的年消费额,共有440个样本 正文 ...
- Gradle DSL Walle渠道包后安装启动APP
DSL(Domain-Specific Language) Gradle 是一个编译打包工具,但实际上它也是一个编程框架. Task 是 Gradle 中的一种数据类型,它代表了一些要执行或者要干的工 ...
- B8 Concurrent JDK中的乐观锁与原子类
[概述] 乐观锁采用的是一种无锁的思想,总是假设最好的情况,认为一个事务在读取数据的时候,不会有别的事务对数据进行修改,只需要在修改数据的时候判断原数据数据是否已经被修改了.JDK 中 java.ut ...
- IPv4
1.IPv4分类地址 PC0(192.168.0.1) 和PC1(172.168.0.1)如果要ping通的话需要设置各自网关 PC0 设置IP 和 默认网关=路由器设置IP 2.Gigabit ...
- DP_Wooden Sticks
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...
- Numbers(CodeForces-128D)【思维/list】
题目链接:https://vjudge.net/problem/CodeForces-128D 题意:给出一组数,要求将这些数排列成一个环,满足每相邻两个数的差值为1,问能否完成. 思路:先取出最小的 ...
- 9.ssh登录慢
修改方式:使用root权限修改ssh的配置文件,vim /etc/ssh/sshd_config增加一行记录:UseDNS no修改GSSAPIAuthentication参数为 no,默认是yesP ...