iBatis resultMap报错 nullValue完美解决
错误信息:SQLErrorCodesFactory - Database product name cached for DataSource [org.apache.commons.dbcp.BasicDataSource@19c5048]: name is MySQLSQLErrorCodesFactory - SQL error codes for MySQL foundSQLE
错误信息:
SQLErrorCodesFactory - Database product name cached for DataSource [org.apache.commons.dbcp.BasicDataSource@19c5048]: name is 'MySQL'
SQLErrorCodesFactory - SQL error codes for 'MySQL' found
SQLErrorCodeSQLExceptionTranslator - Unable to translate SQLException with Error code '0', will now try the fallback translator
DataSourceUtils - Returning JDBC Connection to DataSource
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in net/jbbs/dao/ibatis/map/user.xml.
--- The error occurred while applying a result map.
--- Check the user.userResult.
--- The error happened while setting a property on the result object.
--- Cause: net.sf.cglib.beans.BulkBeanException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/jbbs/dao/ibatis/map/user.xml.
--- The error occurred while applying a result map.
--- Check the user.userResult.
--- The error happened while setting a property on the result object.
--- Cause: net.sf.cglib.beans.BulkBeanException
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/jbbs/dao/ibatis/map/user.xml.
--- The error occurred while applying a result map.
--- Check the user.userResult.
--- The error happened while setting a property on the result object.
--- Cause: net.sf.cglib.beans.BulkBeanException
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:104)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:566)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:541)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:243)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:241)
at net.jbbs.dao.ibatis.UserDAOImpl.findByEmail(UserDAOImpl.java:54)
at net.jbbs.test.UserTest.findByEmail(UserTest.java:42)
at net.jbbs.test.UserTest.main(UserTest.java:34)
Caused by: net.sf.cglib.beans.BulkBeanException
at net.jbbs.domain.User$$BulkBeanByCGLIB$$88b6b34d.setPropertyValues(<generated>)
at com.ibatis.sqlmap.engine.accessplan.EnhancedPropertyAccessPlan.setProperties(EnhancedPropertyAccessPlan.java:33)
at com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:112)
at com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
at com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:190)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery(GeneralStatement.java:205)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
... 10 more
这个错误指出resultMap里有问题。说错误发生在设置某一个属性的时候,但是没有具体说明是一个怎么样的错误<NULL错误>。由于对于iBatis了解还不是很深,所以一时不知如何事好。只有在resultMap里面作文章,把里面的<result>注释掉一些,哈哈,程序居然正常跑起来了。看来我快要找到错误了,接着继续排除其它的<result>,终于让我找到出错的一行<result>了: <result column="ORDER_NUM" property="orderNum" jdbcType="long" /><!--
接下来得弄清楚是一个什么样的错误,怎么去修复它。查找数据库表,发现"orderNum"这一列是nullable,并且没有为它设置默认值。所以表里面orderNum这一列有许多都为NULL。我大概明白了,肯定是iBatis试图把从数据库读出来的NULL 值写入对象属性的时候出现异常。采取的补救措施就是:
<result column="ORDER_NUM" property="orderNum" jdbcType="long" nullValue="0"/>
对从数据库读出来的NULL值采用一个相应可转换为orderNum类型的值来替换
附:
<resultMap id="pageStaticItemMap" class="PageStaticItem">
<result column="ITEMID" property="itemId" jdbcType="long"/>
<result column="ITEMNAME" property="itemName" jdbcType="String"/>
<result column="SERVERID" property="serverId" jdbcType="long"/>
<result column="SOURCE" property="source" jdbcType="String"/>
<result column="TARGET_PATH" property="targetPath" jdbcType="String"/>
<result column="TARGET_FILE" property="targetFile" jdbcType="String"/>
<result column="LOCAL_PATH" property="localPath" jdbcType="String"/>
<result column="FTP_PATH" property="ftpPath" jdbcType="String"/>
<result column="ITEMTYPE" property="itemType" jdbcType="String"/>
<result column="EXT1" property="ext1" jdbcType="String"/>
<result column="EXT2" property="ext2" jdbcType="String"/>
<result column="EXT3" property="ext3" jdbcType="String"/>
<result column="EXT4" property="ext4" jdbcType="String"/>
<result column="ORDER_NUM" property="orderNum" jdbcType="long" /><!--这一行加一个nullValue="0"就OK了-->
<result column="ITEM_TYPE" property="item_Type" jdbcType="String"/>
<result column="SERVER_PROPERTIES" property="serverProperties" jdbcType="String"/>
<result column="GET_SQL" property="getSql" jdbcType="String"/>
</resultMap>
更多推荐
所有评论(0)