Announcement Announcement Module
Collapse
No announcement yet.
Passing object to RowMapper Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing object to RowMapper

    From what I've seen so far, everytime one creates a RowMapper class to pass to a JdbcTemplate, the implemented mapRow method creates a new object with which to fill from the resultset. Is it possible though, to use an existing object, which may have some data already? Maybe pass it to the inner RowMapper class?

    Thanks
    Last edited by Some_One; Oct 26th, 2007, 09:22 AM.

  • #2
    You could potentially construct your RowMapper object with an instance of an object which has some initial state, and add to it in the mapRow method, but depending on your needs, you could also consider using a ResultSetExtractor instead of a RowMapper:

    http://www.springframework.org/docs/...Extractor.html

    A ResultSetExtractor receives only one callback, leaving it up to you to iterate through the ResultSet and construct an object as appropriate.

    Comment


    • #3
      How would I pass an object to my RowMapper object for it to be used inside mapRow(..)?

      Thanks

      Comment


      • #4
        One example might be something like:

        Code:
        private class MyRowMapper implements RowMapper {
          private Object myObject;
        
          public MyRowMapper(Object myObject) {
            this.myObject = myObject;
          }
        
          public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            //use myObject here
           ...
          }
        }
        
        ...
        
        jdbcTemplate.query(sql, new MyRowMapper(myObject));
        
        ...

        Comment

        Working...
        X