๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

์Šคํ”„๋ง JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ - JdbcTemplate

by Leica 2020. 4. 27.
๋ฐ˜์‘ํ˜•

์Šคํ”„๋ง JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ - JdbcTemplate

Spring Framework๋Š” JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•ด JdbcTemplate ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ JdbcTemplate ํด๋ž˜์Šค๋Š” ์†์‰ฝ๊ฒŒ DB์™€ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

์ด ๊ธ€์—์„œ๋Š” Oracle DB 11g๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

๋‹ค๋ฅธ DB๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

 

์˜์กด์„ฑ ์ถ”๊ฐ€

Spring JDBC, Oracle JDBC ๋“œ๋ผ์ด๋ฒ„, Apache Commons DBCP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>5.1.9.RELEASE</version>
</dependency>

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc6</artifactId>
	<version>11.2.0.3</version>
</dependency>

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-dbcp2</artifactId>
	<version>2.7.0</version>
</dependency>

 

Oracle JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

<repository>
	<id>oracle</id>
	<name>ORACLE JDBC Repository</name>
	<url>http://code.lds.org/nexus/content/groups/main-repo</url>
</repository>

 

ํ…Œ์ด๋ธ” ๊ตฌ์กฐ

๋Œ€์ƒ ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

create table jdbc_table(
int_data number not null,
str_data varchar2(500) not null
);

 

DataSource, JdbcTemplate ๋นˆ ์ •์˜

์Šคํ”„๋ง ๋นˆ ์„ค์ • ํด๋ž˜์Šค์—์„œ DataSource, JdbcTemplate ๋นˆ์„ ์ •์˜ํ•œ๋‹ค.

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@ComponentScan
public class BeanConfigClass {
	
	// DataSource
	@Bean
	public BasicDataSource source() {
		BasicDataSource source = new BasicDataSource();
		source.setDriverClassName("oracle.jdbc.OracleDriver");
		source.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
		source.setUsername("scott");
		source.setPassword("1234");
		
		return source;
	}
	
	// JdbcTemplate
	@Bean
	public JdbcTemplate db(BasicDataSource source) {
		JdbcTemplate db = new JdbcTemplate(source);
        
		return db;
	}
}

JdbcTemplate ๋นˆ์„ ์ •์˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ BasicDataSource๋ฅผ ๋ฐ›์•„ JdbcTemplate ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

VO ํด๋ž˜์Šค

@Component
@Scope("prototype")
public class JdbcBean {
	
	private int int_data;
	private String str_data;
	
	public int getInt_data() {
		return int_data;
	}
	public void setInt_data(int int_data) {
		this.int_data = int_data;
	}
	public String getStr_data() {
		return str_data;
	}
	public void setStr_data(String str_data) {
		this.str_data = str_data;
	}
}

ํ…Œ์ด๋ธ” ๊ตฌ์กฐ์™€ ๋™์ผํ•œ VO ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

Component๋ฅผ ๋ถ™์—ฌ VOํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  scope๋Š” prototype์œผ๋กœ ํ•œ๋‹ค.

์ปฌ๋Ÿผ๊ณผ ๋งคํ•‘๋˜๋Š” ํ•„๋“œ๋ฅผ ์„ ์–ธํ•˜๋Š”๋ฐ, ์ปฌ๋Ÿผ๋ช…๊ณผ ๋‹ฌ๋ผ๋„ ๋ฌด๊ด€ํ•˜๋‹ค.

 

DAO ํด๋ž˜์Šค

import org.springframework.jdbc.core.JdbcTemplate;

@Component
public class JdbcDAO {
	
	@Autowired
	private JdbcTemplate db;
}

DAO ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  JdbcTemplate ๋นˆ์„ ์ฃผ์ž…๋ฐ›๋Š”๋‹ค.

JdbcTemplate์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ insert, select, update, delete๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Insert, Select, Update, Delete ๊ตฌํ˜„

1) Insert

insert๋Š” update()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

import org.springframework.jdbc.core.JdbcTemplate;

@Component
public class JdbcDAO {
	
	@Autowired
	private JdbcTemplate db;
    
	// insert
	public void insert_data(JdbcBean bean) {
		String sql = "insert into jdbc_table (int_data, str_data) values (?, ?)";
		db.update(sql, bean.getInt_data(), bean.getStr_data());
	} 
}

