SpringDAO 기능을 사용해 DAO 클래스 작성
spring-jdbc 라이브러리의 클래스 사용
Template Method Pattern: 기능 구현을 쉽게 제공하기 위해 메소드에 필요한 명령이 미리 작성된 디자인 패턴
spring-jdbc 라이브러리 외에 Driver 클래스(ojdbc 라이브러리)를 제공하는 라이브러리도 빌드 처리 해야된다.
가장 먼저 환경설정 세팅을 해줘야한다.
DataSource 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록
spring-jdbc 라이브러리의 DriverManagerDataSource 클래스를 Spring Bean으로 등록해 DBCP(DataBase Connection Pool) 기능을 제공하는 DataSource 객체로 생성
DriverManagerDataSource 클래스의 필드에 Connection 객체를 생성하기 위한 값 주입
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- JdbcTemplate 클래스를 Spring Bean으로 등록 -->
<!-- => JdbcTemplate 클래스의 dataSource 필드에 DataSource 객체가 저장되도록 의존성 주입 -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- StudentDAO 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 -->
<!-- => StudentDAOImpl 클래스의 jdbcTemplate 필드에 JdbcTemplate 객체가 저장되도록 의존성 주입 -->
<bean class="xyz.itwill07.dao.StudentDAOImpl" id="studentDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- StudentService 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 -->
<!-- => StudentServiceImpl 클래스의 studentDAO 필드에 StudentDAO 객체가 저장되도록 의존성 주입 -->
<bean class="xyz.itwill07.dao.StudentServiceImpl" id="studentService">
<property name="studentDAO" ref="studentDAO"/>
</bean>
스프링 컨테이너로부터 DataSource 객체를 제공받아 DataSource 객체로 Connection 객체를 반환받아 Connection 객체의 정보를 출력하는 프로그램
public class DataSourceApp {
public static void main(String[] args) throws SQLException {
ApplicationContext context=new ClassPathXmlApplicationContext("07_dao.xml");
System.out.println("=============================================================");
DataSource dataSource=context.getBean("dataSource", DataSource.class);
System.out.println("dataSource = "+dataSource);
Connection connection=dataSource.getConnection();
System.out.println("connection = "+connection);
connection.close();
System.out.println("=============================================================");
((ClassPathXmlApplicationContext)context).close();
}
}
보면서 이해하는게 빠름
→ DataSourceApp(클래스)
→ Student(클래스)
→ StudentDAO(인터페이스)
→ StudentDAOImpl(클래스)
→ StudentRowMapper(클래스)
→ StudentService(인터페이스)
→ StudentServiceImpl(클래스)
→ StudentApp(클래스)