有一些代码使用了 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 的部分结果?
最佳答案
您的选择查询应该是
<select id="getUserId" resultMap="userResultMap">
select id, name from Foo
</select>
您的查询中缺少“名称”。 这很简单。您的结果 map 有两个属性,但您只选择了一个属性。应该是完全一样的。希望它有效。
关于java - Mybatis 的 resultMap 上的查询是否可以只返回部分结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23794944/