Java结果集行映射器
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;
}
}
这是我目前的课程,它是我的 1 个表的映射器,
我如何对其他数据库表使用相同的映射器类? 或者对于每个表,我必须创建新的映射器类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的 RowMapper 不包含任何状态,因此此类的相同实例可用于任何表/选择。唯一的问题是这些表/选择是否可以转换为 SubscriptionRequest 对象并包含第一个 int 和第二个 string 列。
如果没有,则必须为要生成的每个对象创建一个新的 RowMapper。或者使用一些“通用”行映射器,它将从每一行生成一个映射,而不是一个具体对象。
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.