动态SQL

什么是动态SQL?

根据不同的条件生成不同的SQL语句。

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

搭建环境

create table `blog`(
`id` varchar(50) not null comment '博客id',
`title` varchar(100) not null comment '博客标题',
`auther` varchar(30) not null comment '博客作者',
`create_time` datetime not null comment '创建时间',
`views` int(30) not null comment '浏览量'
)engine =innodb default charset =utf8

创建一个基础工程

  1. 导包

  2. 编写配置文件

  3. 编写实体类

    @Data
    public class Blog {
    private int id;
    private String title;
    private String author;
    private Date date;
    private int views;
    }
  4. 编写对应的Mapper接口和MapperXML文件

IF

<select id="queryBlogIf" resultType="blog" parameterType="map">
select * from mybatis.blog where 1=1
<if test="title!=null">
and title=#{title}
</if>
<if test="auther!=null">
and auther=#{auther}
</if> </select>

choose(where,otherwise)

<choose>
<when test="title!=null">
title=#{title}
</when>
<otherwise>
and views=#{views}
</otherwise>
</choose>

trim(where,set)

<select>
select * from mybatis.blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="auther!=null">
and auther=#{auther}
</if>
</where> </select>
<update id="updataBlog" parameterType="map">
update blog
<set>
<if test="title!=null">
title=#{title},
</if>
<if test="auther!=null">
auther=#{auther}
</if>
</set>
</update>

所谓的动态SQL,本质还是SQL语句,只是在SQL层面增加逻辑代码。

**SQL片段 **

  1. 使用SQL标签抽取公共部分
<sql id="if-title-auther">
<if test="title!=null">
and title=#{title}
</if>
<if test="auther!=null">
and auther=#{auther}
</if>
</sql>
  1. 在需要使用的地方使用include标签引用

select * from mybatis.blog

注意事项:

  • 最好基于单表来定义SQL片段
  • 不要存在where标签

Foreach

Mybatis-08-动态SQL的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  4. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  5. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  6. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  7. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  8. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

  9. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  10. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

随机推荐

  1. vue : history模式与项目部署的爬坑

    需求:url不能有#符号,且不放在服务器虚拟主机的根目录. 假设放在虚拟主机的 medicine 文件夹下. 需要改两个文件,一个是 ./config/index.js (vue设置文件) ,另一个是 ...

  2. 【requests库】七个主要方法

    本文主要介绍requests库访问http的七个主要方法:get.head.post.put.patch.delete. requests.get()方法 get方法用于获取指定url的HTML网页, ...

  3. xshell如果通过跳板机登录其他机器

    首先,跳板机设置隧道 目标机器,选择刚才的隧道作为代理

  4. PHP array_fill_keys() 函数

    ------------恢复内容开始------------ 实例 用给定的指定键名的键值填充数组: <?php$keys=array("a","b",& ...

  5. Python File readline() 方法

    概述 readline() 方法用于从文件读取整行,包括 "\n" 字符.如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符.高佣联盟 w ...

  6. PHP is_writable() 函数

    定义和用法 is_writable() 函数检查指定的文件是否可写. 如果文件可写,该函数返回 TRUE. 语法 is_writable(file) 参数 描述 file 必需.规定要检查的文件. 提 ...

  7. PHP unset() 函数

    unset() 函数用于销毁给定的变量.高佣联盟 www.cgewang.com PHP 版本要求: PHP 4, PHP 5, PHP 7 语法 void unset ( mixed $var [, ...

  8. Skill 如何翻转一个list

    https://www.cnblogs.com/yeungchie/ code 发现已经有内置了reverse(l_list) unless(fboundp('reverse) procedure(y ...

  9. electron开发 - 打印流程(仅支持6.0.0版本以上)

    Electron打印 标签打印 标签打印一般有两种方式: 驱动打印,与普通打印机一样通过驱动方式打印. 通过指令打印,不同厂家的的打印机指令集不一样,可查看厂家提供的手册. electron 打印方式 ...

  10. Linux的VMWare中Centos7文件查找(find-grep)和vim文本编辑器基操

    一.find文件查找 grep 匹配字段 文件名 ——筛选文件   find查找 语法参数示例 格式: find 查找范围 查找类型 参数   find / -name *.conf   按文件名查找 ...