在这种情况下哪些更合适? RowMapper或ResultSetExtractor?

时间:2015-01-10 09:34:36

标签: java spring jdbc

我有两个DAO方法

public EUser getEUserById(Integer id) {
    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(getJdbcTemplate());
    String sql = "SELECT * FROM users WHERE id = :id";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("id", id);
    EUser e = (EUser) template.queryForObject(sql, params, new EUserRowMapper());
    return e;
}

public List<EUser> getEUsers() {
    String sql = "SELECT * FROM users";
    List<EUser> es = getJdbcTemplate().query(sql, new EUserRowMapper());
    return es;
}

Row-mapper本身:

private class EUserRowMapper implements RowMapper<EUser> {
    public EUser mapRow(ResultSet rs, int arg1)
            throws SQLException {
        EUser e = new EUser();
        e.setId(rs.getInt("id"));
        e.setName(rs.getString("name"));
        //etc
        return e;
    }
}

但在我阅读有关这些方法的文件后,我对自己的选择表示怀疑。在ResultSetExtractorRowMapper使用哪种更合适?

0 个答案:

没有答案