日期:2014-05-16  浏览次数:20389 次

Spring数据库访问(HSQL)(四)
    本文接上一篇继续研究Spring的JDBC模板。
    之前,我们使用JDBC模板的方法是在每次调用方法时生成该对象,并使用了一个dataSource作为参数,这个dataSource是在生成DAO的实现类时Spring进行注入的。显然,这样的方式并不是最佳的做法,因为每次方法调用时都需要重新生成新的JDBC模板对象,那么现在,我们就来看看如何优化我们的程序。首先是简化JDBC模板的创建,那么我们就采用注入的方式来进行:
package org.ourpioneer.vehicle.dao;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.ourpioneer.vehicle.bean.Vehicle;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
/**
 * VehicleDAO实现类
 * 
 * @author Nanlei
 * 
 */
public class VehicleDAOImpl implements VehicleDAO {
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public void delete(Vehicle vehicle) {
		String sql = "delete from vehicle where ID=?";
		jdbcTemplate.update(sql, vehicle.getId());
	}
	public Vehicle findById(int id) {
		String sql = "select * from vehicle where ID=?";
		BeanPropertyRowMapper<Vehicle> vehicleRowMapper = BeanPropertyRowMapper
				.newInstance(Vehicle.class);
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				vehicleRowMapper);
	}
	public void insert(final Vehicle vehicle) {
		String sql = "insert into vehicle(PLATE,CHASSIS,COLOR,WHEEL,SEAT) values(?,?,?,?,?)";
		jdbcTemplate.update(sql, vehicle.getPlate(), vehicle.getChassis(),
				vehicle.getColor(), vehicle.getWheel(), vehicle.getSeat());
	}
	public void update(Vehicle vehicle) {
		String sql = "update vehicle set PLATE=?,CHASSIS=?,COLOR=?,WHEEL=?,SEAT=? where ID=?";
		jdbcTemplate.update(sql, vehicle.getPlate(), vehicle.getChassis(),
				vehicle.getColor(), vehicle.getWheel(), vehicle.getSeat(),
				vehicle.getId());
	}
	public void insertBatch(final List<Vehicle> vehicles) {
		String sql = "insert into vehicle(PLATE,CHASSIS,COLOR,WHEEL,SEAT) values(?,?,?,?,?)";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement pstat, int i)
					throws SQLException {
				Vehicle vehicle = vehicles.get(i);
				pstat.setString(1, vehicle.getPlate());
				pstat.setString(2, vehicle.getChassis());
				pstat.setString(3, vehicle.getColor());
				pstat.setInt(4, vehicle.getWheel());
				pstat.setInt(5, vehicle.getSeat());
			}
			public int getBatchSize() {
				return vehicles.size();
			}
		});
	}
	public List<Vehicle> findAll() {
		String sql = "select * from vehicle";
		RowMapper<Vehicle> rows = BeanPropertyRowMapper
				.newInstance(Vehicle.class);
		return jdbcTemplate.query(sql, rows);
	}
	public int countVehicle() {
		String sql = "select count(*) from vehicle";
		return jdbcTemplate.queryForInt(sql);
	}
	public String getChassis(int id) {
		String sql = "select COLOR from vehicle where ID=?";
		return (String) jdbcTemplate.queryForObject(sql, new Object[] { id },
				String.class);
	}
}

    精简后的代码就非常短了,也非常的清晰。那么采用注入之后,需要修改Spring的配置文件,当然这也非常的简单:
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="vehicleDAO" class="org.ourpioneer.vehicle.dao.VehicleDAOImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>

    除了这种使用方式,我们还可以直接扩展Spring的JdbcDaoSupport抽象类,那么只需让我们的DAO实现类扩展JdbcDaoSupport即可,配置文件中直接给DAO注入数据源即可,而且我们不需要再显式注入它们,因为JdbcDaoSupport已经为我们做了这些工作了,代码就得到了进一步的精简:
public class VehicleDAOImpl extends JdbcDaoSupport implements VehicleDAO {
	public void delete(Vehicle vehicle) {
		String sql = "delete from vehicle where ID=?";
		getJdbcTemplate().update(sql, vehicle.getId());
	}
	… … …
}

    对应修改配置文件: