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在类型匹配时找不到匹配的类型,就会报上面的错。

 

解决办法:

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

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

例如:把 insert into user(id,name) values(#{id},#{name}) 
     修改成:insert into user(id,name) values(#{id,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR}) 
就可以解决这个问题了。

 

posted on 2017-01-24 13:27  peter.peng  阅读(2613)  评论(0编辑  收藏  举报