Mybatis新增用if标签出现bad SQL grammar问题解决

新增语句用标签判断时出现 Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds …bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException
而这个用法在之前的项目中又能实用,之前用的数据库是5.7然后换成了8.0一直找不到原因,假如不用if标签直接插入又没有问题,能插入成功,说明我的参数传递是没问题的,后来才发现可能是排空标签没有使用导致
mybatis的trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。
以下为示例

insert into tab_user
<trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="userId != null">user_id,</if>
    <if test="userName != null and userName != ''">user_name,</if>
    <if test="password != null and password != ''">password,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="userId != null">#{userId},</if>
    <if test="userName != null and userName != ''">#{userName},</if>
    <if test="password != null and password != ''">#{password},</if>
</trim>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值