Skip to content

Latest commit

 

History

History
105 lines (86 loc) · 3.98 KB

File metadata and controls

105 lines (86 loc) · 3.98 KB

[JDBC_Template]

  1. JDBC_Template를 알기 전

    • DATA_ACCESS_LAYER 이해하기

      Application -> JDBC_Interface -> JDBC_Implementations -> Persistence Layer DAO -> Spring JDBC -> JDBC Driver -> Database | DataSource Configuration for connection

  2. Spring JDBC 사용 과정 (1) DataSource 설정 - DB와의 연결을 위한 DB Server에 관한 정보를 설정한다. * 설정 정보 : url, driver, username, password - 해당 property file에 있는 값을 place holder을 통해 DataSource의 속성으로 설정한 후 해당 BasicDataSource(DataSource interface 중 하나)를 bean으로 등록한다. - Spring JDBC를 사용하려면 먼저, DB Connection을 가져오는 DataSource를 Spring IoC 컨테이너의 공유 가능한 Bean으로 등록해야 한다. - 생성된 BasicDataSource Bean을 Spring JDBC에 주입한다.

    (2) DAO에서의 처리 과정 - DataSource 설정 - 위에서 bean으로 등록한 DataSource를 Setter parameter를 통해 주입한다. * @Autowired에 의해 DataSource Type에 해당하는 bean을 찾아서 주입한다. * 여기서는 DataSource interface 중 하나인 BasicDataSource를 찾아서 주입한다. - SpringJDBC 접근 방법 중 하나인 하나인 JdbcTemplate 객체를 생성하여 dataSource를 주입한다. - CRUD API를 제공한다 (CRUD = Create, Read, Update, Delete) * SQL문을 작성한다 * 2) RowMapper interface 구현을 통해 SQL의 결과(record type)를 객체(object type)에 매핑하여 결과를 리턴한다. + 개발자는 mapRow()라는 interface method를 정의하여 결과를 처리한다. + 한 번만 사용하는 기능의 경우 RowMapper를 익명 클래스로 작성하여 사용한다. ResultSet -> RowMapper.mapRow -> Object (DB에서 읽어온 것) 개발자가 정의해서 작성해야하는 부분 프로그램에서 쓰는 객체 (record type) (Object type)

    • Component("OffersDao")와 @Autowired의 기능
      • 해당 annotation이 없다면, xml을 통해 직접 bean을 등록하고, setter에 주입해야한다.
      • 사전 annotation을 찾아서 bean으로 등록하기 위한 설정이 필요하다.
      • 해당 annotaion을 사용하면 framework가 자동으로 bean으로 등록하고 setter에 주입해준다.
  3. JDBC_Template 사용법

    1. QueryForObject

      • Querying for an Integer
      	/* 해당 학번(10)에 해당하는 학생의 이름 */
      	String SQL = "select count(*) from Student;
      	int rowCount = jdbcTemplateObject.queryForObject(SQL, Integer.class);
      
      • Querying for an String
      	/* 해당 학번(10)에 해당하는 학생의 이름 */
      	String SQL = "select name from Student where id = ?"; 
      	String name = jdbcTemplateObject.queryForObject(SQL, new Object[]{10}, String.class);
      	// 위와 동일. hard coding
      	String name = jdbcTemplateObject.queryForObject(SQL, 10, String.class);		
      
      • Querying and returning an object(하나의 객체)
      	/* 해당 학번(10)에 해당하는 학생 객체 */
      	String SQL = "select * from Student where id = ?"; 
      	Student student = jdbcTemplateObject.queryForObject(SQL, new Object[]{10}, new StudentMapper()); 
      	// RowMapper interface의 구현 클래스 정의 
      	public class StudentMapper implements RowMapper<Student> { 
      	  // interface method
      	  public Student mapRow(ResultSet rs, int rowNum) throws SQLException { 
      	      Student student = new Student(); 
      	
      	      student.setID(rs.getInt("id")); 
      	      student.setName(rs.getString("name")); 
      	      student.setAge(rs.getInt("age")); 
      	
      	      return student; 
      	  }
      	}
      
      

      *** Integer, String은 컴파일러가 해당 타입에 맞게 알아서 매핑해주지만 우리가 만든 객체는 알 수 없기 때문에 직접 Mapping Logic을 구현해야 한다.

      • Query and returning multiple objects
      	/* 모든 학생 객체 */
      	String SQL = "select * from Student"; 
      	List<Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); 
      	// RowMapper interface의 구현 클래스 정의 
      		public class StudentMapper implements RowMapper<Student> {
      		  // interface method
      		  public Student mapRow(ResultSet rs, int rowNum) throws SQLException { 
      		      Student student = new Student(); 
      		
      		      student.setID(rs.getInt("id")); 
      		      student.setName(rs.getString("name")); 
      		      student.setAge(rs.getInt("age")); 
      		
      		      return student; 
      		  } 
      		}
      	}