开发者

Java resultset rowmapper

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开发者_如何学C 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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