有一些使用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查询:
<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>
您在查询中缺少“名称”.
这很简单.结果映射有两个属性,但您只选择一个属性.它应该完全相同.希望它有效.