动态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. 分布式数据库中间件 MyCat 搞起来!

    关于 MyCat 的铺垫文章已经写了三篇了: MySQL 只能做小项目?松哥要说几句公道话! 北冥有 Data,其名为鲲,鲲之大,一个 MySQL 放不下! What?Tomcat 竟然也算中间件? ...

  2. spark 源码分析之四 -- TaskScheduler的创建和启动过程

    在 spark 源码分析之二 -- SparkContext 的初始化过程 中,第 14 步 和 16 步分别描述了 TaskScheduler的 初始化 和 启动过程. 话分两头,先说 TaskSc ...

  3. MyBatis从入门到精通(五):MyBatis 注解方式的基本用法

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. @Select 注解 1.1 使 ...

  4. Flink中的状态与容错

    1.概述 Flink支持有状态计算,根据支持得不同状态类型,分别有Keyed State和Operator State.针对状态数据得持久化,Flink提供了Checkpoint机制处理:针对状态数据 ...

  5. kafka搭建相关可能出现的bug

    在Kafka搭建时,首先安装zookeeper,新版本直接解压,启动就好了.由于什么原因,在虚拟机下,必须用root账户启动zookeeper,不然其中一个文件由于没有权限无法创建,导致zookeep ...

  6. 100天搞定机器学习|Day7 K-NN

    最近事情无比之多,换了工作.组队参加了一个比赛.和朋友搞了一些小项目,公号荒废许久.坚持是多么重要,又是多么艰难,目前事情都告一段落,我们继续100天搞定机器学习系列.想要继续做这个是因为,一方面在具 ...

  7. 详解FIX协议的原理、消息格式及配置开发

    一.定义 FIX协议是由国际FIX协会组织提供的一个开放式协议,目的是推动国际贸易电子化的进程,在各类参与者之间,包括投资经理.经纪人,买方.卖方建立起实时的电子化通讯协议.FIX协议的目标是把各类证 ...

  8. scrapy基础知识之 CrawlSpiders:

    通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl spidername xx.com LinkExtractors class sc ...

  9. 关于网页授权access_token和普通access_token的区别

    关于网页授权access_token和普通access_token的区别 1.微信网页授权是通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取到一个网页授权特有的接口调用凭证(网页授 ...

  10. C#实现某一属性值变化时触发事件

    在我们做工业软件中,经常会遇到要实时监控某一点,在这个点变化时去做一些事情 放入程序里呢,就是要实时监控某一属性的值,当值发生变化时触发事件,其核心就是借助属性的Set方法,来判断当前set的值是否与 ...