java - Mybatis 的 resultMap 上的查询是否可以只返回部分结果?

标签 java mybatis ibatis ibatis.net

有一些代码使用了 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/

相关文章:

java - 使用 Dijkstra 算法的最短路径

java - JUnit 测试应用程序引擎全文搜索

ibatis - 在不使用复杂对象的情况下将多个参数传递给SELECT

java - ibatis - 如何映射值列表

java - 我可以使用 iBatis 创建动态临时表吗?

java - 如何使用注解配置的 MyBatis 指定 IN param 类型

java - 如何声明 Java API 中的方法已弃用我的本地项目?

Java ee 接口(interface)条件注入(inject)

java - mybatis 更新函数的返回值是什么?

java - 使用 getGenerateKeys 获取更新记录主键失败 - MyBatis