You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@BeanpublicMemberRepositorymemberRepository() {
// return new MemoryMemberRepository();// return new JdbcMemberRepository(dataSource);returnnewJdbcTemplateMemberRepository(dataSource);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
H2 데이터베이스 설치
cd h2/bin마지막 단계에서 계속
driver org.h2.driver is not suitable for jdbc:h2:tcp://localhost/~/test 08001/0오류가 발생했는데 다시 설치해도 되지 않아 재부팅 해주니 오류 해결.. ;; 안 되면 재부팅을 해보자!접속이 되면 H2 데이터베이스에 접속해서 Member 테이블을 생성해준다.
순수 JDBC
DB 연결 설정을 해주면 스프링부트가
DataSource를 주입받아 사용할 수 있다.연결을 해서 쿼리문을 날리고 실행해준다. 결과에서 Key값을 가져올 수 있다.
조회 시
pstmt.executeQuery()DataSourceUtils로 연결을 닫아줘야 트랜잭션에 문제가 생기지 않는다.SpringConfig.java설정 파일만 변경해줘도 다른 코드는 변경할 필요 없이 간단하게 구현 클래스를 변경해줄 수 있다. (개방-폐쇄 원칙 (OCP))스프링 통합 테스트
테스트는 다른 곳에서 쓰는 게 아니기 때문에 편하게 필드 주입해도 상관 없다.
@SpringBootTest스프링 컨테이너와 테스트를 함께 실행 (진짜 스프링을 띄워서 테스트 하는 것)@Transactional테스트를 실행할 때 트랜잭션을 시작하고, 테스트가 끝나면 롤백을 해준다. 따라서 실제 DB에 테스트 데이터가 반영되지 않아 다음 테스트에 영향을 미치지 않는다.💡 tip 통합 테스트보다는 순수한 단위 테스트가 좋은 테스트일 확률이 높다.
스프링 JDBC Template
생성자가 하나일 때는
@Autowired생략 가능역시 설정 파일만 바꿔주면 구현 클래스 변경 가능!
JPA
JPA를 활용하면 SQL과 데이터 중심의 설계에서 객체 중심의 설계로 전환할 수 있다.resources/application.properties파일에 설정 추가EntityManager를 주입 받아야 한다.@Transactional어노테이션을 사용한다.스프링 데이터 JPA
JPA를 알아야스프링 데이터 JPA를 잘 활용할 수 있다.JpaRepository를 상속하면 구현체를 알아서 만들어준다.save(),findAll()등 기본적인 CRUD 메서드들이 제공되기 때문에 따로 만들지 않고 가져다 쓰면 된다.Querydsl을 활용하거나 Native 쿼리를 생성할 수도 있고MyBatis와 같은 다른 라이브러리와도 함께 사용 가능하다.Beta Was this translation helpful? Give feedback.
All reactions