From 3e7b221cf6f4db96857fa1d19d666e8a741a6315 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Thu, 1 Sep 2022 12:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis/mybatis.md | 110 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/mybatis/mybatis.md b/mybatis/mybatis.md index 1144d5f..0a46f99 100644 --- a/mybatis/mybatis.md +++ b/mybatis/mybatis.md @@ -828,4 +828,112 @@ readOnly: 默认情况下,某一命名空间中的语句只会对当前命名空间中的cache进行刷新.但是,如果想在多个命名空间(Mapper)之间共享缓存,可以通过cache-ref来引用其他命名空间中的缓存. ```xml -``` \ No newline at end of file +``` + +## 动态sql +### if +可以通过<if>标签根据条件向where子句中添加查询条件 +```xml + +``` +### choose、when、otherwise +不同于if会检测所有的条件,choose、when、otherwise只会从多个条件中选择一个使用,类似于java中的switch。 +```xml + + +``` +### where、trim、set +#### where +对于where标签,只有在子元素返回任何内容不为空的前提下才会插入where子句,并且,如果子句开头返回的内容为”or“或者”and“,则where标签会删除子句开头的”and“或者”or“ +```xml + +``` +#### trim +如果where标签并不能满足场景需求,那么可以通过trim来自定义想要实现的场景需求。 +和where标签等价的trim标签为 +```xml + + ... + +``` +对于trim标签,其会匹配prefixOverrides属性中指定的内容,文本内容通过'|'符号分隔,并将开头prefixOverrides中的内容删除,并在首部加入prefix属性指定的内容 +#### set +set标签用于update语句中动态设置属性,并且删除额外的逗号 +```xml + + update Author + + username=#{username}, + password=#{password}, + email=#{email}, + bio=#{bio} + + where id=#{id} + +``` +与set标签等效的trim标签是 +```xml + + + ... + +``` +### foreach +foreach标签允许在动态sql中遍历集合,通常用于构建in条件。 +```xml + +``` +> 可以通过foreach执行便利操作,如果foreach指定的collection为array,那么index对应的时数组下标 +> 若collection为map类型,那么index对应的是entry中的key,而value对应的是value