【问题标题】:How to extract a hashmap object from jdbc template query如何从 jdbc 模板查询中提取 hashmap 对象
【发布时间】:2018-12-15 01:22:13
【问题描述】:

我正在尝试从 JDBCTemplate 查询中提取 2 个整数列表/数组。 我认为检索地图将是最实用的。 查询是

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;
}

} ` 有人可以帮我将查询结果提取到地图或多个列表吗?

【问题讨论】:

    标签: java spring spring-boot jdbc jdbctemplate


    【解决方案1】:

    .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);
        
            }
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2018-04-05
      • 2013-09-15
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多