Java Jdbctemplate 查询具有命名参数和行映射器的列表?

Java Jdbctemplate query for list with named parameter and row mapper?

我正在尝试实现一个 Jdbctemplate 查询方法,它将一个命名参数映射和一个行映射器作为附加参数。

到目前为止我有以下内容:

    final SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("searchTerm", "%" + text + "%");
List<Map<String, String>> result = (List<Map<String, String>>)jdbcTemplate.queryForList(query, namedParameters, (ResultSet rs) ->
{
    List<Map<String, String>> rows = new ArrayList<>();
    while (rs.next())
    {
        Map<String, String> row = new HashMap<>();
        for (int x = 0, j = queryColumns.length; x < j; x++) {
            row.put(queryColumns[x], rs.getString(queryColumns[x]));
        }
        rows.add(row);
    }
    return rows;
});

return result;

这给了我以下错误:

Error:(67, 83) java: no suitable method found for queryForList(java.lang.String,org.springframework.jdbc.core.namedparam.SqlParameterSource,(ResultSet[...]ws; }) method org.springframework.jdbc.core.JdbcTemplate.queryForList(java.lang.String,java.lang.Class) is not applicable (cannot infer type-variable(s) T (actual and formal argument lists differ in length)) method org.springframework.jdbc.core.JdbcTemplate.queryForList(java.lang.String,java.lang.Object[],int[],java.lang.Class) is not applicable....

是否可以使用 Jdbctemplate 执行此类查询,我该怎么做?

通过 NamedParameterJdbcTemplates 支持命名参数。 你可以使用类似的东西:

final Map<String, Object> namedParameters = Collections.singletonMap("searchTerm", "%" + text + "%");
List<Map<String, String>> result = new NamedParameterJdbcTemplate(jdbcTemplate).query(query, namedParameters, (rs, rowNum) ->
{
    Map<String, String> row = new HashMap<>();
    for (int x = 0, j = queryColumns.length; x < j; x++) {
        row.put(queryColumns[x], rs.getString(queryColumns[x]));
    }
    return row;
});

return result;