`
ryan_ncu
  • 浏览: 68782 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jdbcTemplate 用法

    博客分类:
  • JAVA
阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics