作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 JDBCTemplate 查询中提取 2 个整数列表/数组。我认为检索 map 是最实用的。查询是
Map<Integer, Integer> availabletime = jdbctemp.query("
Select a.hour,
s.duration from appointment as a inner join services as s on a.service_fid=s.id
where date=? and guru_fid=?
",date,guru_fid,//mapperlogic.class);
我需要 a.hour 和 s.duration 作为 HashMap 的键值对。我对这里的行映射器逻辑有点困惑。到目前为止,我仅映射到对象,例如
public class RoleRowMapper implements RowMapper<Role> {
@Override
public Role mapRow(ResultSet row, int rowNum) throws SQLException {
Role role=new Role();
role.setId(row.getLong("id"));
role.setName(row.getString("name"));
return role;
}
}` 有人可以帮助我将查询结果提取到 map 或多个列表吗?
最佳答案
.query() 将始终返回列表。因此,添加了.get(0)
public Map<Integer,Integer> getAvailableTime(Date date, Integer guru_fid) {
return jdbctemp.query("Select a.hour, s.duration from appointment as a inner join services as s on a.service_fid=s.id where date=? and guru_fid=? ",new Object[] { date, guru_fid }, (ResultSet rs) -> {
HashMap<Integer,Integer> results = new HashMap<>();
while (rs.next()) {
results.put(rs.getInt("a.hour"), rs.getInt("s.duration"));
}
return results;
}).get(0);
}
关于java - 如何从jdbc模板查询中提取hashmap对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51210299/
我是一名优秀的程序员,十分优秀!