给定 iBatis 选择查询的 ResultMap,似乎所有列(映射到 ResultMap 中的属性)实际上都是 SQL 查询的一部分。
但是,如果想要重用 ResultMap,尤其是在“结果映射中包含结果映射”时,那就有点烦人了。
示例:
<resultMap id="myResultMap"
<result property="myPropName" column="myColumnName"/>
<result property="someCollection" resultMap="otherResultMap"/>
</resultMap>
<resultMap id="otherResultMap" groupBy="..."
<result property="otherPropName" column="otherColumnName"/>
</resultMap>
当然,定义这两个结果映射是因为有一个查询使用联接来加载包含 myPropName 的容器对象和包含内部对象集合的 someCollection 的情况。
但是,如果我想为另一个只需要加载容器对象(使用 myPropName)的选择查询重用相同的结果映射定义,但不需要加载内部对象(到 someCollection 中),那么将会出现错误消息:
列名称“otherColumnName”为
在此结果集中未找到
如果 SQL 查询中不存在相应属性(在本例中为 otherPropName),是否不可能允许使用 null 或空集合初始化 someCollection?
是否真的有必要为该场景创建另一个结果图?
使用 iBatis(还不是 myBatis)版本 2.3.4...
Given a ResultMap for an iBatis select query, it seems obligatory that all columns (that are mapped to properties in the ResultMap) are actually part of the SQL query.
But that's a bit annoying if one wants to reuse ResultMaps, particularly when having 'resultmaps within resultmaps'.
Example:
<resultMap id="myResultMap"
<result property="myPropName" column="myColumnName"/>
<result property="someCollection" resultMap="otherResultMap"/>
</resultMap>
<resultMap id="otherResultMap" groupBy="..."
<result property="otherPropName" column="otherColumnName"/>
</resultMap>
Of course, these two result maps are defined because there is a case with a query that uses a join to load the container objects holding myPropName and someCollection holding a collection of inner objects.
But if I want to reuse the same result map definition for another select query that only needs to load the container objects (with myPropName), but doesn't need to load the inner objects (into someCollection), then there will be an error message:
The column name 'otherColumnName' was
not found in this ResultSet
Is there no possibility that allows to initialize the someCollection with null or an empty collection if the respective properties (in this case otherPropName) are not present in the SQL query?
Is it really necessary to create another result map altogether for that scencario?
Using the iBatis (not myBatis yet) version 2.3.4...
发布评论
评论(1)
“问题”存在于所有
TypeHandler
实现中。这些对象必须从ResultSet
中提取列值,并将其映射到相应的 java 类型。例如,在StringTypeHandler
类中,有一个如下方法:如果
ResultSet
中不存在该列,则行rs.getString(columnName)
抛出SQLException
。避免此错误的唯一方法是编写自己的TypeHandler
,它返回null
而不是抛出异常。无论如何,我建议您使用两个结果图。
The "problem" is in all the
TypeHandler
implementations. These objects have to extract the column value fromResultSet
mapping it to the corresponding java type. For instance, inStringTypeHandler
class there's a method like this:If the column is not present in the
ResultSet
, the liners.getString(columnName)
throws aSQLException
. The only way to avoid this error is writing your ownTypeHandler
which returnsnull
instead of throwing the exception.Anyhow, I suggest you to use two resultmaps.