Java 查询能否仅返回Ibatis上resultMap的部分结果?
有一些代码使用Ibatis 2.3,我有一个类用户和一个结果映射,如下所示:Java 查询能否仅返回Ibatis上resultMap的部分结果?,java,mybatis,ibatis,ibatis.net,Java,Mybatis,Ibatis,Ibatis.net,有一些代码使用Ibatis 2.3,我有一个类用户和一个结果映射,如下所示: 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() {
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>
是否可能以某种方式使查询只返回resultMap上的部分结果?您的select查询应该是
<select id="getUserId" resultMap="userResultMap">
select id, name from Foo
</select>
从Foo中选择id、name
查询中缺少“名称”。
很简单。结果贴图有两个属性,但仅选择一个属性。应该完全一样。希望它能工作。如果是空值或空值,您可以在sql查询本身中为该sql列设置一些默认值,以便它总是返回带有值的coulmn 这不是迈巴蒂斯那边的问题。检查是否有名为“name”的列查询不返回列“name”,这正是问题所在。我无法更改查询,它是由其他人拥有的,实际上是对Oracle中某个过程的调用。福不是一张桌子。查询看起来更像:“从app.getid()中选择id…”然后您必须从resultmap中删除属性,因为mybatis无法找到列为“name”的表。这正是我的问题,如果有办法在不同的SQL上重用相同的resultmap,但告诉Ibatis SQL何时不会返回其中一列。类似于“setup”,当SQL上不存在列时,该属性具有默认值/null。一种方法是使用继承。但在这种情况下,我看不到重用能力的真正优势。
--- 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.
<select id="getUserId" resultMap="userResultMap">
select id, name from Foo
</select>