JdbcTemplate 클래스


=>Spring JDBC 클래스

=>sql의 매개변수를 Object [] 을 이용해서 매핑하는 클래스



[생성자]

JdbcTemplate(DataSource dataSource)


[select 구문을 수행해주는 메소드]


[select 구문의 결과가 여러 개의 행일 때 사용]


List query(String sql, RowMapper rowMapper)

List query(String sql, Object [] args, RowMapper rowMapper)



[select 구문의 결과가 하나의 행일 때 사용]

Object queryForObject(String sql, RowMapper rowMapper)

Object queryForObject(String sql, Object [] args, RowMapper rowMapper)


[select 구문의 결과가 하나의 행일 때 하나의 Map 에 결과를 저장하고자 하는 경우 사용]

Map<String, Object> queryForMap(String sql)

Map<String, Object> queryForMpa(String sql, Object [] args)


[RowMapper 인터페이스]

=>select 구문의 결과로 반환되는 하나의 행을 객체로 만들기 위한 메소드를 소유한 인터페이스

=>mapRow(ResultSet rs, int rownum) 추상 메소드를 소유

=>rs는 하나의 행을 저장한 객체이고 rownum은 행 번호를 저장한 정수



[JdbcTemplate 클래스를 이용한 테이블의 데이터 읽기]

1.spring-jdbc 라이브러리 다운로드
=>pom.xml 파일에 spring-jdbc 의존성 확인

2.spring 설정 파일(bean configuration 파일 - applicationContext.xml)에 DataSource 빈이 추가되어 있는지 확인




3.샘플 테이블 및 데이터 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--스쿨버스를 위한 db설계 
-- busnum , name , price , sysdate
 
--시퀀스 생성 
create sequence busnumseq;
 
--테이블 생성 
create table schoolbus(
busnumber number(10primary key,
name varchar2(20not null,
price number(10),
writedate date default sysdate
);
 
--샘플데이터
insert into schoolbus (busnumber, name, price, writedate ) values(busnumseq.nextval, '붕붕카'300, sysdate);
insert into schoolbus (busnumber, name, price, writedate ) values(busnumseq.nextval, '서울카'500, sysdate);
 
--데이터 확인 
select *
from schoolbus;
 
--작업완료 
commit
 
cs


4. JdbcTeplate  클래스의 객체를 소유한 Dao 클래스 생성 dao.schoolbus


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package dao;
 
import java.util.Map;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
public class SchoolBus {
    // Spring JDBC 기본 클래스
    private JdbcTemplate jdbcTemplate;
 
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    // 데이터의 개수를 알려주는 메소드
    public Map<String, Object> count() {
        // 하나의 행만 리턴하는 select 구문 수행
        return jdbcTemplate.queryForMap("select count(*) from schoolbus");
 
    }
}
 
 
cs


5. spring 설정파일에 SchoolBus 객체를 만들기  

1
2
3
4
5
6
7
8
9
10
11
<bean class="org.springframework.jdbc.core.JdbcTemplate" id = "jdbcTemplate">
        <!--  생성자 주입  -->
        <constructor-arg ref= "dataSource"></constructor-arg>
    </bean>
    
    
    <!-- school 클래스의 객체를 생성 IOC -->
    <bean class ="dao.SchoolBus" id="shcoolbus">
    <!-- jdbcTemplate이라는  id를 가진 bean을 jdbcTemplate 이라는 프로퍼티에 주입 :DI  -->
    <property name="jdbcTemplate" ref ="jdbcTemplate"></property>
    </bean>
cs


6.main 메소드를 소유한 Main 클래스를 만들고 main 메소드 실행


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.context.support.GenericXmlApplicationContext;
 
import dao.SchoolBus;
 
public class Main {
 
    public static void main(String[] args) {
        GenericXmlApplicationContext context = 
            new GenericXmlApplicationContext(
                            "classpath:applicationContext.xml");
        SchoolBus bus = context.getBean(SchoolBus.class);
        //select 절에 쓴 내용을 적기 
        System.out.println(bus.count().get("COUNT(*)"));
    }
 
}
 
cs



+ Recent posts