update() ํ˜ธ์ถœ ์‹œ SQL๊ณผ '?'์— ๋ฐ”์ธ๋”ฉ๋  ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ๋„˜๊ฒจ์ค€๋‹ค.

 

public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfigClass.class);
	
	// dao๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
	JdbcDAO dao = ctx.getBean(JdbcDAO.class);
	
	// insert
	JdbcBean bean1 = ctx.getBean(JdbcBean.class);
	bean1.setInt_data(1);
	bean1.setStr_data("๋ฌธ์ž์—ด1");
	dao.insert_data(bean1);

	JdbcBean bean2 = ctx.getBean(JdbcBean.class);
	bean2.setInt_data(2);
	bean2.setStr_data("๋ฌธ์ž์—ด2");
	dao.insert_data(bean2);
	
	ctx.close();
}

์œ„๋Š” ๊ตฌํ˜„ํ•œ DAO๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ๊ฐœ์˜ row๋ฅผ insertํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

2) Select

JdbcTemplate๋กœ select๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋จผ์ € Mapper ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

Select๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ DB ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๊ณผ ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งคํ•‘ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

Mapper ํด๋ž˜์Šค

import org.springframework.jdbc.core.RowMapper;

@Component
public class MapperClass implements RowMapper<JdbcBean> {
	
	public JdbcBean mapRow(ResultSet rs, int rowNum) throws SQLException {
		JdbcBean bean = new JdbcBean();
		bean.setInt_data(rs.getInt("int_data"));
		bean.setStr_data(rs.getString("str_data"));
		
		return bean;
	}
}

์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•˜๋Š” RowMapper ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ Mapper ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค.

RowMapper์˜ ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ์œ„์—์„œ ๋งŒ๋“  VO ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•œ๋‹ค.

mapRow์—์„œ ๋ฐ›๋Š” ResultSet ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ์™€ ์ปฌ๋Ÿผ์„ ์–ด๋–ป๊ฒŒ ๋งคํ•‘ํ•  ๊ฒƒ์ธ์ง€ ์ •์˜ํ•œ๋‹ค.

 

DAO ํด๋ž˜์Šค

DAO ํด๋ž˜์Šค์—์„œ ์œ„์—์„œ ๋งŒ๋“  Mapper ํด๋ž˜์Šค๋ฅผ ์ฃผ์ž…๋ฐ›๋Š”๋‹ค.

import org.springframework.jdbc.core.JdbcTemplate;

@Component
public class JdbcDAO {
	
	@Autowired
	private JdbcTemplate db;
    
	@Autowired
	private MapperClass mapper;
    
	// insert
	public void insert_data(JdbcBean bean) {
		String sql = "insert into jdbc_table (int_data, str_data) values (?, ?)";
		db.update(sql, bean.getInt_data(), bean.getStr_data());
	} 
    
	// select
	public List<JdbcBean> select_data(){
		String sql = "select int_data, str_data from jdbc_table";
		List<JdbcBean> list = db.query(sql, mapper);
		
		return list;
	}    
}

JdbcTemplate์˜ query()๋ฅผ ์‚ฌ์šฉํ•ด์„œ select๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

query() ํ˜ธ์ถœ ์‹œ SQL๊ณผ Mapper ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

Select ๊ฒฐ๊ณผ๋Š” List ํƒ€์ž…์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

Main ๋ฉ”์†Œ๋“œ

public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfigClass.class);
	
	// dao๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
	JdbcDAO dao = ctx.getBean(JdbcDAO.class);
	
	// insert
	JdbcBean bean1 = ctx.getBean(JdbcBean.class);
	bean1.setInt_data(1);
	bean1.setStr_data("๋ฌธ์ž์—ด1");
	dao.insert_data(bean1);

	JdbcBean bean2 = ctx.getBean(JdbcBean.class);
	bean2.setInt_data(2);
	bean2.setStr_data("๋ฌธ์ž์—ด2");
	dao.insert_data(bean2);

	// select
	List<JdbcBean> list = dao.select_data();

	for(JdbcBean bean3 : list) {
		System.out.printf("int_data : %d\n", bean3.getInt_data());
		System.out.printf("str_data : %s\n", bean3.getStr_data());
		System.out.println("----------------------------------------");
	}
    
	ctx.close();
}

 

3) Update

update๋„ insert์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ JdbcTemplate์˜ update()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

import org.springframework.jdbc.core.JdbcTemplate;

