我来项目,spring-jdbc
并使用了DAO层。有时现有实体并未涵盖数据库中所有可能的数据。所以我开始使用DTO。
通过应用 '70 结构编程规则,我将所有 DTO 放入单独的包中:
package com.evil.dao; // DAO interfaces for IOC.
package com.evil.dao.impl; // DAO implementation classes.
package com.evil.dao.dto; // DTOs
现在我重新考虑并决定将所有 DTO 作为内部类放在 DAO 接口上,用于没有重用的结果集。所以DAO界面看起来像:
interface StatisticDao {
class StatisticDto {
int count;
double amount;
String type;
public static void extract(ResultSet rs, StatisticDto dto) { ... }
}
List<StatisticDto> getStatistic(Criteria criteria);
}
class StatisticDaoImpl implements StatisticDao {
List<StatisticDto> getStatistic(Criteria criteria) {
...
RowCallbackHandler callback = new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
StatisticDao.StatisticDto.extract(rs, dto);
// make action on dto
}
}
namedTemplate.query(query, queryParams, callback);
}
}
我认为将相关数据保存在一起(带有 DAO 接口的自定义 DTO)使PageUp
/的代码更好PageDown
。