Question

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.krams.tutorial.oxm.SubscriptionRequest;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class MyMapper implements RowMapper<SubscriptionRequest> {
    public SubscriptionRequest mapRow(ResultSet rs, int rowNum) throws SQLException {
        SubscriptionRequest subscription = new SubscriptionRequest();
        subscription.setId(rs.getInt(1));
        subscription.setCity(rs.getString(2));   
        return subscription;
    }
}

this is my class at the moment, it is a mapper for my 1 table

how can i use the same mapper class for other database tables? or for each table, i must create new mapper class?

Was it helpful?

Solution

As your RowMapper contains no state, the same instance of this class could be used for any table/select. The only question is if these tables/selects can be converted into SubscriptionRequest object and contain first int and second string column.

If not, than you must create a new RowMapper for each object you want to generate. Or use some "generic" row mapper that will produce a map from each row instead of a concrete object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top