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}) 
就能夠解決這個問題了。
相關文章
相關標籤/搜索