Java Reference
In-Depth Information
You can also provide a SQL parameter source, whose responsibility is to offer SQL parameter values
for named SQL parameters. There are three implementations of the SqlParameterSource interface. The
basic one is MapSqlParameterSource , which wraps a map as its parameter source.
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class JdbcVehicleDao extends SimpleJdbcDaoSupport implements
VehicleDao {
public void insert(Vehicle vehicle) {
String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
+ "VALUES (:vehicleNo, :color, :wheel, :seat)";
Map<String, Object> parameters = new HashMap<String, Object>();
...
SqlParameterSource parameterSource =
new MapSqlParameterSource(parameters);
getSimpleJdbcTemplate().update(sql, parameterSource);
}
...
}
Another implementation of SqlParameterSource is BeanPropertySqlParameterSource , which wraps a
normal Java object as a SQL parameter source. For each of the named parameters, the property with the
same name will be used as the parameter value.
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class JdbcVehicleDao extends SimpleJdbcDaoSupport implements
VehicleDao {
public void insert(Vehicle vehicle) {
String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) "
+ "VALUES (:vehicleNo, :color, :wheel, :seat)";
SqlParameterSource parameterSource =
new BeanPropertySqlParameterSource(vehicle);
getSimpleJdbcTemplate().update(sql, parameterSource);
}
...
}
Search WWH ::




Custom Search