Results 1 to 4 of 4

Thread: Passing object to RowMapper

  1. #1

    Default 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 at 09:22 AM.

  2. #2
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    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.
    Mike Bingham

  3. #3

    Default

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

    Thanks

  4. #4
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    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));
    
    ...
    Mike Bingham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •