【发布时间】:2025-11-23 08:00:02
【问题描述】:
情况:我正在设计一个基于 Spring MVC 的 Web 应用程序,我有一个名为 customers 的表,它由 3 列组成 id 、 property 、 property value 。
id 不是primary key。
以下是我正在使用的Model 类:
public class prop {
private String id;
private String property;
private String property_value;
/*setter and getters of these three variables ...*/
}
我的道是:
@Repository("Dao")
public class Dao implements{
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Model> listProp(String id) {
final String sql = "select * from customers where id = ? ";
final List<Model> list = jdbcTemplate.query(sql, new Object[]{id}, new Mapper());
return list;
}
}
我的Mapper 班级是:
public class Mapper implements RowMapper<Model> {
public Model mapRow(ResultSet rs, int rowNum) throws SQLException {
Model m = new Model();
m.setId(rs.getString(1));
m.setProperty(rs.getString(2));
m.setValue(rs.getString(3));
return wl;
}
}
问题:现在我有一个场景,其中 id=1 有 4 个属性,所以它将有 4 个对应的行并创建 4 个模型对象,
如果 id=1 有 100 个属性,则创建 100 个 model 对象,这是低效的,我想要这样,对于 id=1 的所有行,必须创建一个 Modelobject,我尝试使用 map但无法正确实施,有人可以帮忙吗?
注意:在 UI 中,我显示所有记录,因为它们存在于 DB 中
【问题讨论】:
-
我不太了解你的设计。从这个查询来看:
select * from customers where id = ?你怎么会有 4 个结果? -
正如我所说的 id 不是主键,所以上面的查询将检索所有具有特定 id 的记录
标签: java spring spring-mvc optimization dao