Rowmapper vs ResultSetExtractor in spring



我在使用Spring jdbc RowMapper时遇到了奇怪的问题:-

我的代码

public void test(){
String sql=" Query  will fetch records where dateColumn<='2021-08-17' Limit 1";
jdbcTemplate.query(sql, new ModelRowMapper());
}
public class ModelRowMapper implements RowMapper<ModelRowMapper> {
@Override
public ModelRowMapper mapRow(ResultSet rs, int rowNum) throws SQLException {
ModelRowMapper model= new ModelRowMapper();
System.out.println(rs.getString("value"));
}
}

: -

db records:-
2021-08-21
2021-08-15
2021-08-13

我期望的输出是2021-08-15

在ModelRowMapper类中观察到的resultSet打印两个值(第一个是有效的:- 20121-08-15)然后打印无效值,并且在响应中我也将获得无效值

但是上面的查询正常工作时,我使用ResultSetExtractor

jdbcTemplate.query(sql, new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet rs) throws SQLException, DataAccessException {
while (rs.next()) {
System.err.println(rs.getString("value"));
}
//prints only one value and returns the same value
return "";
}
});

rowMapper有什么问题?....任何建议都会有帮助.......

您误解了rowmapper应该如何命名!使用下面的语法,可以得到想要的结果。

public void test(){
String sql=" Query  will fetch records where dateColumn<='2021-08-17' Limit 1";
jdbcTemplate.query(query, new RowMapper<ModelRowMapper>(){
@Override
public ModelRowMapper mapRow(ResultSet rs, int rowNum) throws 
SQLException {
ModelRowMapper model= new ModelRowMapper();
System.out.println(rs.getString("value"));
}
});
}

相关内容

  • 没有找到相关文章

最新更新