<update id="removePrincipal"> update l_payback_bill set `principalIdList`= trim(both ',' from replace(concat(',', principalIdList, ','), concat(',', #{customerId}, ','), ',')) <where> id = #{id} </where> </
时间: 2024-04-27 21:21:46 浏览: 17
这是一个 Mybatis 的 XML 映射文件中的一个 update 标签,其 id 属性为 "removePrincipal"。这个 update 语句的作用是将 l_payback_bill 表中指定 id 的记录的 principalIdList 字段中,与 customerId 相同的值删除。具体做法是:先将 principalIdList 字段前后加上逗号,再将其中与 customerId 相同的部分用逗号替换掉。最后再去掉 principalIdList 字段前后的逗号。其中,#{id} 和 #{customerId} 是 Mybatis 的参数占位符,会在运行时被实际的值替换掉。
相关问题
优化语句<update id="updateByPrimaryKey" parameterType="com.example.demo.domain.Permission"> update sys_permission <set> <if test="permission_id!=null"> permission_id = #{permission_id}, </if> <if test="pid!=null"> pid = #{pid}, </if> <if test="name!=null"> name = #{name}, </if> <if test="type!=null"> type = #{type}, </if> <if test="permission_value!=null"> permission_value = #{permission_value}, </if> <if test="uri!=null"> uri = #{uri}, </if> <if test="icon!=null"> icon = #{icon}, </if> <if test="status!=null"> status = #{status}, </if> <if test="ctime!=null"> ctime = #{ctime}, </if> <if test="orders!=null"> orders = #{orders}, </if> <if test="system_id!=null"> system_id = #{system_id} </if> </set> where permission_id = #{permission_id} </update>
这段MyBatis XML中的SQL语句看起来已经比较优化了,但还有一些可以改进的地方。下面是一些建议:
1. 使用`<trim>`标签:在`<set>`标签中,每个`<if>`标签都会生成一个逗号,最后一个`<if>`标签需要特殊处理。可以使用`<trim>`标签来解决这个问题,示例代码如下:
```xml
<set>
<trim suffixOverrides="," suffix=" ">
<if test="permission_id!=null">
permission_id = #{permission_id},
</if>
<if test="pid!=null">
pid = #{pid},
</if>
<!-- 其他属性 -->
<if test="system_id!=null">
system_id = #{system_id}
</if>
</trim>
</set>
```
上面的代码中,使用了`suffixOverrides`属性来删除每个`<if>`标签生成的逗号。同时,使用了`suffix`属性来在最后一个属性后面添加一个空格。
2. 使用`<foreach>`标签:如果要更新的字段比较多,可以使用`<foreach>`标签来生成`<if>`标签,减少重复代码。示例代码如下:
```xml
<set>
<foreach collection="updateFields" item="field">
<if test="field=='permission_id'">
permission_id = #{permission_id},
</if>
<if test="field=='pid'">
pid = #{pid},
</if>
<!-- 其他属性 -->
<if test="field=='system_id'">
system_id = #{system_id}
</if>
</foreach>
</set>
```
上面的代码中,`updateFields`是一个`List<String>`类型的变量,存储需要更新的字段名。使用`<foreach>`标签遍历`updateFields`,生成对应的`<if>`标签。
3. 使用动态SQL:如果更新的条件和更新的字段都不确定,可以使用动态SQL来生成SQL语句。示例代码如下:
```xml
<update id="updatePermission" parameterType="Map">
UPDATE sys_permission
<set>
<foreach collection="updateFields" item="field">
${field} = #{${field}},
</foreach>
</set>
<where>
1=1
<foreach collection="conditions" item="condition" separator="AND">
${condition.field} ${condition.operator} #{condition.value}
</foreach>
</where>
</update>
```
上面的代码中,`updateFields`和`conditions`都是存储在`Map`中的变量,分别表示需要更新的字段和更新的条件。使用`${}`语法来插入动态SQL。
update inspection_standard_check <trim prefix="set" suffixOverrides=","> <if test="lineCode != null and lineCode != ''"> line_code = #{lineCode}, </if> <if test="lineCode == null and lineCode ==
空字符串"> line_code = null, </if> <if test="checkName != null and checkName != ''"> check_name = #{checkName}, </if> <if test="checkName == null and checkName == ''"> check_name = null, </if> <if test="checkMethod != null and checkMethod != ''"> check_method = #{checkMethod}, </if> <if test="checkMethod == null and checkMethod == ''"> check_method = null, </if> <if test="description != null and description != ''"> description = #{description}, </if> <if test="description == null and description == ''"> description = null, </if> <if test="sortNo != null"> sort_no = #{sortNo}, </if> <if test="sortNo == null"> sort_no = null, </if> <if test="updateBy != null and updateBy != ''"> update_by = #{updateBy}, </if> <if test="updateBy == null and updateBy == ''"> update_by = null, </if> <if test="updateTime != null"> update_time = #{updateTime}, </if> <if test="updateTime == null"> update_time = null, </if> </trim> where id = #{id}
请问这段代码是用来做什么的?
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)