Usage
Example code is available on github.com/vaadin-on-grails/jdbc-template.
This article shows a few ways how to use JDBC template by Spring.
JdbcTemplate with parameters
The default way how to use JdbcTemplate with parameters.
JdbcTemplate jdbcTemplate = Grails.get(JdbcTemplate)
Object[] args = new Object[1]
args[0] = 1
int[] argsTypes = new int[1]
argsTypes[0] = Types.INTEGER
Map userFromDb = jdbcTemplate.queryForMap("SELECT * FROM User WHERE id=?", args, argsTypes)
String firstName = userFromDb.get('first_name')
layout.addComponent(new Label(firstName))NamedParameterJdbcTemplate with parameters
Named parameters are more readable form of passing parameters into a query.
If you use jOOQ for SQL string construction, there is a comfortable way to get named parameters as a map.
NamedParameterJdbcTemplate with parameters and RowMapper
Create a row mapper that will map result of a query to a domain object.
Now we can give UserRowMapper to named JDBC template and it will map the result of the query into a new instance of User class.
Use BeanPropertyRowMapper in case your domain model matches name of columns in a database table. Then you do not have to create mappers and data from your queries will automatically transfered to instance of a domain class.
Last updated
Was this helpful?