MyBatis动态插入数据(trim标签的使用)

MyBatis动态插⼊数据(trim标签的使⽤)
今天在MyBatis中看到了这样的代码
<insert id="insertTbbaseItemMaterial" parameterType="TbbaseItemMaterial" useGeneratedKeys="true" keyProperty="id">
insert into tbbase_item_material
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="materialCode != null  and materialCode != ''">material_code,</if>
<if test="materialName != null  and materialName != ''">material_name,</if>雨水收集利用系统
<if test="materialType != null  and materialType != ''">material_type,</if>
<if test="remark != null  and remark != ''">remark,</if>
<if test="createBy != null  and createBy != ''">create_by,</if>
电池钢壳
<if test="createTime != null ">create_time,</if>
<if test="updateBy != null  and updateBy != ''">update_by,</if>
<if test="updateTime != null ">update_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="materialCode != null  and materialCode != ''">#{materialCode},</if>
<if test="materialName != null  and materialName != ''">#{materialName},</if>
橡塑模具<if test="materialType != null  and materialType != ''">#{materialType},</if>
<if test="remark != null  and remark != ''">#{remark},</if>
<if test="delFlag != null  and delFlag != ''">#{delFlag},</if>
<if test="createBy != null  and createBy != ''">#{createBy},</if>地龙多肽
<if test="createTime != null ">#{createTime},</if>
智能家居管理系统
<if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
<if test="updateTime != null ">#{updateTime},</if>
</trim>
</insert>
不禁好奇trim标签的作⽤是什么,查阅资料之后,发现trim的作⽤就是可以在⾃⼰包含的内容前加上某些前缀,也可以在其后加上某些后缀小型地源热泵
prefix:前缀覆盖并增加其内容。也就是给中的sql语句加上前缀;
suffix:后缀覆盖并增加其内容。给包裹的sql语句加上后缀;
prefixOverrides:前缀判断的条件。取消指定的前缀,如where;
suffixOverrides:后缀判断的条件。取消指定的后缀,如and | or.,逗号等。
如上⾯的插⼊sql语句,⽆⾮就是拼接了"(“和”)“在字段的前后,但是只是这样会发现多出来了最后⼀个字段的”,"从⽽导致语句报错。所以要取消掉最后⼀个逗号⽤上了suffixOverrides。

本文发布于:2024-09-21 19:43:00,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/144341.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:发现   前缀   数据   加上   后缀   取消   内容   语句
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议