有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java iBatis加载对象列表

我是iBatis的新手(从今天开始),我正在尝试查询数据库中的对象列表(返回单个对象和值很好)

我遵循THIS教程(稍加修改-我的类有不同的名称)。你能告诉我在这种情况下我做错了什么吗

这是我获取用户列表的映射:

    <!-- Retrieve all users -->
    <select id="getAll" resultClass="wa.myslima.refman.entities.User">
      SELECT * FROM USER
   </select>

打电话的时候

rd = Resources.getResourceAsReader("SqlMapConfig.xml");

SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

users = (List<User>) smc.queryForList("User.getAll", null);

我得到以下例外情况:

--- The error occurred in User.xml.  
--- The error occurred while applying a result map.  
--- Check the User.getAll-AutoResultMap.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: wa.myslima.refman.entities.User

在其他一些教程中,它们使用了一些额外的映射,因此我也尝试了这种方法,但没有成功:

<resultMap id="UserResult" class="wa.myslima.entities.User">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
  </resultMap>

  <select id="getAll" resultMap="UserResult">
    select * from USER
  </select>

谢谢你的提示。这可能是一些愚蠢的错误,比如打字错误,但我还是看不透

编辑:用户。爪哇

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1456239434165308496L;

    private int id;
    private String name;
    private String password;
    private String email;

    public User(String name, String password, String email) {
        this.name = name;
        this.password = password;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在用户类中需要一个默认构造函数。因此,将其定义为:

    public User() {}
    

    Ibatis将使用此构造函数创建新用户