◆SQL/H2 DataBase
[H2]💥 H2 Database란 / 설정 / 테스트 실행
쿠키린
2024. 7. 23. 13:53
메모리모드에서 동작하는 게 있기 때문에 따로 DB를 안 띄우고도
여러가지 테스트를 해볼 수 있는 장점이 있다.
H2 설치
https://www.h2database.com/html/main.html
H2 Database Engine
H2 Database Engine Welcome to H2, the Java SQL database. The main features of H2 are: Very fast, open source, JDBC API Embedded and server modes; in-memory databases Browser based Console application Small footprint: around 2.5 MB jar file size Supp
www.h2database.com
실행 - H2 Console 더블클릭

순서1) 설정 - 파일모드로 db를 첫 실행해야함.

연결 클릭
순서2) TCP 모드로 접근 변경하기

순서3) application.yml - 프로젝트 DB 연결 설정
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/querydsl
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
#깔끔하게 테이블 재생성
ddl-auto: create
properties:
hibernate:
format_sql: true
show_sql: true #쿼리 예쁘게 보여짐
logging.level:
org.hibernate.SQL: debug
- application.properties 파일을 지우고 application.yml 파일 생성
테스트
@SpringBootTest
@Transactional
@Commit
class QuerydslApplicationTests {
@Autowired
EntityManager em;
@Test
void contextLoads() {
Hello hello = new Hello();
em.persist(hello);
JPAQueryFactory query = new JPAQueryFactory(em);
QHello qHello = new QHello("h"); // h는 그냥 넣어준 변수명
Hello result = query.selectFrom(qHello).fetchOne();
assertThat(result).isEqualTo(hello);
assertThat(result.getId()).isEqualTo(hello.getId());
}
}
