ITeye博客

jdbcTemplate 用法

ryan_ncu · 2009-11-30 11:37 · JAVA · Markdown

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;
  }
 }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。