이 예시는 바로 복붙해서 실행해볼 수 있는 수준으로 구성돼 있습니다.
🏗️ 프로젝트 구조 (기본)
src/
└─ main/
├─ java/
│ └─ com.example.demo/
│ ├─ entity/
│ │ └─ Employee.java
│ ├─ repository/
│ │ └─ EmployeeRepository.java
│ ├─ service/
│ │ └─ EmployeeService.java
│ ├─ controller/
│ │ └─ EmployeeController.java
│ └─ DemoApplication.java
└─ resources/
├─ application.yml
└─ data.sql (테스트 데이터)
🧩 1️⃣ Employee.java (Entity)
package com.example.demo.entity; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private int age; private int salary; private String name; public Employee() {} public Employee(String name, int age, int salary) { this.name = name; this.age = age; this.salary = salary; } // Getters / Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
🧩 2️⃣ EmployeeRepository.java
package com.example.demo.repository; import com.example.demo.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface EmployeeRepository extends JpaRepository<Employee, Long> { // 두 필드 모두 크거나 같은 조건으로 조회 List<Employee> findByAgeGreaterThanEqualAndSalaryGreaterThanEqual(int age, int salary); }
🧩 3️⃣ EmployeeService.java (선택적 – 비즈니스 로직 계층)
package com.example.demo.service; import com.example.demo.entity.Employee; import com.example.demo.repository.EmployeeRepository; import org.springframework.stereotype.Service; import java.util.List; @Service public class EmployeeService { private final EmployeeRepository employeeRepository; public EmployeeService(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } public List<Employee> getEmployeesByAgeAndSalary(int age, int salary) { return employeeRepository.findByAgeGreaterThanEqualAndSalaryGreaterThanEqual(age, salary); } }
🧩 4️⃣ EmployeeController.java
package com.example.demo.controller; import com.example.demo.entity.Employee; import com.example.demo.service.EmployeeService; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/employees") public class EmployeeController { private final EmployeeService employeeService; public EmployeeController(EmployeeService employeeService) { this.employeeService = employeeService; } // GET /api/employees/filter?age=30&salary=5000 @GetMapping("/filter") public List<Employee> getEmployeesByFilter( @RequestParam int age, @RequestParam int salary ) { return employeeService.getEmployeesByAgeAndSalary(age, salary); } }
🧩 5️⃣ DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
🧩 6️⃣ application.yml (DB 설정)
테스트용 H2 인메모리 DB를 사용합니다 👇
spring: datasource: url: jdbc:h2:mem:testdb driver-class-name: org.h2.Driver username: sa password: jpa: hibernate: ddl-auto: create show-sql: true h2: console: enabled: true
🧩 7️⃣ data.sql (초기 테스트 데이터)
INSERT INTO employee (name, age, salary) VALUES ('Alice', 25, 4000); INSERT INTO employee (name, age, salary) VALUES ('Bob', 30, 5000); INSERT INTO employee (name, age, salary) VALUES ('Charlie', 35, 6000); INSERT INTO employee (name, age, salary) VALUES ('David', 40, 7000);
🚀 실행 및 테스트
서버를 실행한 뒤 브라우저 또는 Postman에서 아래 URL을 호출해보세요 👇
GET http://localhost:8080/api/employees/filter?age=30&salary=5000
🔹 결과 (JSON 응답 예시)
[ { "id": 2, "age": 30, "salary": 5000, "name": "Bob" }, { "id": 3, "age": 35, "salary": 6000, "name": "Charlie" }, { "id": 4, "age": 40, "salary": 7000, "name": "David" } ]
✅ 정리
구성 요소역할
| Entity | DB 테이블 매핑 (Employee) |
| Repository | JPA 기반 CRUD + 조건 조회 |
| Service | 비즈니스 로직 계층 (선택적) |
| Controller | REST API 엔드포인트 |
| application.yml | H2 DB 설정 |
| data.sql | 초기 데이터 |
'Springboot' 카테고리의 다른 글
| Spring Data JPA의 @Query + DTO 생성자 방식 (0) | 2025.11.04 |
|---|---|
| JPA 메소드명 맵핑 방식 (0) | 2025.11.04 |
| JPA+ Hibernate 데이터 조회 방법 (0) | 2025.11.04 |
| EntityManager (0) | 2025.11.04 |