Java Reference
In-Depth Information
You can test your findAll() method with the following code snippet in the Main class:
package com.apress.springenterpriserecipes.vehicle;
...
public class Main {
public static void main(String[] args) {
...
VehicleDao vehicleDao = (VehicleDao) context.getBean("vehicleDao");
List<Vehicle> vehicles = vehicleDao.findAll();
for (Vehicle vehicle : vehicles) {
System.out.println("Vehicle No: " + vehicle.getVehicleNo());
System.out.println("Color: " + vehicle.getColor());
System.out.println("Wheel: " + vehicle.getWheel());
System.out.println("Seat: " + vehicle.getSeat());
}
}
}
If you use a RowMapper<T> object to map the rows in a result set, you will get a list of mapped objects
from the query() method.
package com.apress.springenterpriserecipes.vehicle;
...
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcVehicleDao implements VehicleDao {
...
public List<Vehicle> findAll() {
String sql = "SELECT * FROM VEHICLE";
RowMapper<Vehicle> rm =
ParameterizedBeanPropertyRowMapper.newInstance(Vehicle.class);
List<Vehicle> vehicles = getSimpleJdbcTemplate().query(sql, rm);
return vehicles;
}
}
Querying for a Single Value
Finally, let's see how to query for a single-row and single-column result set. As an example, add the
following operations to the DAO interface:
package com.apress.springenterpriserecipes.vehicle;
...
public interface VehicleDao {
...
public String getColor(String vehicleNo);
public int countAll();
}
Search WWH ::




Custom Search