【发布时间】:2014-07-10 18:57:02
【问题描述】:
有一些使用Ibatis 2.3的代码,我有一个类User和一个resultMap如下:
public class User {
private Integer id;
private String name;
public Integer getId() {
return this.id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
}
<resultMap id="userResultMap" class="user">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
然后我有一个只返回 id 的选择查询:
<select id="getUserId" resultMap="userResultMap">
select id from Foo
</select>
像这样,Ibatis 想要在 resultMap 上填写所有结果,并且由于它发送的查询没有返回“name”并且错误:
--- The error occurred in ibatis/user.xml.
--- The error occurred while applying a result map.
--- Check the user.userResultMap.
--- Check the result mapping for the 'name' property.
--- Cause: java.sql.SQLException: Column 'name' not found.
是否有可能以某种方式让查询只返回 resultMap 上的部分结果?
【问题讨论】:
-
不是mybatis这边的问题。检查您是否有名为“名称”的列
-
查询没有返回“名称”列,这正是问题所在。我无法更改查询,它归其他人所有,实际上是对 Oracle 中的过程的调用。 Foo 不是一张桌子。查询看起来更像:'select id from app.getid() ...'
-
那么你必须从resultmap中删除属性,因为mybatis找不到列'name'的表
-
这正是我的问题,如果有办法在不同的 SQL 上重用相同的 ResultMap,但告诉 Ibatis 什么时候 SQL 不会返回其中一个列。当 SQL 上不存在列时,类似于“设置”该属性的默认值/空值。
-
一种方法是使用继承。但在这种情况下,我看不到重用能力的真正优势。
标签: java mybatis ibatis ibatis.net