ApplicationContext.xml:
<bean id="jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" >
<property name = "dataSource" >
<ref bean = "dataSource" />
</property >
</bean>
DAO:
@Autowired
private JdbcTemplate jdbcTemplate;
public List getAllByJDBC() throws SQLException {
String sql = "select * from depts";
RowMapper rowMapper = new DeptRowMapper();
List a = jdbcTemplate.query(sql, rowMapper);
return a;
}
/**
* 部门映射
* @author yanke
*
*/
private class DeptRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Dept dept = new Dept();
int id = rs.getInt("id");
String name = rs.getString("name");
dept.setId(id);
dept.setName(name);
return dept;
}
}