开发者

Spring Jdbc mapping rows

开发者 https://www.devze.com 2023-03-21 20:13 出处:网络
How can i map rows of two tables that refers each other? For example the开发者_如何学Pythonre are Employee and Department tables. Employee has a reference to department model which is the department o

How can i map rows of two tables that refers each other? For example the开发者_如何学Pythonre are Employee and Department tables. Employee has a reference to department model which is the department of the employee and Department has a reference to employee model which is the manager of the department. So how can i do map rows using spring RowMapper.

Thanks,


How can i map rows of two tables that refers each other?

for example like this:

public class TwoTablesRowMapper implements RowMapper<Map<String, Object>> {

  /**
   * Map data from select over 2 tables e.g.:
   * 
   * select 
   *    A.foo as afoo, 
   *    B.bar as bbar
   * from PARENT A, 
   *      CHILD B
   * where A.ID = B.ID
   * 
   * 
   * @param rs
   * @param rowNum
   * @return
   * @throws SQLException 
   */
  public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
      Map<String, Object> resultMap = new HashMap<String, Object>();

      // instead of a map one could fill an object
      // e.g.: myObject.set.afoo(afoo)
      resultMap.put("afoo", rs.getString("afoo"));
      resultMap.put("bbar", rs.getString("bbar"));

      return resultMap;
  }
}

for the SQL part i recommend you create a new question with specific SQL details (tables, relations, etc.) and tagged sql, it should find more (sql-savvy) viewers this way

0

精彩评论

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