Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
package hello.hellospring; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import hello.hellospring.repository.JdbcMemberRepository; | ||
import hello.hellospring.repository.MemberReository; | ||
import hello.hellospring.repository.MemoryMemberRepository; | ||
import hello.hellospring.service.MemberService; | ||
|
||
@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 MemberReository memberRepository() { | ||
return new MemoryMemberRepository(); | ||
// return new MemoryMemberRepository(); | ||
return new JdbcMemberRepository(dataSource); | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
src/main/java/hello/hellospring/repository/JdbcMemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package hello.hellospring.repository; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
import hello.hellospring.domain.Member; | ||
|
||
public class JdbcMemberRepository implements MemberReository { | ||
private final DataSource dataSource; | ||
public JdbcMemberRepository(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
@Override | ||
public Member save(Member member) { | ||
String sql = "insert into member(name) values(?)"; | ||
Connection conn = null; | ||
PreparedStatement pstmt = null; | ||
ResultSet rs = null; | ||
try { | ||
conn = getConnection(); | ||
pstmt = conn.prepareStatement(sql, | ||
Statement.RETURN_GENERATED_KEYS); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
spring.output.ansi.enabled=always | ||
spring.datasource.url=jdbc:h2:tcp://localhost/~/test | ||
spring.datasource.driver-class-name=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password= |