在Mybatis 请勿使用`--`来注释单行sql 语句,mybatis 进行编译执行时出于某种原因,可能会忽略sql 语句中的`--`注释语句,使得你原本注释掉的语句重新生效。
例如,你的mapper.xml 中有以下内容:
<update id="updateById">
update player_login_log
set id = #{id, jdbcType=INTEGER},
xbox_uuid = #{uuid, jdbcType=VARCHAR}
-- is_disabled = #{isDisabled, jdbcType=BIT},
-- last_login_location = #{lastLoginLocation, jdbcType=VARCHAR}
where xbox_uuid = #{uuid, jdbcType=VARCHAR}
</update>
此时上述使用 -- 注释掉的内容如果mybatis 去执行仍会当成正常的参数去执行。在我这里则是由于注释不起作用导致实际在实体类中不存在的字段仍使用mybatis 插值语法进行了赋值,从而导致错误:
Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='last_login_location', mode=IN, javaType=class java.lang.String, jdbcType=VARCHAR, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #28 with JdbcType VARCHAR . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 28 超出范围。
at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:89)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy253.update(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)