application.yml
spring:
datasource:
url: jdbc:postgresql://[host]:[port]/[database]
username: [username]
password: [password]
build.gradle
// PostgreSQL
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.27'
// Spring Data JPA
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
연동 확인
package com.woogiemon.board.runner;
import java.sql.Connection;
import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class PostgreRunner implements ApplicationRunner {
@Autowired
DataSource dataSource;
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(ApplicationArguments args) {
try {
Connection con = dataSource.getConnection();
Statement statement = con.createStatement();
String sql = "CREATE TABLE POSTS(ID INTEGER NOT NULL, NAME VARCHAR(255), PRIMARY KEY(ID))";
statement.execute(sql);
jdbcTemplate.execute("INSERT INTO POSTS VALUES (1, 'test')");
} catch (Exception e) {
e.printStackTrace();
}
}
}