开发者

Modelling Database Entities using Spring Rowmapper Object

I'm in the process of creating a front end for a Database Driven application and Could do with some advice. I have the following basic entities in my database:

  • aspect
  • aspect_value

As you can image I can have many aspect value to each aspect so that when a user开发者_运维问答 records an aspect they can select more than one value per aspect... simple.

I've created an POJO entity to model each aspect an my question is this... using Spring and the jdbcTemplate how would I be able to create the desired composite relationship using org.springframework.jdbc.core.RowMapper i.e. each aspect object containing one or more aspect value ojects? And for that matter I would appreciate if you could please let me know if this would be the best way to do it. I'm keen at some point to delve deeper into ORM but I've been put off so far by the number of issues I've encountered which has slowed down my development and led to the decision to use jdbcTemplate instead.

Thanks


You can use a RowMapper if you are storing aspect_values in your aspect object as objects. Each call to RowMapper returns an object so you'll end up with a collection of aspect_values. If you need to build an aspect object (or objects) with values contained in the aspect_value table then a ResultSetExtractor is the better choice.

Here are my examples as promised. I have to type these in by hand because our development network is on an internal network only so any typos are copy errors and not errors in the code. These are abbreviated versions of inner classes in my DAO:

This maps a single row in the ResultSet to an object:

public List<MessageSummary> getMessages(Object[] params)
{
  // mList is filled with objects created in MessageRowMapper,
  // so the length of the list equal to the number of rows in the ResultSet
  List<MessageSummary> mList = jdbcTemplate.query(sqlStr, new MessageRowMapper(),
                                                  params);
  return mList;
}

private final class MessageRowMapper implements RowMapper<MessageSummary>
{
  @Override
  public MessageSummary mapRow(ResultSet rs, int i) throws SQLException
  {
    MessageSummary ms = new MessageSummary();

    ms.setId(rs.getInt("id"));
    ms.setMessage(rs.getString("message"));

    return ms;
  }
}

ResultSetExtractor works on the same idea except you map the entire set yourself instead of just converting a row into an object. This is useful when your object has attributes from multiple rows.

public Map<Integer, List<String>> getResults(Object[] params)
{
  Map<Integer, List<String>> result = jdbcTemplate.query(sqlStr, new ResultExtractor(),
                                                         params);
  return result;
}

private final class ResultExtractor implements ResultSetExtractor<Map<Integer, List<String>>>
{
  @Override
  public Map<Integer, List<String>> extractData(ResultSet rs)
                                    throws SQLException, DataAccessException
  {
    Map<Integer, List<String>> resultMap = new HashMap<Integer, List<String>>();

    while (rs.next())
    {
      int id = rs.getInt("id");
      List<String> nameList = resultMap.get(id);
      if (nameList == null)
      {
        nameList = new ArrayList<String>();
        resultMap.put(id, nameList);
      }
      nameList.add(rs.getString("name"));
    }
    return resultMap;
  }

}


The RowMapper interface provides a method

Object mapRow(ResultSet rs,
          int rowNum)
          throws SQLException

You implement this method in a class and provide code to populate your entity object with values held in the row of the ResultSet rs. To obtain the resultset itself from database , you can use JdbcTemplate.select method's overload

List jdbcTemplate.query(String sql, RowMapper mapper )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