动态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条件 ...
随机推荐
- JDK8新特性关于Stream流
在Java1.8之前还没有stream流式算法的时候,我们要是在一个放有多个User对象的list集合中,将每个User对象的主键ID取出,组合成一个新的集合,首先想到的肯定是遍历,如下: 1 2 3 ...
- Oracle问题解决记录
一.前言 oracle这么一个庞大的东西,出点问题真是太常见了.开个博客,用于记录遇到的问题吧. 持续更新. 二.问题列表 归档日志满,引起的问题. 一台服务器,用了很久了,某天,出现了磁盘空间占满的 ...
- java-规约-集合
/** * 1 * @hashCode&equals的处理: * 1-只要覆写了equals,必须复写hashCode. * 2-因为Set存储的是不重复的对象,依据hashCode和equa ...
- DRF 简单使用(详细注释版)
1.djangorestframework使用 下载安装 pip install djangorestframework ## djangorestframework pip install djan ...
- CommonsCollection2反序列链学习
CommonsCollection2 1.前置知识 CmonnosCollection2需要用到Javassist和PriorityQueue 1.1.Javassist Javassist是一个开源 ...
- Blog Ideas
Blog Ideas How-to Post Case Studies Product + Service Updates Product Reviews Content Survey Current ...
- css流程图、步骤图,流程线与环节分别实现,支持单环节、多环节情况。scss生成CSS
适用于分步骤操作的页面导航图 实现结果如下 上图对应下述代码,稍作修改可以生成下图.css代码如下: @charset "UTF-8"; /**单列宽度 单行高度 列数 行数*/ ...
- 【Android开发】jarsigner重新打包apk
签名(sign):在应用程序的特定字段写入特定的标记信息,表示该软件已经通过了签署者的审核. 过程:使用私有密钥数字地签署一个给定的应用程序. 作用: 识别应用程序作者: 检測应用程序是否发生改变: ...
- Android开发小经验
1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSiz ...
- uniapp中生成二维码(附代码和插件)
wxqrcode.js文件: https://github.com/Clearlovesky/-js-jq-/tree/master/wxqrcode // 引入二维码库 import QR fro ...