TIL_220727_강의용 게시판 프로젝트 3

창고·2022년 7월 28일
0

들어가기에 앞서
실제 프로젝트 진행했던 코드 내용은 업로드하지 않았으며
그 외 세부 내용은 일부만 업로드하였습니다.

8. 데이터베이스 접근 로직 테스트 정의

(1) 데이터베이스 접근 가능 상태로 세팅

  • 사용 DB : MySQL
  • MySQL Community Server 설치 (이미 설치했음)
  • Intellij 우측 Database > + > Datasource > MySQL > test connection > apply > ok
  • SQL 실행 시 ctrl + enter = 실행 단축키 선택 (해당 쿼리문, 해당 지점까지 전체 등)
  • SQL 입력하여 유저 생성 및 권한 부여 (모든 권한을 부여)
    • <''> 과 <``>구분할 것!!
create user 'mrcocoball'@'localhost' identified by '비밀번호';
use mysql;
select `user` from `mysql`.`user`;
show grants for 'mrcocoball'@'localhost';
grant all on `board`.* to 'mrcocoball'@'localhost' with grant option;

flush privileges;
  • dependency 추가 Spring Data JPA, MySQL Driver, H2 Database
  • spring initializr 에서 추가한 후 explore, dependency 추가 후 gradle 새로고침
  • 좌측에 추가된 JPA Structure : JPA Buddy에 의해 설치된 것
  • resources > application.properties에서 JPA를 위한 설정 변경
    • application.properties를 yml 파일로 refactoring > application.yml
    • 설정 추가
debug: false # debug 로그 활성화 여부
management.endpoints.web.exposure.include: "*" # actuator의 endpoints에 감춰진 것을 모두 활성화, 검색 필요

logging: # 로그 관련
  level: # 로그 레벨 설정
    com.fastcampus.projectboard: debug # 여기에 발생하는 모든 로그를 debug 레벨로
    org.springframework.web.servlet: debug # 여기에 발생하는 모든 로그를 debug 레벨로, 위의 debug를 false하고 이 부분만 확인
    org.hibernate.type.descriptor.sql.BasicBinder: trace # JPA 쿼리 로그를 관찰

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/board # DB 접근 url
    username: mrcocoball
    password: 비밀번호
    driver-class-name: com.mysql.cj.jdbc.Driver # 외우지 않아도 자동완성됨! 사용 가능 Driver만 표시됨
  jpa: # 차후 JPA 관련 강의 다시 확인해보기!
    defer-datasource-initialization: true # 테스트용 DB 데이터를 생성할 수 있게 함
    hibernate.ddl-auto: create # Entity, JPA 설정들을 통해 테이블 데이터를 유추, 자동으로 DDL문을 만듬 (테스트 시 자동화)
    # open-in-view: false
    show-sql: true
    properties: # JPA에서 부가로 사용하고 싶은 properties 넣음
      hibernate.format_sql: true # 한줄로 나오는 쿼리문을 포맷팅해서 출력, 실무에서 많이 사용
      hibernate.default_batch_fetch_size: 100
  h2.console.enabled: false
  sql.init.mode: always
  # thymeleaf.cache: false
  # data.rest:
    # bast-path: /api
    # detection-strategy: annotated

---
# --- 세 줄을 기준으로 여러 개의 Document 생성 가능
# Test DB용 설정

spring:
  config.activate.on-profile: testdb
#  datasource:
#    url: jdbc:h2:mem:board;mode=mysql
#    driverClassName: org.h2.Driver
#  sql.init.mode: always
#  test.database.replace: none
  • 테스트 DB에 넣을 데이터 초기화를 위해 resources > new file > data.sql
    • Generic SQL > MySQL로 변경
profile
공부했던 내용들을 모아둔 창고입니다.

0개의 댓글