๋ฐ์ํ
ํ๊ฒฝ์ค์
build.gradle
ํ์ผ์ jdbc
, h2
๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
์คํ๋ง ๋ถํธ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์ ์ถ๊ฐresources/application.properties
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
์ฌ๊ธฐ์ datasource๊ฐ ์คํ๋ง ๋น์ ๋ฑ๋ก์ด ๋๊ณ db ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ๋ฃ์ด์ค๋ค. ์ด๊ฒ์ ์ฐ๋ฆฌ๋ Injection ๋ฐ์์ ์ฌ์ฉํ๋ฉด ๋๋ค!
Jdbc ๋ฆฌํฌ์งํ ๋ฆฌ ๊ตฌํ
package hello.hellospring.repository;
import hello.hellospring.domain.Member;
import org.springframework.jdbc.datasource.DataSourceUtils;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class JdbcMemberRepository implements MemberRepository {
private final DataSource dataSource;
//application.properties์์ ์ค์ ํ๋ datasource๋ฅผ ์ฃผ์
๋ฐ์ ์ฌ์ฉํ๋ค!
public JdbcMemberRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public Member save(Member member) {
String sql = "insert into member(name) values(?)";
//๊ผญ null๋ก ์ด๊ธฐํ ์์ผ์ค์ผํ๋ค.
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection(); //์ปค๋ฅ์
์ ๋ฐ์์์
pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); // sql๋ฌธ ๋๊ธฐ๊ณ , ๊ณ ์ ํค๋ฅผ ๋๊ฒจ ๋ฐ๋๋ค
pstmt.setString(1, member.getName()); // ํค ๊ฐ, ์ด๋ฆ ์ธํ
pstmt.executeUpdate(); // ์ฟผ๋ฆฌ ์คํ
rs = pstmt.getGeneratedKeys(); // ๊ฒฐ๊ณผ๊ฐ์ ๊ธฐ๋ณธํค ์ธํ
ํด์ฃผ๊ณ
if (rs.next()) {
member.setId(rs.getLong(1));
} else {
throw new SQLException("id ์กฐํ ์คํจ");
}
return member;
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
// ๋ง์ง๋ง์ ๋ฌด์กฐ๊ฑด ๋ชจ๋ ์ฐ๊ฒฐ์ ํด์ ์์ผ์ผ ํ๋ค!
close(conn, pstmt, rs);
}
}
@Override
public Optional<Member> findById(Long id) {
String sql = "select * from member where id = ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
Member member = new Member();
member.setId(rs.getLong("id"));
member.setName(rs.getString("name"));
return Optional.of(member);
} else {
return Optional.empty();
}
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
close(conn, pstmt, rs);
}
}
@Override
public List<Member> findAll() {
String sql = "select * from member";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
List<Member> members = new ArrayList<>();
while(rs.next()) {
Member member = new Member();
member.setId(rs.getLong("id"));
member.setName(rs.getString("name"));
members.add(member);
}
return members;
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
close(conn, pstmt, rs);
}
}
@Override
public Optional<Member> findByName(String name) {
String sql = "select * from member where name = ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
rs = pstmt.executeQuery();
if(rs.next()) {
Member member = new Member();
member.setId(rs.getLong("id"));
member.setName(rs.getString("name"));
return Optional.of(member);
}
return Optional.empty();
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
close(conn, pstmt, rs);
}
}
private Connection getConnection() {
return DataSourceUtils.getConnection(dataSource);
}
//๋ชจ๋ ์ฐ๊ฒฐ ํด์ ํด์ฃผ๋ ๋ฉ์๋
private void close(Connection conn, PreparedStatement pstmt, ResultSet rs){
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
close(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void close(Connection conn) throws SQLException {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
config ์ค์
ํด์ฃผ๊ธฐ
@Configuration
public class SpringConfig {
private final DataSource dataSource;
public SpringConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public MemberService memberService() {
return new MemberService(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
// return new MemoryMemberRepository();
return new JdbcMemberRepository(dataSource);
}
}
DataSource
๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์ ์ ํ๋ํ ๋ ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ค. ์คํ๋ง ๋ถํธ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋กDataSource
๋ฅผ ์์ฑํ๊ณ ์คํ๋ง ๋น์ผ๋ก ๋ง๋ค์ด๋๋ค. ๊ทธ๋์ DI๋ฅผ ๋ฐ์ ์ ์๋ค.
๋ฐ์ํ