hanker

Spring boot - application.properties MSSQL DB 설정 본문

SPRING

Spring boot - application.properties MSSQL DB 설정

hanker 2024. 9. 29. 16:39

이전에 MYSQL 에 대한 설정 방법에 대해서 글을 적었는데, 이번 글에서는 MSSQL에 대해서 적으려고 한다.

 

방법은 비슷하지만 다들 헷갈려 하기에 DB별로 설정방법들을 기회가 되면 다 적을 예정이다.

 

우선 pom.xml에 의존성 추가를 해주자

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>8.4.1.jre8</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

위 mssql-jdbc 버전은 각 자바 버전에 맞게 설정해야한다.

추가 후 maven 업데이트를 하고 Spring boot 실행을 하면 실행이 안되는데, 해당 오류는 DB 설정을 찾을 수 없어서 그렇다.

***************************
APPLICATION FAILED TO START
***************************

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).

Disconnected from the target VM, address: '127.0.0.1:57238', transport: 'socket'

Process finished with exit code 0

application.properties나 yml 파일에 정보추가를 해줘야 한다.

spring.datasource.url 에는 IP : PORT를 적어주면 되는데, 기본 PORT는 1433이고 포트가 다르면 변경해서 적어준다.

spring.datasource.username / password 에는 Id, Password를 적어준다.

spring.datasource.driver-class-name 에는 mssql 드라이버 클래스 경로를 적어주는데, 해당 pom.xml에 mssql 의존성을 추가하면 Class가 생성된다.

(만약 작성했는데, 빨간색이 뜬다거나 클래스가 생성되지 않으면 [ File] - [Invalidate Caches] 재실행하면 된다)

spring.datasource.database-platform 은 설정하지 않으면 자동으로 감지해서 설정해주지만 권장되는 방법은 설정을 해주는게 권장된다.

 

 

 

아주 잘 실행된다.

 

 

 

끝.