4

有一些使用 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 上的部分结果的查询?

4

2 回答 2

1

您的选择查询应该是

<select id="getUserId" resultMap="userResultMap">
     select id, name from Foo
</select>

您在查询中缺少“名称”。这很简单。您的结果图有两个属性,但您只选择了一个属性。应该完全一样。希望它有效。

于 2014-10-22T20:09:24.470 回答
0

在空白或空值的情况下,您可以在 sql 查询本身中为该 sql 列设置一些默认值,以便它始终返回该库的值。

于 2017-01-20T11:54:17.407 回答