์คํ๋ง 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();
}
๋๊ธ