在项目中利用Mybatis做持久化框架,XXXMapper.xml中insert语句做插入时,偶然会遇到一些报错或者说是问题,如下:
报错日志:
nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='__frch_item_0.perSource', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #7 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
解决方案:
由于插入的字段为null,故报此错。在插入的字段为null值,且数据库字段存储允许为空时,mybatis的xml文件中需要指定字段的jdbc类型(当下字段在Java的实体类中被定义为String类型),即在#表达式中加入jdbcType=VARCHAR就OK了,如下
#{item.perSource,jdbcType=VARCHAR}