给定一个用于 iBatis 选择查询的 ResultMap,所有列(映射到 ResultMap 中的属性)实际上都是 SQL 查询的一部分似乎是强制性的。
但是,如果想要重用 ResultMaps,尤其是在“resultmaps 中包含 resultmaps”的情况下,那就有点烦人了。
例子:
<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)的选择查询重用相同的结果映射定义,那么就会有错误信息:
The column name 'otherColumnName' was not found in this ResultSet
如果 SQL 查询中不存在相应的属性(在本例中为 otherPropName),是否不可能允许使用 null 或空集合初始化 someCollection?
是否真的有必要为该场景完全创建另一个结果图?
使用 iBatis(还不是 myBatis)版本 2.3.4...
最佳答案
“问题”存在于所有 TypeHandler
实现中。这些对象必须从 ResultSet
中提取列值并将其映射到相应的 java 类型。例如,在 StringTypeHandler
类中有一个这样的方法:
public Object getResult(ResultSet rs, String columnName) throws SQLException {
Object s = rs.getString(columnName);
if (rs.wasNull()) {
return null;
} else {
return s;
}
}
如果该列不在 ResultSet
中,rs.getString(columnName)
行将抛出 SQLException
。避免此错误的唯一方法是编写您自己的 TypeHandler
,它返回 null
而不是抛出异常。
无论如何,我建议您使用两个结果图。
关于java - iBatis ResultMaps : The column name <. ..> 在此 ResultSet 中未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5586196/