<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>spring-boot-starter-data-jpa : JPA/Hibernate ์์กด์ฑ
spring.datasource.username= username
spring.datasource.password=password
spring.datasource.url=jdbc:mysql://localhost:3306/rest_study?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&characterEncoding=UTF-8&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_createion=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
-
spring.jpa.hibernate.ddl-auto : DB ์ด๊ธฐํ ์ ๋ ฅ์ผ๋ก
- none : ์๋์์ฑํ์ง ์๋๋ค.
- update : ๋ณ๊ฒฝ๋ ์คํค๋ง๋ฅผ ์ ์ฉํ๊ณ ์ ๊ฑฐ๋ ํ์ง ์๋๋ค.
- validate : ๋ณ๊ฒฝ๋ ์คํค๋ง๊ฐ ์๋์ง ํ์ธ๋ง ํ๊ณ ๋ณ๊ฒฝ์ด ์๋ค๋ฉด ์ข ๋ฃ
- create : ์์๋ ๋๋ง dropํ๊ณ ๋ค์ ์์ฑ
- create-drop : ์์๊ณผ ์ข ๋ฃ์ ๋ชจ๋ drop
-
spring.jpa.properties.hibernate.format_sql=true
์ฌ์ฉ๋ sql๋ฌธ์ formattingํ์ฌ ๋ณด๊ธฐ ํธํ๊ฒ ๋ง๋ค์ด์ฃผ๋ ์ต์
-
logging.level.org.hibernate.SQL=DEBUG
-
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
์ฌ์ฉ๋ sql๋ฌธ์ ๋ก๊น ํ๊ธฐ ์ํ ์ต์
test์ resoruces/application-test.properties๋ฅผ ๋ง๋ค์ด application.properties์ ์ค๋ณต๋๋ ๋ถ๋ถ์ overrideํ์ฌ ์คํ๋๊ฒ ์ค์
Project Structure -> Modules -> Test Resource Folders์ ์๋ก ๋ง๋ resoruces/application-test.properties๋ฅผ ์ถ๊ฐ
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.hikari.jdbc-url=jdbc:h2:mem:testdb
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
application.properties๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก git์ ์ฌ๋ฆฌ๊ณ ์๋ค๋ฉด db ์ฌ์ฉ์ ์ ๋ณด๊ฐ ์ ์ธ์ด ๋ ์ ์์ผ๋ฏ๋ก ๋ฐ๋ก properties๋ฅผ ๋ง๋ค๊ณ .gitignore๋ก ์ ์ฉํ์.
jdbc.properties
spring.datasource.username=id
spring.datasource.password=pass
spring.datasource.url=jdbc:mysql://localhost:3306/rest_study?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&characterEncoding=UTF-8&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false
ํ์ build์์ ์ด properties๋ ์ ์ฉ์์ผ์ค์ผ db์ ์ฐ๊ฒฐ์ด ๊ฐ๋ฅํ๋ฏ๋ก @PropertySource(value = { "classpath:jdbc.properties" })์ main ๋ฉ์๋์ ๋ถ์ฌ์ฃผ์.