Skip to content

Latest commit

ย 

History

History
77 lines (55 loc) ยท 2.86 KB

File metadata and controls

77 lines (55 loc) ยท 2.86 KB

MySQL ์—ฐ๊ฒฐ

dependency ์ถ”๊ฐ€

<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 ์˜์กด์„ฑ

properties ์ถ”๊ฐ€

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๋Š” H2 ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •

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

DB ์‚ฌ์šฉ์ž ์ •๋ณด

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 ๋ฉ”์„œ๋“œ์— ๋ถ™์—ฌ์ฃผ์ž.