JDBC๋?
JDBC๋ ์๋ฐ ๊ฐ๋ฐ์๋ค์ด ๊ฐ์ฅ ์ค๋ซ๋์ ์ฌ์ฉํ DB ์ฐ๋ ๊ธฐ์ ์ด๋ค.
JDBC ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฐ์๊ฐ ์์ฑํด์ผ ํ ์ฝ๋๊ฐ ๊ต์ฅํ ๋ง๋ค.
์คํ๋ง์ JDBC ๊ธฐ๋ฐ์ DB ์ฐ๋ ํ๋ก๊ทธ๋จ์ ์ฝ๊ฒ ๊ฐ๋ฐํ ์ ์๋๋ก JDBCTemplate ํด๋์ค๋ฅผ ์ง์ํ๋ค.
๋ฐ๋ณต๋๋ DB ์ฐ๋ ๋ก์ง์ JDBCTemplate ํด๋์ค์ ํ ํ๋ฆฟ ๋ฉ์๋๊ฐ ์ ๊ณตํ๊ณ , ๊ฐ๋ฐ์๋ ๋ฌ๋ผ์ง๋ SQL ๊ตฌ๋ฌธ๊ณผ ์ค์ ๊ฐ๋ง ์ ๊ฒฝ์ฐ๋ฉด ๋๋ค.
์คํ๋ง JDBC ์ค์ ํ๋ ๋ฐฉ๋ฒ
1. pom.xml์์ JDBC์ DBCP ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ค.
<!-- JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
โ
2. ํ๋กํผํฐ ํ์ผ์ ์ด์ฉํ์ฌ DataSource๋ฅผ ๋ฑ๋กํ๋ค.
JdbcTemplate ํด๋์ค๊ฐ JDBC API๋ฅผ ์ด์ฉํ์ฌ DB ์ฐ๋์ ์ฒ๋ฆฌํ๋ ค๋ฉด DB๋ก๋ถํฐ ์ปค๋ฅ์ ์ ์ป์ด์ผ ํ๋ค.
์ด ๊ณผ์ ์ JdbcTemplate ๊ฐ์ฒด๊ฐ ์ฌ์ฉํ DataSource๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ์ฌ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์์ฑํ๋๋ก ์ค์ ํ๋ ๊ฒ์ด๋ค.
์ฐธ๊ณ ๋ก, DataSource ์ค์ ์ ๋งค์ฐ ์ค์ํ ์ค์ ์ด๋ค. ํธ๋์ญ์ ์ฒ๋ฆฌ, Mybatis ์ฐ๋, JPA ์ฐ๋์์๋ ์ฌ์ฉ๋๋ค.
DataSource ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ ๋ค์ํ์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ Apache์ BasicDataSource๋ฅผ ๋ฑ๋กํ๋ค.
๊ฐ์ฒด๊ฐ ์ญ์ ๋๊ธฐ ์ ์ ์ฐ๊ฒฐ์ ํด์ ํ๊ณ ์ close() ๋ฉ์๋๋ฅผ destroy ์์ฑ์ ์ง์ ํ๋ค.
ํ๋กํผํฐ ํ์ผ์ ์ฌ์ฉํ๋ ค๋ฉด <context:property-placeholder>๋ก ์์น๋ฅผ ๋ฑ๋กํด์ผ ํ๋ค.
"${}"๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
[src/main/resources/config/database.properties]
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springquickstart?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true
jdbc.username=jueun
jdbc.password=jueun
[applicationContext.xml ์ค]
<!-- DataSource ์ค์ -->
<context:property-placeholder location="classpath:config/database.properties" />
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
JdbcTemplate ๋ฉ์๋ ์ข ๋ฅ๋?
1. update() ๋ฉ์๋
์ฒซ๋ฒ์งธ ๋ฐฉ์
SQL๋ฌธ์ '?' ์ ๋งํผ ๊ฐ์ ๋์ดํ๋ ๋ฐฉ์
int update(String sql, Object args, Object args......)
๋๋ฒ์งธ ๋ฐฉ์
Object ๋ฐฐ์ด ๊ฐ์ฒด์ SQL ๊ตฌ๋ฌธ์ ์ค์ ๋ '?' ์ ๋งํผ ๊ฐ์ ์ธํ ํด ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ ๋ฐฉ์
int update(String sql, Obejct[] args)
2. queryForInt() ๋ฉ์๋
select ๋ฌธ์ผ๋ก ๊ฒ์๋ ๊ฐ์ค์ ์ ์๋ฅผ ๋ฆฌํด๋ฐ์ ๋ ์ฌ์ฉํ๋ค.
int queryForInt(String sql)
int queryForInt(String sql, Object args...)
int queryForInt(String sql, Object[] args)
3. queryForObject() ๋ฉ์๋
select ๋ฌธ์ผ๋ก ๊ฒ์๋ ๊ฒฐ๊ณผ์์ ๊ฐ์ฒด๋ก ๋ฆฌํด๋ฐ์ ๋ ์ฌ์ฉํ๋ค.
Object queryForObject(String sql)
Object queryForObject(String sql, RowMapper<t> rowMapper)
Object queryForObject(String sql, Object[] args, RowMapper<t> rowMapper)
4. query() ๋ฉ์๋
select ๋ฌธ์ผ๋ก ๊ฒ์๋ ๊ฒฐ๊ณผ์์ ๋ฆฌ์คํธ์ผ ๋ ์ฌ์ฉํ๋ค.
List query(String sql) List query(String sql, RowMapper<t> rowMapper)
List query(String sql, Object[] args, RowMapper<t> rowMapper)
โ
DAO ํด๋์ค ๊ตฌํํด๋ณด์!
DAO ํด๋์ค์์ JDBCTemplate ๊ฐ์ฒด๋ฅผ ์ป๋ ๋ฐฉ๋ฒ์ ๋ ๊ฐ์ง์ด๋ค.
1. ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ: JDBCDaoSupport ํด๋์ค ์์
package com.springbook.biz.board.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.springbook.biz.board.BoardVO;
//DAO(Data Access Object)
@Repository
public class BoardDAOSpring extends JdbcDaoSupport {
// SQL ๋ช
๋ น์ด๋ค
private final String BOARD_INSERT = "insert into board(seq, title, writer, content) values((select nvl(max(seq), 0)+1 from board),?,?,?)";
private final String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
private final String BOARD_DELETE = "delete board where seq=?";
private final String BOARD_GET = "select * from board where seq=?";
private final String BOARD_LIST = "select * from board order by seq desc";
@Autowired
public void setSuperDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
// CRUD ๊ธฐ๋ฅ์ ๋ฉ์๋ ๊ตฌํ
// ๊ธ ๋ฑ๋ก
public void insertBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก insertBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
getJdbcTemplate().update(BOARD_INSERT, vo.getTitle(), vo.getWriter(), vo.getContent());
}
// ๊ธ ์์
public void updateBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก updateBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
getJdbcTemplate().update(BOARD_UPDATE, vo.getTitle(), vo.getContent(), vo.getSeq());
}
// ๊ธ ์ญ์
public void deleteBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก deleteBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
getJdbcTemplate().update(BOARD_DELETE, vo.getSeq());
}
// ๊ธ ์์ธ ์กฐํ
public BoardVO getBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก getBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
Object[] args = { vo.getSeq() };
return getJdbcTemplate().queryForObject(BOARD_GET, args, new BoardRowMapper());
}
// ๊ธ ๋ชฉ๋ก ์กฐํ
public List<BoardVO> getBoardList(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก getBoardList() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
return getJdbcTemplate().query(BOARD_LIST, new BoardRowMapper());
}
}
getJdbcTemplate() ๋ฉ์๋๋ฅผ ์์๋ฐ์ ์ฌ์ฉํ์ฌ update(), query() ๋ฑ ์ํ๋ ๊ธฐ๋ฅ์ ์ธ ์ ์๋ค.
๋ฌธ์ ๋ getJdbcTemplate() ๋ฉ์๋๊ฐ JdbcTemplate ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ค๋ฉด DataSource ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ค.
๊ทธ๋์ ๋ฐ๋ก @Autowired๋ฅผ ํตํด setDataSource() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ DataSource๋ฅผ ์์กด์ฑ ์ฃผ์ ํ๋ค.
๋ฉ์๋ ์์ @Autowired๋ฅผ ๋ถ์ด๋ฉด ํด๋น ๋ฉ์๋๋ฅผ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์๋์ผ๋ก ํธ์ถํ๊ฒ ๋๋ค.
์ด๋ ๋ฉ์๋ ๋งค๊ฐ๋ณ์ ํ์ ์ ํ์ธํ๊ณ ํด๋น ํ์ ์ ๊ฐ์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์กด์ฌํ๋ฉด ๊ทธ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ค๋ค.
2. ๋๋ฒ์งธ ๋ฐฉ๋ฒ: JDBCTemplate ํด๋์ค <bean> ๋ฑ๋ก, ์์กด์ฑ ์ฃผ์
์ผ๋ฐ์ ์ผ๋ก ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
[applicationContext.xml์ ๋น ๋ฑ๋ก]
<!-- Spring JDBC ์ค์ -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
๋ฐ์์ ์ค์ํ ๋ถ๋ถ์ JdbcTemplate ํ์ ์ ๊ฐ์ฒด๋ฅผ @Autowired ๋ง์ผ๋ก ๋๋ธ๋ค๋ ๊ฒ์ด๋ค.
package com.springbook.biz.board.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.springbook.biz.board.BoardVO;
//DAO(Data Access Object)
@Repository
public class BoardDAOSpring {
@Autowired
private JdbcTemplate jdbcTemplate;
// SQL ๋ช
๋ น์ด๋ค
// private final String BOARD_INSERT = "insert into board(seq, title, writer, content) values((select nvl(max(seq), 0)+1 from board),?,?,?)";
private final String BOARD_INSERT = "insert into board(seq, title, writer, content) values(?, ?, ?, ?)";
private final String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
private final String BOARD_DELETE = "delete board where seq=?";
private final String BOARD_GET = "select * from board where seq=?";
private final String BOARD_LIST = "select * from board order by seq desc";
// CRUD ๊ธฐ๋ฅ์ ๋ฉ์๋ ๊ตฌํ
// ๊ธ ๋ฑ๋ก
public void insertBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก insertBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
jdbcTemplate.update(BOARD_INSERT, vo.getSeq(), vo.getTitle(), vo.getWriter(), vo.getContent());
}
// ๊ธ ์์
public void updateBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก updateBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
jdbcTemplate.update(BOARD_UPDATE, vo.getTitle(), vo.getContent(), vo.getSeq());
}
// ๊ธ ์ญ์
public void deleteBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก deleteBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
jdbcTemplate.update(BOARD_DELETE, vo.getSeq());
}
// ๊ธ ์์ธ ์กฐํ
public BoardVO getBoard(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก getBoard() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
Object[] args = { vo.getSeq() };
return jdbcTemplate.queryForObject(BOARD_GET, args, new BoardRowMapper());
}
// ๊ธ ๋ชฉ๋ก ์กฐํ
public List<BoardVO> getBoardList(BoardVO vo) {
System.out.println("===> Spring JDBC๋ก getBoardList() ๊ธฐ๋ฅ ์ฒ๋ฆฌ");
return jdbcTemplate.query(BOARD_LIST, new BoardRowMapper());
}
}
'Spring > Spring Quick Start' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Model 2 ์ํคํ ์ณ๋ก ๊ฒ์ํ ๊ฐ๋ฐ (Controller์ ๋ฑ์ฅ) (0) | 2021.01.07 |
---|---|
Model 1 ์ํคํ ์ณ๋ก ๊ฒ์ํ ๊ฐ๋ฐ (JSP์ JavaBeans๋ง์ ์ฌ์ฉ) (0) | 2021.01.07 |
ํธ๋์ญ์ (Transaction) ์ ์ฉ (0) | 2021.01.07 |
Annotation ๊ธฐ๋ฐ AOP ์ ์ฉ (0) | 2021.01.07 |
JointPoint ๋ฉ์๋ (0) | 2021.01.07 |
Advice ๋์ ์์ (0) | 2021.01.07 |