MyBatis的参数,不能传入null

时间:2017-01-24 14:33:30   收藏:0   阅读:193
今天在调试的过程中发现一个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}) 
就可以解决这个问题了。

 

原文:http://www.cnblogs.com/peter-peng/p/6346929.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!