动态sql简介&OGNL了解

动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处 理器相似。

MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作。

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

注意xml中的转义字符的使用

使用XML转义序列表示这些特殊的字符,这5个特殊字符所对应XML转义序列为:
& &
< &lt;
> &gt;
" &quot;
' &apos;

if标签使用

要求:查询员工,携带了哪个查询条件就带上这个字段的值

<resultMap id="EmployeeMap" type="Employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<association property="department"
select="com.wang.Dao.DepartmentMapper.getDepartmentByDid"
column="d_id"/>
</resultMap>
<!--if
choose (when, otherwise)
trim (where, set)
foreach-->
<!-- //测试if语句-->
<!--if标签里的test属性放的就是OGNL表达式-->
<!--List<Employee> getEmployeeIf(HashMap<String, Object> map);-->
<select id="getEmployeeIf" parameterType="map" resultMap="EmployeeMap">
select *
from tbl_employee
where
<if test="id!=null">
id = #{id}
</if>
<if test="lastName!=null and lastName !=''">
and Last_name = #{lastName}
</if>
<if test="gender!=null and gender != ''">
and gender = #{gender}
</if>
<if test="email!=null and email != ''">
and email = #{email}
</if>
</select>

where标签使用

在使用if时会发现我们在使用if语句时,会因为and无法自动去除而导致了后面的sql语法错误,使用where标签就可以帮助我们自动去除and

  • 解决方法1:

    在原本的sql语句的where后面加上1=1

    但if语句中必须是这样的,and + 字段

    <if test="email!=null and email != ''">
    and email = #{email}
    </if>
  • 解决方法2:

    把我们需要判断的所有语句全部放到where标签下,用以取代原来的where语句

    and任然需要放在sql语句的前面

    <select id="getEmployeeIf" parameterType="map" resultMap="EmployeeMap">
    select *
    from tbl_employee
    <where>
    <if test="id!=null">
    and id = #{id}
    </if>
    <if test="lastName!=null and lastName !=''">
    and Last_name = #{lastName}
    </if>
    <if test="gender!=null and gender != ''">
    and gender = #{gender}
    </if>
    <if test="email!=null and email != ''">
    and email = #{email}
    </if>
    </where>
    </select>

trim代替where标签

在使用where时会发现,where只能去除整句sql语句的前缀,但后缀不能去除,且不够灵活

使用trim可以自定义在整句sql语句上加上前缀和后缀,当发现整句话前或者后多了某个词也能帮助我们去除

属性:

  • prefix:整句sql语句前加上一个前缀

  • suffix:整句sql语句后加上一个后缀

  • prefixOverride:整句sql语句前多了指定的值就会被删除

  • subfixOverride:整句sql语句后多了指定的值就会被删除

<select id="getEmployeeIf" parameterType="map" resultMap="EmployeeMap">
select *
from tbl_employee
<trim prefix="where" prefixOverrides="and" suffixOverrides="and">
<if test="id!=null">
and id = #{id}
</if>
<if test="lastName!=null and lastName !=''">
and Last_name = #{lastName}
</if>
<if test="gender!=null and gender != ''">
and gender = #{gender}
</if>
<if test="email!=null and email != ''">
and email = #{email}
</if>
</trim>
</select>

choose分支查询

要求:如果带了id就用id查询,不再使用其他的查询了,如果带了last_name就用last_name查询

所以:choose-when类似于switch-case

注意点:当输入了大量值的时候,按照when的先后顺序排其次

<!--    //测试choose语句-->
<!-- List<Employee> getEmployeeChoose(HashMap<String, Object> map);-->
<select id="getEmployeeChoose" resultMap="EmployeeMap">
select *
from tbl_employee
<trim prefix="where" prefixOverrides="and">
<choose>
<when test="id!=null">
and id = #{id}
</when>
<when test="lastName!=null and lastName!=''">
and last_name = #{lastName}
</when>
<when test="gender!=null and gender!=''">
and gender = #{gender}
</when>
<when test="email!=null and email!=''">
and email = #{email}
</when>
</choose>
</trim>
</select>

set标签

要求:更新数据库的时候,传入了什么数据就更新哪些数据类似于where属性,但是他主要用于uptate的sql语句

<update id="UpdateEmployee" parameterType="map">
update tbl_employee
<set>
<if test="lastName!=null">
last_name = #{lastName},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="email!=null">
email = #{email},
</if>
</set>
where id = #{id}
</update>

trim代替set标签

<update id="UpdateEmployee" parameterType="map">
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name = #{lastName},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="email!=null">
email = #{email},
</if>
</trim>
where id = #{id}
</update>

foreach标签遍历传入参数给sql语句

