多个表的数据必须映射到单个bean。
在这种情况下,id,name
将从一个表optionList
中检索到其他表。因此,来自n个表的数据应与单个bean匹配。使用RowMapper or ResultSetExtractor or any alternative
的最佳方法是什么?两者之间的性能差异是什么?
BeanPropertyRowMapper和RowMapper 。
我的模型类就像:
class Preference{
int id;
int name;
List<Option> optionList; //will be retrieved from other table.
String contactName;
String ContactTitle;
}
答案 0 :(得分:0)
您可以使用Spring Row Mapper来完成这项工作。 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jdbc-JdbcTemplate-examples-query
非常喜欢
public class MyRowMapper implements RowMapper<MyCustomEntity> {
@Override
public MyCustomEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
MyCustomEntity custom = new MyCustomEntity();
custom.setId(rs.getLong("id"));
custom.setName(rs.getString("name"));
custom.setMoreData(rs.getString("more_data"));
return custom;
}
}
在你的DAO家伙中,你可以做类似
的事情@Repository
public class DaoGuy {
@Inject
private JdbcTemplate jdbcTemplate;
public List<MyCustomEntity> getMyCustomEntityData(String whateverYouWantToUseToFilter) {
String sqlQuery = "select a.id, b.name, c.more_data from my tablea a, tableb b, tablec c";
return jdbcTemplate.query(sqlQuery, new MyRowMapper());
}
}