MyBatis的动态sql小练习,小回顾
关键字if+trim
trim可以去除多余的关键字,是where和set的组合
trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:
<select id="select03" resultMap="BaseResultMap">
select * from personDemo
<trim prefix="where" prefixOverrides="and|or">
//这里的trim意思是代替where,并且在某个条件下,把多余的and或者or去掉比如name为空的时候
//第二条age是不是会有个and,这样sql语句就错了,这时候prefixOverride就可以把and去掉
//prefixOverrides去掉前缀的and 关键字
<if test="name !=null and name!=''">
name=#{name}
</if>
<if test="age !=null and age!=''">
and age=#{age}
</if>
<if test="id !=null and id!=''">
and id=#{id}
</if>
</trim>
</select>
<update id="update02" parameterType="com.demo.mybatisdemo.bean.Person">
update personDemo
<trim prefix="set" suffixOverrides=",">
<if test="name != null and name != ''">
name=#{name},
</if>
<if test="age != null and age !=''">
age=#{age}
</if>
</trim>
where id=#{id}
</update> 标签<choose><when><otherwise>
这个类似于java里面的switch标签,首先在choose标签里面,加入判断条件<when>如果有一个when的条件符合,那么久不执行其他when标签,假如所有when标签都不符合,那么就执行
<otherwise>标签里面的内容
<update id="update03" parameterType="com.demo.mybatisdemo.bean.Person"> update personDemo
<set>
<choose>
<when test="name!=null and name.length>0">
name=#{name}
</when>
<when test="age!=null and age.length>0">
age=#{age}
</when>
<otherwise>
name='BBBBB'
</otherwise>
</choose>
</set>
where id=#{id}
</update> 还有标签<foreach>,多配合 关键字in使用
这个标签就是循环,里面有collection,item,open,separator,close
collection接口,例如传入一个数组ids,那么这个值为array,如果是list,那么这个值为list
item为传入的值的名字,如传入String[] ids 那么item为ids
open,即开头拼接的字符
close,结束拼接的字符
separator,中间分割的字符
<select id="select04" resultMap="BaseResultMap">
select * from personDemo where id in <foreach collection="array" item="ids" open="(" separator="," close=")">
#{ids}
</foreach> </select>
上面这段SQL打印出来为:select * from personDemo where id in ( ? , ? )
MyBatis的动态sql小练习,小回顾的更多相关文章
- MyBatis使用动态SQL标签的小陷阱
现在MyBatis越来越受大家的喜爱了,它的优势大家都知道,我就不多说了,直接说重点. MyBatis中提供动态SQL功能,我们可以使用<if><when><where& ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
随机推荐
- SpringSercurity基础
创建 spring 配置文件 spring-security.xml intercept-url 表示拦截页面 /* 表示的是该目录下的资源,只包括本级目录不包括下级目录 /** 表示的是该目 ...
- SOA架构设计和相关案例分析
一.SOA概念 1.定义: SOA,是一个组件模型,面向服务的体系架构,它将应用程序的不同服务通过这些服务之间定义良好的接口和契约联系起来,不涉及底层编程接口和通讯模型.服务层是SOA的基础,可以直接 ...
- Nginx系列篇三:linux中Nginx+keepalived做一个高可用的主从配置
建议:先阅读搭建Nginx负载均衡之后再看此篇 备注: Nginx+keepalived的高可用有两种方式 一.主从配置 二.双主热备配置[下一篇] 准备: 标配四台服务器 Master:192.16 ...
- display:table / display:table-cell 用法
display:table 元素会作为块级表格来显示(类似table):表格前后带有换行符: display:table-cell 元素会作为表格单元格来显示(类似<td> 和 < ...
- python中的栈
# @File: stack # 列表实现栈 class MyStack(object): def __init__(self): self.data = [] def push(self, item ...
- 洛谷 P2398 GCD SUM || uva11417,uva11426,uva11424,洛谷P1390,洛谷P2257,洛谷P2568
https://www.luogu.org/problemnew/show/P2398 $原式=\sum_{k=1}^n(k\sum_{i=1}^n\sum_{j=1}^n[(i,j)=k])$ 方法 ...
- 关于list,字符串的小记录
1.关于操作list的命令: a.append("hi") 这个可以在list的最后一项加上个这个字符串"hi",a是list的名字. del a[3] 删去l ...
- AO-XXXX
一 AO4419:应用于开关应用或PWM应用的场效应管.
- [转]利用telnet进行SMTP的验证
本文转自:http://www.cnblogs.com/rootq/articles/1320266.html [crazywill@localhost crazywill]$ telnet #tel ...
- c#内存管理,垃圾回收和资源释放
<1>关于虚拟内存的概念 Windows使用一个虚拟寻址系统,该系统把程序可用的内存地址映射到硬件内存中的实际地址上去,这些任务完全由windows后台管理,其实际结果是32位处理机上的每 ...