ibatis操作oracle數(shù)據(jù)庫時,空值(null)異常的不完全解決方法
ibatis操作oracle數(shù)據(jù)庫時,如果出現(xiàn)空值,ibatis不是插入NULL,而是出現(xiàn)異常,異常信息大致如下:Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in [someSqlMapFile.xml].
--- The error occurred while applying a parameter map.
--- Check the insertUser-InlineParameterMap.
--- Check the parameter mapping for the '[someProperty]' property.
--- Cause: java.sql.SQLException: 無效的列類型
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:91)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:447)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:59)
--- The error occurred in [someSqlMapFile.xml].
--- The error occurred while applying a parameter map.
--- Check the insertUser-InlineParameterMap.
--- Check the parameter mapping for the '[someProperty]' property.
--- Cause: java.sql.SQLException: 無效的列類型
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:91)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:447)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:59)
如果你先插入空值,必須告訴IBatis當(dāng)該字段出現(xiàn)NULL值該用什么值來替代,方法有2種,如下:
方法一(parameterClass):
1 INSERT INTO TEST(ID, NAME, PASSWD) VALUES (#id#,#name#, #passwd:VARCHAR:NULL#)
支持的類型在 java.sql.Types 中列示出來了
注意:DATA 類型默認(rèn)不能為NULL
方法二(parameterMap):
使用這個方法時,首先應(yīng)保證你的Oracle jdbc driver是10G以上版本,不然也是白搭。
1 <parameterMap id="insert-person-paraMap" class="com.unmi.Person" >
2 <parameter property="id"/>
3 <parameter property="name"/>
4 <parameter property="passwd" jdbcType="VARCHAR"/>
5 </parameterMap>
6
7 <!-- 插入一條Person對應(yīng)的記錄到數(shù)據(jù)庫中 -->
8 <insert id="insertPerson" parameterMap="insert-person-paraMap">
9 INSERT INTO PERSON (ID, NAME, PASSWD) VALUES (?,?,?)
10 </insert>
11
2 <parameter property="id"/>
3 <parameter property="name"/>
4 <parameter property="passwd" jdbcType="VARCHAR"/>
5 </parameterMap>
6
7 <!-- 插入一條Person對應(yīng)的記錄到數(shù)據(jù)庫中 -->
8 <insert id="insertPerson" parameterMap="insert-person-paraMap">
9 INSERT INTO PERSON (ID, NAME, PASSWD) VALUES (?,?,?)
10 </insert>
11
參考:http://www.aygfsteel.com/fatbear/archive/2007/06/07/122607.html
iBatis 應(yīng)用程序向 Oralce 數(shù)據(jù)表字段插入 NULL 值
作者:肥熊熊
posted on 2007-08-16 16:18 YY 閱讀(4284) 評論(1) 編輯 收藏 所屬分類: ibatis