들어가기에 앞서..
책은 H2 데이터베이스를 예제로 다루고 있으나, 이 글에서는 Mysql을 사용하였습니다.
[사전준비] MAC에 Mysql 설치
터미널을 통해 설치 한다.
> brew update
> brew install mysql
> mysql --version
mysql Ver 8.3.0 for macos13.6 on arm64 (Homebrew)
> mysql.server start
Starting MySQL
SUCCESS!
> mysql_secure_installation
초기설정 작업 수행
> mysql -u root -p
Enter password: [비밀번호 입력]
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 12
Server version: 8.3.0 Homebrew
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
mysql>
예제를 위해 데이터베이스(이름 “shop”)을 하나 생성한다.
mysql> create database shop;
1) 데이터베이스 연동 설정
dependency 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
- spring-boot-starter-data-jpa
- JPA를 사용할 수 있게 해주는 스프링 부트 데이터 JPA
- mysql-connector-j
- mysql 연결을 위한 connector
- scope를 runtime 으로 지정해서 컴파일 타임에는 사용되지 않고 어플리케이션 런타임에만 사용되도록 설정한다.
Mysql 데이터베이스 설정
application.yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:{포트(port)}/{데이터베이스명}?useSSL=false&serverTimezone=Asia/Seoul&characterEncoding=UTF-8
username: root
password: {비밀번호}
JPA 설정
application.yaml
spring:
jpa:
hibernate:
ddl-auto: create # DB 초기화 전략으로 기존 데이블 삭제 후 새로 생성한 한다.
properties:
hibernate:
show_sql: true # 만들어진 sql을 보여준다.
format_sql: true # sql을 좀 더 가독성있게 보여준다.
database-platform: org.hibernate.dialect.MySQLDialect
책
참고
'실전 스프링부트' 카테고리의 다른 글
[ch03-5] @Query로 쿼리문 지정 (0) | 2024.03.12 |
---|---|
[ch03-4] 스프링 데이터를 사용한 데이터 조회 (0) | 2024.03.11 |
[ch03-3] CrudRepository 인터페이스 이해 (0) | 2024.03.11 |
[ch03-1] 스프링 데이터 소개 (0) | 2024.03.11 |
[ch02] 빈 벨리데이션으로 사용자 입력 데이터 유효성 검증 (0) | 2024.03.11 |