오늘부터 내가 다시 spring으로 이전하고 있는 프로젝트의 진행을 만들어보려고 한다.
## 오늘은 스프링 어플리케이션을 postgres와 연결하는 걸 만들어보겠다
우선 도커가 설치돼있어야한다.
docker run -d \
--name my-postgres \
-e POSTGRES_USER=your_username \
-e POSTGRES_PASSWORD=your_password \
-e POSTGRES_DB=your_database_name \
-p 5432:5432 \
postgres:latest
-> 포스트 그레스 설치
이제 application.properties나 application.yaml에 db 연결 정보를 넣어주면 된다.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/db_name
username: your_username
password: your_password
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
나처럼 dev 와 product를 나눴다면
spring:
profiles:
active: dev
테스트를 위해 간단한
코드를 만들어보았다.
package com.algorithm.mate.dbTEST.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/test")
public class DbTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping
public String testConnection() {
String result = jdbcTemplate.queryForObject("SELECT version()", String.class);
return "Connected to: " + result;
}
}
이제
서버를 실행하고
localhost:8080/api/test 를 실행하면
'AlgoMate' 카테고리의 다른 글
Celery + Redis vs Celery + RabbitMQ: 어떤 선택이 더 나을까? (0) | 2025.02.17 |
---|---|
🚀 동적으로 파일을 제공하는 방법 (2) | 2025.02.16 |
Spring Boot 단위 테스트: @Mock 과 @InjectionMocks의 원리와 활용 (2) | 2025.02.14 |
AlgoMate#3 - docker 로 띄운 DB 에 접근하기(PostgreSQL) (2) | 2025.02.01 |
AlgoMate#2 - Jplag를 통한 유사도 검사 (2) | 2025.02.01 |