日常提交

This commit is contained in:
wu xiangkai
2022-09-01 12:53:05 +08:00
parent 8516c31fcc
commit 3e7b221cf6

View File

@@ -829,3 +829,111 @@ readOnly:
```xml
<cache-ref namespace="com.someone.application.data.SomeMapper"/>
```
## 动态sql
### if
可以通过&lt;if&gt;标签根据条件向where子句中添加查询条件
```xml
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ACTIVE
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
```
### choose、when、otherwise
不同于if会检测所有的条件choose、when、otherwise只会从多个条件中选择一个使用类似于java中的switch。
```xml
<!--
如果传入了title那么会只按照title查找
如果title没有传入且author传入那么会按照author查找
如果title和author都没有提供那么会根据featured=1查找
-->
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ACTIVE
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
```
### where、trim、set
#### where
对于where标签只有在子元素返回任何内容不为空的前提下才会插入where子句并且如果子句开头返回的内容为”or“或者”and“则where标签会删除子句开头的”and“或者”or“
```xml
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
```
#### trim
如果where标签并不能满足场景需求那么可以通过trim来自定义想要实现的场景需求。
和where标签等价的trim标签为
```xml
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
```
对于trim标签其会匹配prefixOverrides属性中指定的内容文本内容通过'|'符号分隔并将开头prefixOverrides中的内容删除,并在首部加入prefix属性指定的内容
#### set
set标签用于update语句中动态设置属性并且删除额外的逗号
```xml
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
```
与set标签等效的trim标签是
```xml
<!--
其会在首部加入set并且删除位于末尾的','符号
-->
<trim prefix="SET" suffixOverrides=",">
...
</trim>
```
### foreach
foreach标签允许在动态sql中遍历集合通常用于构建in条件。
```xml
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
<where>
<foreach item="item" index="index" collection="list"
open="ID in (" separator="," close=")" nullable="true">
#{item}
</foreach>
</where>
</select>
```
> 可以通过foreach执行便利操作如果foreach指定的collection为array那么index对应的时数组下标
> 若collection为map类型那么index对应的是entry中的key而value对应的是value