MyBatis的参数,不能传入null

今天在调试的过程当中发现一个bug,把传入的参数写到查询分析器中执行没有问题,可是在程序中执行就报错:org.springframework.jdbc.UncategorizedSQLException : Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: Java.sql.SQLException:无效的列类型

 

网上找了好久,才找到了错误缘由:myBatis的参数,不能传入null(可是在查询分析器中参数为null是容许的),若是传入了null,SQL在类型匹配时找不到匹配的类型,就会报上面的错。

 

解决办法:

一、将null替换成“”字符串;

二、在参数后面添加jdbcType=VARCHAR,

例如:把 insert into user(id,name) values(#{id},#{name}) 
     修改为:insert into user(id,name) values(#{id,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR}) 
就能够解决这个问题了。
相关文章
相关标签/搜索