`

iBatis resultMap报错 nullValue完美解决

阅读更多

错误信息:

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>

分享到:
评论

相关推荐

    Ibatis查询语句里,可以使用多表查询

    &lt;result property="adminLevel" column="ADMIN_LEVEL" nullValue="0"/&gt; &lt;/resultMap&gt; ``` 这里新增了一个 `adminLevel` 属性,用以保存 `ADMIN_LEVEL` 字段的值。这样,查询结果就可以被正确地映射到 `MemberPost`...

    ibatis2.0中文API

    比如使用`nullValue`属性处理空值,或者通过优化SQL语句来提高查询速度。同时,对于复杂的业务场景,iBATIS支持动态SQL,使得在映射文件中可以编写条件语句,根据对象状态动态生成执行的SQL片段。 最后,iBATIS 2.0...

    spring+ibatis集成文档

    ### Spring与iBatis集成开发详解 #### 一、引言 在Java企业级应用开发领域,Spring...综上所述,Spring与iBatis的集成为企业级应用开发提供了一个强大而灵活的解决方案,对于提高软件开发效率和质量具有重要意义。

    ibatis中输入输出各种类型的参数分析及#与$区别

    `resultMap`是iBatis中一个重要的概念,主要用于定义如何将数据库查询结果映射到Java对象上。它可以提供比默认的列名到属性名匹配更加复杂的映射逻辑,如一对多、多对多关系的处理。 例如: ```xml &lt;resultMap id=...

    ibatis配置文件信息

    ### ibatis配置文件信息 #### 一、简介 在Java开发领域中,ibatis(现称为MyBatis)是一款优秀的持久层框架,它通过XML或注解的方式将接口方法与SQL语句绑定起来,实现对象关系映射(ORM)功能。ibatis的主要优势...

    ibatis16个常用sql语句

    iBatis 16个常用SQL语句 iBatis是一个基于Java的持久层框架,提供了一个简洁的方式来访问和操作数据库。在iBatis中,SQL语句是通过XML文件来配置的。下面是16个常用的iBatis SQL语句,涵盖了基本的CRUD(Create、...

    ibatis的动态查询

    ### ibatis的动态查询知识点详解 #### 一、模糊查询 **知识点1:** 在进行模糊查询时,ibatis支持两种不同的语法标记:`#` 和 `$`。 1. **使用 `$value$` 进行模糊查询:** - 在进行模糊查询时,使用 `$value$` ...

    ibatis的一个CRUD

    ### IBatis CRUD 实例详解 #### 一、IBatis简介 IBatis(现称为MyBatis)是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。IBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。IBatis...

    Ibatis常用sql语句

    根据给定的文件信息,以下是对“Ibatis常用...Ibatis通过其动态SQL标签如`iterate`, `isNotNull`, `dynamic`等提供了极高的灵活性,能够有效应对复杂多变的业务需求。掌握这些基本用法对于提升Ibatis开发效率至关重要。

    Spring+Ibatis集成开发实例.doc

    在Spring和iBatis的集成开发中,我们通常利用Spring的IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)特性,结合iBatis的简单灵活的SQL映射功能,实现数据访问层的高效...

    srping+ibatis整合的小案例

    Spring和iBatis是两个非常流行的Java开发框架,Spring是一个全面的后端开发框架,而iBatis则是一个优秀的持久层框架,它简化了数据库操作。将这两个框架整合在一起可以提供一个灵活、高效的数据库访问机制。下面我们...

    10_ibatis教程_ibatis-2.3.3.720.zip

    《深入理解iBatis:基于2.3.3.720版本的教程解析》 iBatis,作为一款优秀的Java持久层框架,为开发者提供了灵活的SQL映射功能,使得数据库操作与业务逻辑分离,提高了开发效率。本教程基于iBatis 2.3.3.720版本,...

    iBATIS-基础入门资料

    iBATIS,全称为“Java SQL Mapping Framework”,是一款开源的Java数据访问框架,它为Java开发者提供了一种方便地将SQL语句与Java代码分离,实现数据库操作的方式。本资料集是针对iBATIS的基础入门教程,适合初学者...

    ibatis 入门例子

    Ibatis 是一款优秀的、轻量级的Java持久层框架,它主要解决了Java应用程序与数据库交互的问题。本教程将通过一个基础的Ibatis入门示例,帮助你理解如何在实际项目中使用Ibatis进行数据操作。 首先,Ibatis 提供了...

    IBATIS入门(第二节)

    **IBATIS入门(第二节)** 在本节中,我们将深入探讨开源的Java持久层框架——IBATIS,它提供了一种将SQL语句与Java代码分离的方式,从而简化了数据库操作。IBATIS这个名字来源于“Java中的iBATIS”,其中“i”是...

    iBatis-设置缓存模式-Java源码(下载)

    if (value == null) { System.out.print("\t "); } else { System.out.print("\t"+value.toString().trim()); } } System.out.println(""); } } } File: Account.java public class...

    iBATIS3用户指南(EngLish)

    &lt;property name="defaultObjectFactory" value="org.apache.ibatis.reflection.DefaultObjectFactory"/&gt; ``` - **plugins**:扩展插件支持。 - 示例代码: ```xml ``` - **environments**:配置多...

    ibatis dynamic 用法

    5. **`&lt;isEmpty&gt;`**:检查 Collection.size() 的值,属性的 String 或 String.valueOf() 值 是否为 null 或空("" 或 size() )。 6. **`&lt;isNotEmpty&gt;`**:检查 Collection.size() 的值,属性的 String 或 String....

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    韩顺平 Java工程师课程-MyBatis.pdf

    MyBatis 的前身是 iBatis,于 2010 年更名为 MyBatis。MyBatis 提供了一个灵活的映射方案,能够将对数据表的操作(SQL、方法)等等直接剥离,写到 XML 配置文件,实现和 Java 代码的解耦。 1.1 MyBatis 的主要特点 ...

Global site tag (gtag.js) - Google Analytics