처음 실행 시 오류 - JPA, DB 연결

김민영·2023년 1월 25일
0

Spring

목록 보기
13/15

참고
https://velog.io/@alswl689/MySQL-JPA-SpringBoot-%EC%97%B0%EB%8F%99-%EB%B0%8F-%ED%85%8C%EC%8A%A4%ED%8A%B8Gradle

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

종료 코드 1(으)로 완료된 프로세스

  • JPA 를 사용하려면 기본적으로 DB와 연결이 되어있어야 함.
  • 연결 설정을 안했기 때문에 발생한 에러

1. 프로젝트 의존성 추가

  • spring.io.starter에서 JPA와 Mysql 의존성을 추가했기 때문에 이미 들어있었다.
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'
	runtimeOnly 'com.mysql:mysql-connector-j'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

2. application.properties에 DB 정보 추가

  • application.properties에 DB 관련 설정을 한다.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/board
spring.datasource.username=root
spring.datasource.password=qwer5124*
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true

// MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
// DB Source URL. board는 스키마의 이름이다.
spring.datasource.url=jdbc:mysql://localhost:3306/board
// DB username, password
spring.datasource.username=root
spring.datasource.password=123456
// 처리시 발생하는 SQL을 보여줄 것인지 결정
spring.jpa.show-sql=true
// 프로젝트 실생시 자동으로 DDL(create, alter, drop)을 생성할 지 결정하는 설정
// create : 매번 테이블 생성 새로 시도,
// update : 변경 필요한 경우 alter로 변경, 테이블이 없는 경우 create
spring.jpa.hibernate.ddl-auto=update
// 실제 JPA 구현체인 Hibernate가 동작하면서 발생하는 SQL을 포맷팅해서 출력 -> 실행되는 SQL 가독성 높임
spring.jpa.properties.hibernate.format_sql=true

  • 스키마
  • 좌측 상단에서 4번째 아이콘 원통모양을 클릭해서 스키마를 만들 수 있다.
  • 여기까지 진행했고, 빌드가 되는 것을 확인했다.

  • Controller package에 Controller 메소드를 만들고, 실행했다.

profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글