解决mapper.xml 中Update动态SQL逗号的问题; 必须为元素类型 “mapper“ 声明属性 “xmlns“的问题
1、mapper.xml文件使用了修改语句2、对不同参数是否为空或者“”来修改update 语句3、使用到 <set></set> 或者》=
·
目录
第一个--动态SQL逗号的问题
1、mapper.xml文件使用了修改语句
2、对不同参数是否为空或者“”来修改update 语句
3、使用到 <set></set> 或者<trim></trim>
<!--修改文章-->
<update id="editArticle">
UPDATE article
<set>
<if test="title !=null and title!='' ">
title = #{title},
</if>
<if test="type != null ">
`type` = #{type},
</if>
<if test="author !=null and author!='' ">
author = #{author},
</if>
<if test="publish != null ">
publish = #{publish} ,
</if>
<if test="article_abstract !=null and article_abstract!='' ">
article_abstract = #{article_abstract},
</if>
<if test="content !=null and content!='' ">
content = #{content},
</if>
<if test="like_num !=null ">
like_num = #{like_num},
</if>
<if test="front_cover !=null and front_cover!='' ">
front_cover = #{front_cover},
</if>
<if test="top != null ">
top = #{top},
</if>
</set>
WHERE id =#{id}
</update>
4、对参数进行注释,建议放置到赋值那里;而不是判断那里。
错误的书写形式:

这样运行会导致sql报错:
### SQL: UPDATE article set /*名称*/ /*文章类型*/ /*作者*/ /*发布状态*/ publish = ? , /*摘要*/ /*正文*/ /*点赞数量*/ /*封面*/ /*置顶*/ WHERE id =?
其实sql语句逻辑上没错 但是注释展示出来导致 逗号没有被消除。这样就影响到了sql语句的规范
所以我将注释放在判断下。

这样就没问题了。但是这样的格式并不是最合理的,xml的注释方式应该是
<!--注释-->
所以再次修改。

这样即可正常使用
第二个-xmlns的问题
报错出现位置在:

出现报错信息:
nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 115; 必须为元素类型 "mapper" 声明属性 "xmlns"。
这里只需要把xmlns取消即可。 mapper文件在这里不需要使用xmlns这个属性。
关于xmlns的解释:https://www.w3school.com.cn/tags/tag_prop_xmlns.asp
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)