1

If my SQL is:

select id, name, age from my_table where id in (...)

(we want to pass in the in values as arguments).

We have a our ID values

 List<Integer> = new ArrayList<Integer>(Arrays.asList(111,222,333));

A person object

public class Person
{
    private int id;
    private int age;
    private String name;
    //getters setters, constructors
}

Now I want to use the jdbcTemplate to execute the SQL such that I get a List<Person> returned.

I've already created a method that will convert the sql string into

select id, name, age from my_table where id in (?,?,?)

if that helps.

Looking at the JdbcTemplate javadoc it looks like I'd want to use one of the queryForList methods.

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • Possible answer here: http://stackoverflow.com/questions/1327074/how-to-execute-in-sql-queries-with-springs-jdbctemplate-effectivly?rq=1 – dwjohnston Apr 29 '15 at 22:45

3 Answers3

1

Yes you have to use one of queryForList method.

Probably you will need this method:

queryForList(java.lang.String sql, java.lang.Class<T> elementType, java.lang.Object... args)

Dilip Kumar
  • 2,504
  • 2
  • 17
  • 33
0

Following method gives you the list of objects:

jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(T.class));
mahesh reddy
  • 367
  • 3
  • 10
0

You need to create a RowMapper of Person class and use below code to get the list of objects.

    String SQL = "select * from my_table";
    List<Person> persons = jdbcTemplateObject.query(SQL,
            new PersonMapper());
    return persons;
Weidian Huang
  • 2,787
  • 2
  • 20
  • 29