Programming/Spring boot

[DB] Spring Boot 프로젝트에 DB 연동하기(MySQL)

녱녱 2023. 3. 17.

추가할 dependency

dependencies {
	...

    //DB
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.mysql:mysql-connector-j'
}

[방법 1] application.yml에 DB Property 값 설정해주기

spring:
  datasource:
    url: jdbc:mysql://{host}:{port}/{database-name}?useSSL=false&serverTimezone=UTC
    username: {username}
    password: {password}

반드시 {} 빼주기!

[방법 2] application.properties에서 설정하기

# MySQL connection properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Hibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

DB 사용 안 할 경우

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

 

'Programming > Spring boot' 카테고리의 다른 글

Entity와 관련 annotation  (0) 2023.03.27
Controller와 관련 annotation  (0) 2023.03.22

댓글