新增语句用标签判断时出现 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>