动态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. 微信小程序开发(一)基础知识学习

    1.特点:   ①无DOM对象(虚拟DOM),一切基于组件化(复用.解耦) ②四个重要文件: *.js.*.wxml --> html..wxss --> css.*.json ③无需下载 ...

  2. Java代码片段

    type 为Java中的任意数据类型,包括基本类型和组合类型,arrayName为数组名,必须是一个合法的标识符,[ ] 指明该变量是一个数组类型变量.例如: 这两种形式没有区别,使用效果完全一样,读 ...

  3. MacOS工具

    原文是"池建强"的微信文章,公众号为"MacTalk" 1. Alfred 2. iTerm2 一些基本功能如下: 分窗口操作:shift+command+d( ...

  4. matplotlib基础汇总_04

    3D图形 导包 import numpy as np import matplotlib.pyplot as plt #3d图形必须的 from mpl_toolkits.mplot3d.axes3d ...

  5. PHP 5 echo 和 print 语句

    PHP 5 echo 和 print 语句 在 PHP 中有两个基本的输出方式: echo 和 print. 本章节中我们会详细讨论两个语句的用法,并在实例中演示如何使用 echo 和 print. ...

  6. PHP attributes() 函数

    实例 返回 XML 的 body 元素的属性和值: <?php$note=<<<XML<note><to>Tove</to>高佣联盟 www ...

  7. Blob分析之board _components.hdev

    *用立体方法分割板子组件的示例程序*Application program to illustrate the segmentation* of board _components.hdev  wit ...

  8. 《分享》Graphql入门与实践

    最近项目用到了graphql,学习了一些并在公司做了一个小分享,希望对你有帮助 一.介绍 Graphql是一种面向数据的API查询语言 Graphql给前端提供一种强力的查询工具,我们可以根据自己定义 ...

  9. SpringBoot+Dynamic多数据源动态切换

    最近做了个小模块,需求就是项目同时读取三个数据库,操作数据.并不是分库分表,只用定时跑,不需要对外提供接口. 技术选型:SpringBoot + Mybatis Plus(Mybatis) + Dyn ...

  10. C++文件操作和模板

    1.数据层次 位 bit 字节 byte 域/记录 将所有记录顺序地写入一个文件---->顺序文件:一个有限字符构成的顺序字符流 C++标准库中:ifsteam,ofstream,fstream ...