Spring 09 Spring DAO(jdbc)

Kang.__.Mingu·2024년 9월 8일

Spring

목록 보기
8/21

Spring DAO

  • SpringDAO 기능을 사용해 DAO 클래스 작성

  • spring-jdbc 라이브러리의 클래스 사용

    • Template Method Pattern이 적용된 JdbcTemplate 객체의 메소드의 호출하여 DAO 클래스의 메소드 작성
  • Template Method Pattern: 기능 구현을 쉽게 제공하기 위해 메소드에 필요한 명령이 미리 작성된 디자인 패턴

    • 템플릿 메소드의 매개변수에 필요한 값(객체)를 전달하여 원하는 기능 구현 가능
  • spring-jdbc 라이브러리 외에 Driver 클래스(ojdbc 라이브러리)를 제공하는 라이브러리도 빌드 처리 해야된다.


XML 환경설정

가장 먼저 환경설정 세팅을 해줘야한다.

  • 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>

DataSourceApp

  • DataSource 객체: 다수의 Connection 객체를 미리 생성하여 저장하고 있는 객체 - DBCP(DataBase Connection Pool)
    => Spring Bean Configuration File에서 DataSource 인터페이스를 상속받은 자식클래스를 Spring 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(클래스)

profile
최선을 다해 꾸준히 노력하는 개발자 망고입니당 :D

0개의 댓글