日期:2014-05-16 浏览次数:20520 次
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite:test.db" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="employeeDao" class="cn.lettoo.spring.jdbc.EmployeeDao"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean>
package cn.lettoo.spring.jdbc; import java.util.List; public interface IDao<T> { int insert(T object); int delete(T object); int update(T object); T select(Object id); List<T> selectList(String condition); }
package cn.lettoo.spring.jdbc; import org.springframework.jdbc.core.JdbcTemplate; public abstract class AbstractDao<T> implements IDao<T> { protected JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }
package cn.lettoo.spring.jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.RowMapper; import cn.lettoo.jdbc.Employee; import cn.lettoo.jdbc.Department; import cn.lettoo.jdbc.SqlParser; public class EmployeeDao extends AbstractDao<Employee> implements IDao<Employee> { public int insert(Employee employee) { String sql = SqlParser.getInstance().getSql("Employee.insert"); Object[] args; if (employee.getDepartment() != null) { args = new Object[] { employee.getId(), employee.getName(), employee.getDepartment().getId(), employee.getDescription() }; } else { args = new Object[] { employee.getId(), employee.getName(), java.sql.Types.NULL, employee.getDescription() }; } return jdbcTemplate.update(sql, args); } public int delete(Emplo