mybatis的报错总结_____2Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an err...

本文通过一个实际案例介绍了MyBatis中trim元素的正确使用方法,包括prefix、prefixOverrides及suffix属性的作用,并对比了错误配置导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编写动态sql时 trim的属性写错了,如下图:

错误示范:

(接口的xml配置文件中:)

1  <update id="modifyBill" parameterType="Bill">
2         update smbms_bill
3         <trim suffix="set" prefixOverrides="," prefix="where id=#{id}">
4         <if test="billCode!=null">billCode=#{billCode},</if>
5             <if test="productName!=null">productName=#{productName},</if>
6             <if test="productDesc!=null">productDesc=#{productDesc}</if>
7         </trim>
8     </update>

报错日志:

Error updating database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=3 billCode='33333',


//可以看到日志中打印出来的sql语句也是有错的,如下:
### SQL: update smbms_bill          where id=? billCode=?,             productName=?,             productDesc=? set

 

正确的示范:

接口的xml配置文件中:

 <!--最后一个没有逗号~~~~~-->
    <update id="modifyBill" parameterType="Bill">
        update smbms_bill
        <trim prefix="set" prefixOverrides="," suffix="where id=#{id}">
        <if test="billCode!=null">billCode=#{billCode},</if>
            <if test="productName!=null">productName=#{productName},</if>
            <if test="productDesc!=null">productDesc=#{productDesc}</if>
        </trim>
    </update>

打印出来的sql语句:

[com.xbf.dao.bill.BillMapper.modifyBill]-==>  Preparing: update smbms_bill set billCode=?, productName=?, productDesc=? where id=? 

总结:(网上找的图,感觉说的很清晰!)

prefix:字首 eg:set

prefixOverrides:字首的替换 eg:  ,

suffix:后缀 eg:where id=#{ id }

转载于:https://www.cnblogs.com/xbfchder/p/11247715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值