simpleJdbcTemplate multiple row fetch
-
26-10-2019 - |
Question
I'm using SimpleJdbcTemplate to fetch a list of records from a database. Each record corresponds to a data model object. I have searched in the internet a lot but I still can't figure out how to fetch multiple rows from a database using generic row mapper.
Say I need to fetch a list of objects of type SampleObj.
How will I define the parameterized row mapper? How will I get the list of the SampleObj type objects?
Whenever I try creating a row mapper, the return type from the row mapper is always detected as a single object rather than a list. Please help.
Solution
The RowMapper maps one row. When you execute a query, every row returned is mapped, and you get a List back. Just try it, and it'll work the way you want. There are examples of doing so throughout the JDBC chapter of the Spring reference guide.