• Home
  • About
    • Young's Github Pages photo

      一日不作一日不食

    • About
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Java EE 정리 41

24 Apr 2019

Reading time ~2 minutes

Java EE 정리 41 - Spring JDBC


Spring JDBC

  • JDBC를 단순화하여 제공하는 기능
  • 기존 JDBC의 순서
    • 드라이버 로딩 - 커넥션 얻기 - 쿼리문 생성객체 얻기 - 쿼리 수행 결과 얻기 - 연결 끊기
  • Spring JDBC가 해주는 것 (Spring Framework Reference Docs p406)
    • Spring JDBC를 사용하면 사용자가 하는일이 줄어든다

01

  • JdbcTemplate 객체 사용
    • insert, update, delete => update() 메서드 사용
    • select => queryForObject(), query() 메서드** 사용
  • Spring JDBC에서 DB연결 관리한다
    • 연결 얻기, 연결 종료 등
  • 쿼리문 수행 객체(Statement, PreparedStatement)도 Spring JDBC에서 자동 처리
  • DB에 연동하게될 계정정보, Driver 정보, 설정 파일에서 설정한다
    • 설정파일
      • MVC - servlet-context.xml
      • DI - applicationContext.xml
  • 값은 bind변수로 처리할 수 있다
  • 조회시에 ResultSet을 직접 얻어서 사용하지 않고 RowMapper를 통해서 얻고 사용하게 된다

Spring JDBC 설정

  • Maven Repository에서 Spring JDBC depedency(Spring JDBC, DAO)를 얻어서 pom.xml에 의존성 추가
    • 받아진 jar파일을 WEB-INF/lib에 옮기기(개발뿐만 아니라 운영시에도 사용하기 위해서)
    • Build Path를 설정해서 사용가능하도록 변경 02
<!-- pom.xml에 추가 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc  -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-dao -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-dao</artifactId>
    <version>2.0.8</version>
</dependency>
...
  • DB 연동 설정 (DBCP 사용)
    • DriverManagerDataSource를 사용 DBMS와 연동하는 bean 설정
<!-- servlet-context.xml -->
...
      <context:annotation-config/>
      <context:component-scan base-package="kr.co.sist" />
      
      <!-- DBMS와 연동 하는 bean 설정 -->
      <beans:bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <beans:property name="driverClassName"  value="oracle.jdbc.OracleDriver"/>
            <beans:property name="url"  value="jdbc:oracle:thin:@localhost:1521:orcl"/>
            <beans:property name="username" value="scott"/>
            <beans:property name="password" value="tiger"/>
      </beans:bean>
      
...
  • JDBC 객체를 설정 (DB연동 설정 정보를 가지고)
    • Method Injection방식을 사용
<!-- servlet-context.xml -->
...
      <!-- Spring JDBC를 사용할 수 있는 객체 설정 : JdbcTemplate -->
      <beans:bean id="jt"  class="org.springframework.jdbc.core.JdbcTemplate">
            <beans:property name="dataSource" ref="dataSource"/>
      </beans:bean>
...
  • Spring JDBC 사용 예제를 위해 패키지, 클래스 생성
    • annotation 스캔이 가능하도록 설정
<!-- servlet-context.xml -->
...
      <context:annotation-config/>
      <context:component-scan base-package="kr.co.sist" />
      <context:component-scan base-package="kr.co.dao" />
      <context:component-scan base-package="kr.co.dao.*" />
      <context:component-scan base-package="kr.co.service" />
      <context:component-scan base-package="kr.co.service.*" />
...
@Component
public class JdbcDAO {
      
      @Autowired(required=false)
      private JdbcTemplate jt;
      
      public JdbcTemplate getJt() {
            return jt;
      }
}
@Component
public class JdbcService {
      @Autowired(required=false)
      private JdbcDAO jdao;
      
      public void daoPrint() {
            System.out.println("쿼리 실행 객체 : "+jdao.getJt());
      }
}
@Controller
public class JdbcController {
      @Autowired
      private JdbcService js;
      
      @RequestMapping("index.do")
      private String test() {
            js.daoPrint();
            return "test";
      }
}
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
      location.href="index.do";
</script>
</head>
<body>
</body>
</html>

03



Java EESpring