3

in Spring's NamedParameterJdbcTemplate. how can I a query to perform "Select * from Student" to return a List without providing any parameters using Spring version 3.1.x ?
according to http://docs.spring.io/spring/docs/3.1.2.RELEASE/javadoc-api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html

there is

List query(String sql, Map paramMap, RowMapper rowMapper)

but i dont want to provide any paramMap because i am doing a "select *".

how can this be done using version 3.1.x ?

Thanks

Zeus
  • 49
  • 1
  • 1
  • 3
  • Have you tried simply passing an empty parammap? If I recall correctly the code only uses it if it encounters a param which requires substitution. If your query has none, everything might work fine even with an empty map. – Gergely Bacso Aug 18 '15 at 22:11
  • @Zeus: Please consider accepting the answer or respond with a comment in case you think its not an answer. – Hille Aug 23 '15 at 20:48

3 Answers3

2

In addition to @Kirill Ch example, Spring has BeanPropertyRowMapper, which can maps a row’s column value to a property by matching their names. Make sure your property and column has the same name. eg orgId with column name ORGID or org_id

public List<Organization> getAllOrganization(){
     String sql = "SELECT * FROM organization";
     List<Organization> orgList = namedParamJdbcTemplate.query(sql, new 
     BeanPropertyRowMapper<>(Organization.class));

    return orgList;
}
Abereham
  • 141
  • 3
  • 9
0

You may call getJdbcOperations() on your NamedParameterTemplate; using the obtained object you may then call e.g. <T> List<T> query(String sql, RowMapper<T> rowMapper).

Look up the docs, though; as using the above mentioned query method does not used a prepared statement. You may use (as the Spring docs mention) <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) passing null as args.

Update: Just checked Spring's source code and you can just pass an empty paramMap as Gergely suggested.

Update 2: Any chance to update to a newer Spring version? Spring 3.2.14 e.g. has the method you are looking for.

Hille
  • 4,096
  • 2
  • 22
  • 32
0

Here is an example:

public List<Organization> getAllOrganization(){
    String sql = "SELECT * FROM organization";
    List<Organization> orgList = namedParamJdbcTemplate.query(sql, new OrganizationRowMapper());

    return orgList;

}

Where is OrganizationRowMapper:

enter image description here

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65