1. Maven MySQL 연동
MySQL dependency 추가하기
2. 검색창에 mysql 검색
3. MySQL Connector/J 클릭
4. 현재 MySQL 버전 클릭
5. Maven 선택 > 코드 복사
6. pom.xml에 붙여넣기
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jpa-basic</groupId>
<artifactId>ex1-hello-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JPA 하이버네이트 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.15.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<!-- MYSQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</project>
persistence.xml
프로젝트 > src > main > resources 에 아래 이름의 package 생성
META-INF
해당 package에 persistence.xml 생성
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="hello">
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="유저이름"/>
<property name="javax.persistence.jdbc.password" value="유저 패스워드"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpaDB"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
<!-- 옵션 -->
<property name="hibernate.show_sql" value="true"/> <!-- 실행창에 실제 날리는 DB 쿼리를 출력해줌 -->
<property name="hibernate.format_sql" value="true"/> <!-- 쿼리가 출력되는 형태를 이쁘게 잡아줌 -->
<property name="hibernate.use_sql_comments" value="true"/> <!-- 주석을 통해서 무슨 쿼리가 나온건지 알려줌 -->
<property name="hibernate.jdbc.batch_size" value="10"/> <!-- batch size만큼 모아서 한 방에 네트워크로 쿼리 전송하고 DB 커밋 (버퍼링 기능) -->
<!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
</properties>
</persistence-unit>
</persistence>
2. Gradle MySQL 연동
build.gradle 에 의존관계 추가
runtimeOnly 'com.mysql:mysql-connector-j'
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'study'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/[DB이름]?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
username: [username]
password: [userpassword]
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
database: mysql
hibernate:
ddl-auto: update
'Spring Boot' 카테고리의 다른 글
[Spring Boot] 로그인 기능 구현 (0) - 공통 기능, 코드 구현 (2) | 2024.01.07 |
---|---|
[Spring Boot] 페이징 기능 구현 ( + 페이징, 정렬, 검색, 에러 메시지 포함 예제) (1) | 2024.01.03 |
[Spring Boot] 스프링 구동 시에 특정 코드 자동 실행시키기 (Command Line Runner, Application Runner) (1) | 2023.11.22 |
[Spring Boot] Gradle 빌드해서 Jar 파일 생성, 실행 (0) | 2023.11.22 |
[Spring Boot] Port 8080 was already in use. 에러 해결 (2) | 2023.11.22 |