@Component
public class JdbcDAO {
	
	@Autowired
	private JdbcTemplate db;
    
	@Autowired
	private MapperClass mapper;
    
	// insert
	public void insert_data(JdbcBean bean) {
		String sql = "insert into jdbc_table (int_data, str_data) values (?, ?)";
		db.update(sql, bean.getInt_data(), bean.getStr_data());
	} 
    
	// select
	public List<JdbcBean> select_data(){
		String sql = "select int_data, str_data from jdbc_table";
		List<JdbcBean> list = db.query(sql, mapper);
		
		return list;
	}    
    
	// update
	public void update_data(JdbcBean bean) {
		String sql = "update jdbc_table set str_data = ? where int_data = ?";
		db.update(sql, bean.getStr_data(), bean.getInt_data());
	}    
}
public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfigClass.class);
	
	// dao๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
	JdbcDAO dao = ctx.getBean(JdbcDAO.class);
	
	// insert
	JdbcBean bean1 = ctx.getBean(JdbcBean.class);
	bean1.setInt_data(1);
	bean1.setStr_data("๋ฌธ์ž์—ด1");
	dao.insert_data(bean1);

	JdbcBean bean2 = ctx.getBean(JdbcBean.class);
	bean2.setInt_data(2);
	bean2.setStr_data("๋ฌธ์ž์—ด2");
	dao.insert_data(bean2);

	// select
	List<JdbcBean> list = dao.select_data();

	for(JdbcBean bean3 : list) {
		System.out.printf("int_data : %d\n", bean3.getInt_data());
		System.out.printf("str_data : %s\n", bean3.getStr_data());
		System.out.println("----------------------------------------");
	}
    
	// update
	JdbcBean bean4 = ctx.getBean(JdbcBean.class);
	bean4.setInt_data(1);
	bean4.setStr_data("๋ฌธ์ž์—ด3");
	dao.update_data(bean4);        
    
	ctx.close();
}

 

4) Delete

delete๋„ insert, update์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ JdbcTemplate์˜ update()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

import org.springframework.jdbc.core.JdbcTemplate;

@Component
public class JdbcDAO {
	
	@Autowired
	private JdbcTemplate db;
    
	@Autowired
	private MapperClass mapper;
    
	// insert
	public void insert_data(JdbcBean bean) {
		String sql = "insert into jdbc_table (int_data, str_data) values (?, ?)";
		db.update(sql, bean.getInt_data(), bean.getStr_data());
	} 
    
	// select
	public List<JdbcBean> select_data(){
		String sql = "select int_data, str_data from jdbc_table";
		List<JdbcBean> list = db.query(sql, mapper);
		
		return list;
	}    
    
	// update
	public void update_data(JdbcBean bean) {
		String sql = "update jdbc_table set str_data = ? where int_data = ?";
		db.update(sql, bean.getStr_data(), bean.getInt_data());
	}
    
	// delete
	public void delete_data(int int_data) {
		String sql = "delete from jdbc_table where int_data = ?";
		db.update(sql, int_data);
	}    
}
public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfigClass.class);
	
	// dao๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
	JdbcDAO dao = ctx.getBean(JdbcDAO.class);
	
	// insert
	JdbcBean bean1 = ctx.getBean(JdbcBean.class);
	bean1.setInt_data(1);
	bean1.setStr_data("๋ฌธ์ž์—ด1");
	dao.insert_data(bean1);

	JdbcBean bean2 = ctx.getBean(JdbcBean.class);
	bean2.setInt_data(2);
	bean2.setStr_data("๋ฌธ์ž์—ด2");
	dao.insert_data(bean2);

	// select
	List<JdbcBean> list = dao.select_data();

	for(JdbcBean bean3 : list) {
		System.out.printf("int_data : %d\n", bean3.getInt_data());
		System.out.printf("str_data : %s\n", bean3.getStr_data());
		System.out.println("----------------------------------------");
	}
    
	// update
	JdbcBean bean4 = ctx.getBean(JdbcBean.class);
	bean4.setInt_data(1);
	bean4.setStr_data("๋ฌธ์ž์—ด3");
	dao.update_data(bean4);  
    
	// delete
	dao.delete_data(1);    
    
	ctx.close();
}

 

๊ด€๋ จ ๊ธ€

- ์Šคํ”„๋ง JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ - MyBatis ์—ฐ๋™

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€