I am using JDBC template to get my query results from database. I had written the below working code to get my query results converted into the custom object "test" (for which I have a written class for separately).
jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<test>(test.class) );
My question is can the above code be made generic/reusable as a method so as to allow any kind of object to be passed as a parameter to this method and return type as the same object as well? Something like the non working code shown as below?
public static calledClass ExecuteQuery(String sql,Class calledClass) {
return jdbcTemplate.queryForObject(sqlCommand,new BeanPropertyRowMapper<calledClass>(calledClass.class))
}
In JdbcTemplate
, queryForObject()
is declared as :
public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException
So, something like that should do the job :
public static <T> T executeQuery(String sql,Class<T> calledClass){
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<T>(calledClass));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.