今天在调试的过程中发现一个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、在参数后面添加jdbcType=VARCHAR

2、使用if标签

 and id = #{id}

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

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

精彩链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: