2017년 1월 23일 월요일

[자바교육/스프링교육/스프링프레임워크/스프링부트학원추천_탑크리에듀]RowCallbackHandler



데이타 베이스에 질의후에는 ResultSet을 순환하여 결과를 가져온다. 스프링은 이러한 일이 항상 필요하다는 것을 알고 그 일을 대신해 줌. 
개발자는 ResultSet내의 각 레코드에 대한 무엇을 가져올 지만 알려주면 된다. 이러한 일은 RowCallBackHandler 인터페이스의 processRow 인터페이스를 구현함으로써 가능 


//아이디를 통해 person 객체를 얻어오는 메소드 
public Person getPerson(final  Integer id) { 
        String sql = “select id, first_name, last_name from person where id =? “; 
        final Person person = new Person();            //질의 대상 객체 생성 
        final Object[] params = new Object[] { id };    //질의 파라미터 생성 
        jdbcTemplate.query(sql, params, new RowCallbackHandler() { 
  public void processRow(ResultSet rs) throws SQLException { 
      person.setId(new Integer(rs.getInt(“id”))); 
      person.setFirstName(rs.getString(“first_name”)); 
      person.setFirstName(rs.getString(“last_name”)); 
  }  //질의된 객체 리턴 
}); 
return person; 
}//: 

댓글 없음:

댓글 쓰기