2011-12-10 46 views
9

我用的MyBatis-彈簧1.0.3-SNAPSHOT的MyBatis-3.0.6 spring3.0.6.I試圖從這樣一個表中刪除記錄:MyBatis的錯誤設置爲null參數

 <delete id="deleteNote" parameterType="hashMap"> 
    DELETE FROM BBSCS_NOTE 
    <where> 
     <if test="ids !=null and ids.length > 0"> 
      <foreach collection="ids" item="id" open="(" close=")" separator=","> 
       ID IN #{id} 
      </foreach> 
     </if> 
     <if test="toID != null and toID != ''">AND TOID = #{toID}</if> 
     <if test="fromID != null and fromID != ''">AND FROMID = #{fromID}</if> 
     <if test="noteType != -1">AND NOTETYPE = #{noteType}</if> 
    </where>   
</delete> 

當你有可見,這是一個動態的sql.The Java測試代碼:

Map map = new HashMap(); 
String ids[] = {"1","2","3"}; 
map.put("ids", ids); 
noteService.del(map); 

當我執行的Java測試代碼,有一些例外,像這樣:

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: Invalid column type 
; uncategorized SQLException for SQL []; SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type 

瓦你能給我一些建議來解決這個問題嗎?謝謝。

回答

15

好的,我看到一些問題。首先,將空參數設置爲Prepared Statement或Callable Statement時,MyBatis需要知道jdbc類型。 這樣,

#{myNullParamenter, jdbcType=VARCHAR} 

你還生成你的「條款不正確。您需要使用foreach標籤才能生成值的列表。將「ID IN」部分移出foreach標籤。

<if test="ids !=null and ids.length > 0"> 
    ID IN 
    <foreach collection="ids" item="id" open="(" close=")" separator=","> 
     #{id} 
    </foreach> 
</if> 

我也建議不要使用HashMaps。新的Mapper類更好。

7

的問題是,既然3.0.x的版本默認的JDBC類型空參數是Types.OTHER它不被一些JDBC驅動程序一樣的Oracle 10g支持。

Here解釋此問題的帖子。

我發現的解決方案非常簡單,我在配置文件中將jdbcTypeForNull設置爲NULL

<configuration> 
    <properties resource="mybatis-config.properties" /> 
    <settings> 
     <setting name="jdbcTypeForNull" value="NULL" /> 
    </settings> 

    <environments default="development"> 
    .... 
    </environments> 

    <mappers> 
    .... 
    </mappers> 
</configuration> 
相關問題