动态sql

常见的几种:trim、where、set、foreach、if、choose、when

下面通过案例一一演示


if语法

 <select id="selectIfTest1" resultType="cn.kgc.mybatisdemo.mode.User">
select id , userCode , userName from smbms.smbms_user where 1=1
<if test="userName !=null and userName != '' ">
and userName like concat('%',#{userName},'%')
</if>
<if test="roleId != 0">
and userRole = #{roleId}
</if>
</select>

if模块在判断通过后拼接模块内的代码

接下来是where代码

 <select id="selectIfTest2" resultType="cn.kgc.mybatisdemo.mode.User">
select id , userCode , userName from smbms.smbms_user
<where>
<if test="userName != null and userName != ''">
and userName like concat('%',#{userName},'%')
</if>
<if test="roleId != 0">
and roleId = #{roleId}
</if>
</where>
</select>

/*
where标签代替了where关键字,他除了能给添加关键字同时,
也可以智能的出去多余的and和or,where标签一般和if一起使用
当where标签中的if条件都不满足要求的时候,where标签不会拼接关键字
*/

set标签的使用

 <update id="updateUserTest3">
update smbms.smbms_user
<set>
<if test="userName != null and userName != ''">
userName = #{userName},
</if>
<if test="userCode != null and userCode != ''">
userCode = #{userCode},
</if>
<if test="userPassword != null and userPassword != ''">
userPassword = #{userPassword},
</if>
<if test="modifyDate != null" >
modifyDate = now(),
</if>
</set>
</update>

/*
set标签 代替set关键字,可以智能的删除多余的逗号
其他内容和where一样,修改的时候必须要传值,否则会报错,这样就没意义了
*/

trim的使用

<update id="updateUserTest4">
update smbms_user <trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
<if test="userName != null and userName != ''">
userName = #{userName},
</if>
<if test="userCode != null and userCode != ''">
userCode = #{userCode},
</if>
<if test="userPassword != null and userPassword != ''">
userPassword = #{userPassword},
</if>
<if test="modifyDate != null" >
modifyDate = now(),
</if>
</trim>
</update>

/*
prefix添加前缀
prefixOverride清除前缀’
suffix添加后缀
suffixOverride清除后缀

trim 迄今为止最好用的标签
*/

foreach的三种形式

<select id="selectUserByForeachArray" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user
where userRole in
/*
collection:当前的参数类型(必填)
open:开始要添加的字符
separator:分隔符
close:结束时要添加的字符
item:相当于变量
*/
<foreach collection="array" open="(" separator="," close=")" item="roleId">
#{roleId}
</foreach>
</select>

第二种

    <select id="selectUserByList" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user where userRole in
<foreach collection="list" item="roleId" open="(" separator="," close=")">
#{roleId}
</foreach>
</select>

第三种

    <select id="selectUserByMap" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user where userRole in
/*
这里的collection的值放的是Map集合的键
*/
<foreach collection="roleId" open="(" separator="," close=")" item="roleId">
#{roleId}
</foreach>
and gender = #{gender}
</select>

choose的使用

    <select id="selectUserByChoose" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user
/*
choose类似于java中的switch when相当于case,只要走一个分支就不进入其他分支
otherwise是默认,如果上面条件不满足就走它
*/
<where>
<choose>
<when test="id != 0 and id != null">
id = #{id}
</when>
<when test="userName != null and userName != '' ">
userName like concat('%',#{userName},'%')
</when>
<when test="gender">
gender = #{gender}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>

Mybatis使用动态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. 03 我的第一个html页面

    <!--定义文档的类型,一个html就是一个文档--> <!DOCTYPE html> <html lang="en"> <!--head ...

  2. 视频直播技术之iOS端推流

    随着网络基础建设的发展和资费的下降,在这个内容消费升级的时代,文字.图片无法满足人们对视觉的需求,因此视频直播应运而生.承载了实时性Real-Time和交互性的直播云服务是直播覆盖各行各业的新动力.网 ...

  3. 论文研读Unet++

    Unet++: A Nested U-Net Architecture for Medical Image Segmentation Unet++ 论文地址 这里仅进行简要介绍,可供读者熟悉其结构与特 ...

  4. NumPy基础操作

    NumPy基础操作(1) (注:记得在文件开头导入import numpy as np) 目录: 数组的创建 强制类型转换与切片 布尔型索引 结语 数组的创建 相关函数 np.array(), np. ...

  5. Oracle 聚集函数

    (1)avg(x):返回x的平均值 select avg(grade) from sc; (2)count(x):返回统计的行数 select count(name) from sc; (3)max( ...

  6. Python爬虫入门:爬取pixiv

    终于想开始爬自己想爬的网站了.于是就试着爬P站试试手. 我爬的图的目标网址是: http://www.pixiv.net/search.php?word=%E5%9B%9B%E6%9C%88%E3%8 ...

  7. 大规模SDN云计算数据中心组网的架构设计

    本文首先分析了在大规模SDN数据中心组网中遇到的问题.一方面Underlay底层组网规模受限于设备实际的转发能力和端口密度,单一Spine-leaf的Fabric架构无法满足大规模组网的需求:另一方面 ...

  8. Python题整理

    1.Python的多态.鸭子类型? 2.Python的序列化工具有哪些? 3.Python处理Excel的工具有哪些? 4.Python处理Graph的算法库有哪些? 5.Python的dict是基于 ...

  9. PTP 接线方式及通讯距离

    PTP 接线方式 CB 1241 RS485 接线 (6ES7 241 1CH30-1XB0) CB1241 RS485 信号板(安装在CPU机本体上) ,订货号为: 6ES7 241 1CH30-1 ...

  10. Python入门基础(2)

    如果你是博客园团队,,看到不符合您们要求的地方可否指出来?不要你不符合要求,然后我不符合哪项要求?是要我自己去找么? python条件语句 首先介绍的是if语句,python中的if语句格式如下: i ...