日期:2014-05-16 浏览次数:20389 次
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); } }
<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>
public class VehicleDAOImpl extends JdbcDaoSupport implements VehicleDAO { public void delete(Vehicle vehicle) { String sql = "delete from vehicle where ID=?"; getJdbcTemplate().update(sql, vehicle.getId()); } … … … }