动态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. m3u8解析、转码、下载、合并

    m3u8解析.转码.下载.合并 现在网也上大多数视频需要下载都很麻烦,极少数是MP4,大多都是m3u8, 先说视频下载, pc端: 打开网页,点击视频播放,打开开发者工具,找到网络那一栏, 等整个网页 ...

  2. Hexo+NexT(一):在Windows下安装Hexo+NexT及搭建博客

    阅读本篇之前,假定读者已经有了Node.js的基础,如需要补充Node.js知识的,请自行百度. Hexo是在Node.js框架下的一个项目,利用Node.js提供的强大功能,完成从Markdown到 ...

  3. vi,etc目录文件,环境变量,别名功能

      1 vi命令使用技巧补充 1.1 如何快速编辑文本内容 yy ---快速复制文件内容 3yy ---复制三行内容 p ---快速粘贴文本内容 3p ---粘贴三行内容 dd ---快速删除文件内容 ...

  4. Storm —— 集群环境搭建

    一.集群规划 这里搭建一个3节点的Storm集群:三台主机上均部署Supervisor和LogViewer服务.同时为了保证高可用,除了在hadoop001上部署主Nimbus服务外,还在hadoop ...

  5. python 基础学习笔记(2)---字符串功能函数

    **上一篇写到了,基本的数据类型,今天重点来讲一下字符串的功能函数**回顾一下上篇的内容:一.int 整型,在python 3 中与long型合并 可以达到 -9223372036854775808- ...

  6. javascript——原型与原型链

    一.prototype 在JavaScript中,每个函数都有一个prototype属性,这个属性指向函数的原型对象. 例如: function Person(age) { this.age = ag ...

  7. HDU 4444:Walk(思维建图+BFS)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给出一个起点一个终点,给出n个矩形的两个对立顶点,问最少需要拐多少次弯可以从起点到达终点,如果不能输 ...

  8. 阿里系手淘weex学习第一天

    官网原文:https://weex.apache.org/zh/tools/extension.html#功能 功能 创建Weex项目. 支持在VSCode对Weex的语法支持. 检查iOS和Andr ...

  9. RedisDesktopManager远程连接Linux系统的Redis服务

    linux下安装redis :https://www.runoob.com/redis/redis-install.html 进入 src 运行redis   : ./redis-server 打开另 ...

  10. AWS S3 上传文件

    一.获取签名的URL 通过后端给的接口拿到已经签名好的文件上传的URL地址 二.读取文件(注:AWS 接受的二进制,不能使用form-data) // 获取文件二进制 getFileMd5 = (ke ...