将 JdbcTemplate 与 Lamda RowMapper 一起使用的更好方法
Better way to use JdbcTemplate with Lamda RowMapper
我经常使用 jdbcTemplate.query 不是 return 结果集,而是访问数据库并对每个选定的记录进行一些计算。
例如下面的代码有效
Map<String, Double> mAcc = new HashMap<>();
sql = "select ar.id_codice_art, sum(ar.quantita) quantita\n" +
"from accettazione a \n" +
"inner join accettazione_riga ar on a.id_accettazione = ar.id_accettazione\n" +
"where a.id_ordine = ? \n" +
"group by ar.id_codice_art";
jdbcTemplate.query(sql, (rs, i)->{
mAcc.put(rs.getString("id_codice_art"), rs.getDouble("quantita"));
return "";
}, idOrdine);
但它并不干净,尤其是当 return "" 时,因为 RowMapper 接口需要它。同时,我不想创建记录或 class 只是为了 return 列表并在列表上工作。
是否有更好的方法来使用 jdbcTemplate 进行同样的思考?
You can try using the following code. Just create a new map, insert values in it and return it. and then you can copy values from that result map to your map.
Map<String, Double> mAcc = new HashMap<>();
sql = "select ar.id_codice_art, sum(ar.quantita) quantita\n" +
"from accettazione a \n" +
"inner join accettazione_riga ar on a.id_accettazione = ar.id_accettazione\n" +
"where a.id_ordine = ? \n" +
"group by ar.id_codice_art";
Map<String,Double> = jdbcTemplate.query(sql, (rs)->{
Map<String,Double> map = new HashMap<>();
while(rs.next()){
map.put(rs.getString("id_codice_art"), rs.getDouble("quantita"));
}
return map;
}, idOrdine);
我假设 idOrdine 键是 Long。
Map<String, Double> mAcc = new HashMap<>();
PreparedStatementSetter pss = ps -> ps.setLong(1, idOrdine);
RowCallbackHandler rch = rs -> mAcc.put(rs.getString("id_codice_art"),
rs.getDouble("quantita"));
jdbcTemplate.query(sql, pss, rch);
当然,您可以 in-line 那些 lambda 以避免导入这两个 类。
我刚刚在研究您的答案时了解到这一点,现在我将重写我刚刚为工作编写的一大块代码,以也使用该方法!
我经常使用 jdbcTemplate.query 不是 return 结果集,而是访问数据库并对每个选定的记录进行一些计算。 例如下面的代码有效
Map<String, Double> mAcc = new HashMap<>();
sql = "select ar.id_codice_art, sum(ar.quantita) quantita\n" +
"from accettazione a \n" +
"inner join accettazione_riga ar on a.id_accettazione = ar.id_accettazione\n" +
"where a.id_ordine = ? \n" +
"group by ar.id_codice_art";
jdbcTemplate.query(sql, (rs, i)->{
mAcc.put(rs.getString("id_codice_art"), rs.getDouble("quantita"));
return "";
}, idOrdine);
但它并不干净,尤其是当 return "" 时,因为 RowMapper 接口需要它。同时,我不想创建记录或 class 只是为了 return 列表并在列表上工作。
是否有更好的方法来使用 jdbcTemplate 进行同样的思考?
You can try using the following code. Just create a new map, insert values in it and return it. and then you can copy values from that result map to your map.
Map<String, Double> mAcc = new HashMap<>();
sql = "select ar.id_codice_art, sum(ar.quantita) quantita\n" +
"from accettazione a \n" +
"inner join accettazione_riga ar on a.id_accettazione = ar.id_accettazione\n" +
"where a.id_ordine = ? \n" +
"group by ar.id_codice_art";
Map<String,Double> = jdbcTemplate.query(sql, (rs)->{
Map<String,Double> map = new HashMap<>();
while(rs.next()){
map.put(rs.getString("id_codice_art"), rs.getDouble("quantita"));
}
return map;
}, idOrdine);
我假设 idOrdine 键是 Long。
Map<String, Double> mAcc = new HashMap<>();
PreparedStatementSetter pss = ps -> ps.setLong(1, idOrdine);
RowCallbackHandler rch = rs -> mAcc.put(rs.getString("id_codice_art"),
rs.getDouble("quantita"));
jdbcTemplate.query(sql, pss, rch);
当然,您可以 in-line 那些 lambda 以避免导入这两个 类。
我刚刚在研究您的答案时了解到这一点,现在我将重写我刚刚为工作编写的一大块代码,以也使用该方法!