动态SQL常用标签
动态 SQL
目的:为了摆脱在不同条件拼接 SQL 语句的痛苦
在不同条件在生成不同的SQL语句
本质上仍然是SQL语句,不过是多了逻辑代码去拼接SQL,只要保证SQL的正确性按照格式去排列组合
可以先写好SQL语句
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
if, where(可以自动去除多余的and)


<select id="queryBlog_if" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title !=null">
and title=#{title}
</if>
<if test="author !=null">
and author=#{author}
</if>
</where>
</select>
choose(when, otherwise)
从多个条件中选择一个使用,有点像 Java 中的 switch 语句


<select id="queryBlog_choose" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title !=null ">
title=#{title}
</when>
<when test="author!=null ">
author=#{author}
</when>
<!-- <otherwise>-->
<!-- and views=#{views}-->
<!-- </otherwise>-->
<!-- 没有otherwise的时候可以不用传值,有的话得至少传一个值--> </choose>
</where>
</select>
set(可以去除多余的逗号, ) [update]


<update id="updateBlog" parameterType="map" >
update mybatis.blog
<set>
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if> </set>
where id=#{id}
</update>
SQL片段(提取共有的SQL语句,达到便捷复用的作用) [尽量只要if判断的就行]


<sql id="oo_sb">
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if>
</sql> <!-- 然后就用下面方式,写到原有的位置--> <include refid="oo_sb"></include>


<select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open="id in (" separator="," close=")">
#{id_}
</foreach>
</where>
</select> SQL:select * from mybatis.blog where id in(1,2,3) ids=通过万能map传入的集合名称
item=遍历的每个元素的值
open 前缀
separator 分隔
close 后缀
#{iid} 前缀和后缀里面的东西 等于遍历的元素
collection取值不用加#{} 方法二: <select id="queryBlogget1_3" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id_"
open=" (" separator="or" close=")">
id=#{id_}
</foreach>
</where>
</select> SQL:select * from mybatis.blog where(id=? or id=?)


//测试代码片段 HashMap map = new HashMap();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> blogs= mapper.queryBlogget1_3(map);
动态SQL常用标签的更多相关文章
- Mybatis学习笔记之---动态sql中标签的使用
动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...
- 动态SQL之标签
本节主要讲了动态SQL的几个标签:where set trim where: 检出where语句的最前面是否含有AND和一个空格 或者 or和一个空格 ,如果有的话删除 set: 检出set的最后是否 ...
- 动态SQL各个标签作用以及注意事项详解
创建com.mybatis包,包含:UserMapper.xml和mybatis-config.xml UserMapper.xml代码: <?xml version="1.0&quo ...
- MyBatis动态SQL foreach标签实现批量插入
需求:查出给定id的记录: <select id="getEmpsByConditionForeach" resultType="com.test.beans.Em ...
- Mybatis 最强大的动态sql <where>标签
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...
- Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!
封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...
- [刘阳Java]_MyBatis_动态SQL标签用法_第7讲
1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwi ...
- Mybatis:缓存,动态SQL,注解SQL以及动态标签使用
1 转义字符 字符 转义 描述 < < 小于 <= <= 小于等于 > > 大于 >= >= 大于等于 <> <> 不等于 &a ...
- [Mybatis]Mybatis 常用标签及功能整理
Mybatis中生成动态SQL的标签有四类,分别是: if choose (when, otherwise) trim (where, set) foreach 1.if 当需要动态生成where条件 ...
随机推荐
- vue Cannot read property ‘tapPromise‘ of undefined
https://blog.csdn.net/qq379682421/article/details/111574290 https://github.com/SimenB/add-asset-html ...
- Spring框架中有哪些不同类型的事件?
Spring 提供了以下5种标准的事件: (1)上下文更新事件(ContextRefreshedEvent):在调用ConfigurableApplicationContext 接口中的refre ...
- SpingMvc中的控制器的注解一般用那个,有没有别的注解可以替代?
答:一般用@Controller注解,也可以使用@RestController,@RestController注解相当于@ResponseBody + @Controller,表示是表现层,除此之外, ...
- 当一个线程进入一个对象的 synchronized 方法 A 之后, 其它线程是否可进入此对象的 synchronized 方法 B?
不能.其它线程只能访问该对象的非同步方法,同步方法则不能进入.因为非静 态方法上的 synchronized 修饰符要求执行方法时要获得对象的锁,如果已经进入 A 方法说明对象锁已经被取走,那么试图进 ...
- java-idea创建maven管理web项目不能解析EL的解决方法
默认会原样输出: 这是由于这样子创建的web.xml的版本不够高 2.5之前web.xml文件中的头定义中,el表达式默认是忽略不解析的,故需要显示声明解析el表达式 所以我们要修改版本: 再< ...
- 【Linux-vim】vim文件:查看某几行,把某几行复制到另一个文件中
一.查看文件的某几行1.使用cat命令(1)查看文件的前10行: cat filename |head -n 10(2)查看文件后10行: cat filename |tail -n 10(3)查看文 ...
- PID算法原理 一图看懂PID的三个参数
找了好久这一篇算是很容易看懂的了 推荐给大家 写的十分清楚 原文作者DF创客社区virtualwiz LZ以前有个小小的理想,就是让手边的MCU自己"思考"起来,写出真正 ...
- sublime text3 好用的插件
sublime text3 推荐插件 Package Controller安装 1.打开sublime text 3,按ctrl+~或者菜单View > Show Console打开命令窗口.2 ...
- API的自动化测试
传统的测试工具在测试一个API的时候,必须手动填写这个API所需要接收的所有信息,比如一个查询航班动态的API,他接收两个输入字段,一个叫flight, 一个叫date,那么测试这个API的用户,需要 ...
- Episode 3:我们想要更好的社交网络
我们为什么爱看评论?怎样的人类文字最有效率?更「好」的手机设计.APP 设计?APP Store 已经十年了?这是 WEB VIEW 的第三期节目<我们想要更好的社交网络>. 链接描述 s ...