<select id="getEmployeeForeach" parameterType="map" resultMap="EmployeeMap">
select *
from tbl_employee
<!--
collection:指定要遍历的集合
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历的元素赋值给指定的变量
separator:遍历出来的每个元素的分割符
open:该SQL语句拼接后在前面加上的字符
close:该sql语句拼接后在后面加上的字符
index:索引。遍历list的时候index就是索引,item就是当前值
遍历map时候index表示的就是map的key
--> <foreach collection="ids" item="item_id" separator="," open="where id in (" close=")">
#{item_id}
</foreach>
</select>

foreach实现批量保存

<insert id="insertEmployeeForeach" parameterType="list">
insert into tbl_employee
(id,last_name,gender,email,d_id)
<foreach collection="list" separator="," open="values" item="item">
(#{item.id},#{item.lastName},#{item.gender},#{item.email},#{item.department.id})
</foreach>
</insert>

mybatis的两个内置参数(提取可重用代码基础)

<select id="getEmployeeInnerParamter" parameterType="employee" resultMap="EmployeeMap">
<!--两个内置参数:
不只是方法传递过来的参数可以传递,取值。
mybatis还有两个内置参数:
_paramter:代表整个参数
单个参数时:_paramter就是这个歌参数
多个参数时:这些参数会被封装成一个map,_paramter就是封装好的map
_databaseId:如果配置了databaseIdProvider标签,_databaseId就代表数据库的别名-->
<!--这里使用databaseId参数来进行根据不同数据库,执行不同语句-->
<choose>
<when test="_databaseId=='mysql'">
<choose>
<when test="_parameter!=null">
select * from tbl_employee
</when>
</choose>
</when>
<when test="_databaseId=='oracle'">
<choose>
<when test="_parameter!=null">
select * from tbl_employee
</when>
</choose>
</when>
</choose>
</select>

bind绑定OGNL值,便于后来调用

<select id="getEmployeeBind" resultMap="EmployeeMap">
<!--bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值-->
<!--帮助我们提前拼接好lastName以便于使用模糊查询-->
<bind name="_lastName" value="'%'+lastName+'%'"/>
select * from tbl_employee where last_name like #{_lastName};
</select>

sql标签和include标签结合使用实现sql语句复用

抽取可重复使用的sql片段,方便后期维护

1、sql抽取,经常将要查询的列名,或者插入用的列名抽取出来方便使用

2、include用来引用已经抽取的sql

3、include还可以自定义一些property,sql标签内部就能使用自定义的属性

​ include-property:取值的正确方式${prop},而不能是#{prop}

<sql id="insertColumn">
id,last_name,gender,email,${testcolumn}
</sql>
<insert id="insertEmployee">
insert into tbl_employee(
<include refid="insertColumn">
<property name="testcolumn" value="abc"/>
</include>
)
values(
<include refid="insertColumn"/>
)
</insert>

mybatis-6-动态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. 如何使用Intel vtune profilier?

    如何使用Intel vtune profilier?

  2. 源码简析Spring-Integration执行过程

    一,前言 Spring-Integration基于Spring,在应用程序中启用了轻量级消息传递,并支持通过声明式适配器与外部系统集成.这一段官网的介绍,概况了整个Integration的用途.个人感 ...

  3. JAVA面向对象详细总结

    面向对象概念   所有操作基于对象进行操作实现 面向对象的三大特征   封装.继承.多态 类:具有相同特征和行为物体的统称 在java中类的定义语法: [修饰符]   class   类名{   属性 ...

  4. 「题解」POI2005 AKC-Special Forces Manoeuvres

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 P3428.官网. 题意简述 给定 \(n\) 个圆 \((x_i,y_i,r_i)\),每个圆对应一个点集 \(S ...

  5. java并发编程JUC第九篇:CountDownLatch线程同步

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口.ArrayBlockingQueue.DelayQueue.LinkedBlockingQueue.Priorit ...

  6. LuatOS | 全新在线模拟器,随时随地发挥创意

    LuatOS --运行在嵌入式硬件的实时操作系统,开启全新物联网生态. 聚焦嵌入式应用生态,内置功能可支持绝大多数物联网应用场景.深度整合Lua语言,只需少量内存和Flash空间就能运行.不局限于合宙 ...

  7. 在Centos7下安装RabbitMQ

    1.背景 不用多说,这东西好用! 2.安装 步骤一:下载安装包 链接:https://pan.baidu.com/s/1PIYI60wX6L7BtVyVft-vSA 提取码:1234 复制这段内容后打 ...

  8. 5.23考试总结(NOIP模拟2)

    5.23考试总结(NOIP模拟2) 洛谷题单 看第一题第一眼,不好打呀;看第一题样例又一眼,诶,我直接一手小阶乘走人 然后就急忙去干T2T3了 后来考完一看,只有\(T1\)骗到了\(15pts\)[ ...

  9. 【模拟8.01】big(trie树)

    一道trie树的好题 首先我们发现后手对x的操作就是将x左移一位,溢出位在末尾补全 那么我们也可以理解为现将初值进行该操作,再将前i个元素异或和进行操作,与上等同. 那么我们等于转化了问题:     ...

  10. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...