개념정리
실전 배당금 프로젝트 개념 (초기단계)
밍쎄
2023. 8. 27. 21:21
단순하게 가닥을 잡아본 것.
1. 엔티티 클래스 생성
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;
@Entity
public class Dividend {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String companyName;
private LocalDate dividendDate;
private double amount;
// getters and setters
}
- 배당금 정보를 표현하는 엔티티 클래스입니다.
- 회사명, 배당일, 배당액 등의 필드를 가집니다.
2. Repository 생성
import org.springframework.data.jpa.repository.JpaRepository;
public interface DividendRepository extends JpaRepository<Dividend, Long> {
// 추가적인 쿼리 메소드 정의 가능
}
- JPA의 JpaRepository를 확장한 인터페이스입니다.
- 데이터베이스와 상호작용하는 메소드를 정의합니다.
3. 서비스 계층 구현
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DividendService {
private final DividendRepository dividendRepository;
@Autowired
public DividendService(DividendRepository dividendRepository) {
this.dividendRepository = dividendRepository;
}
public List<Dividend> getAllDividends() {
return dividendRepository.findAll();
}
public void saveDividend(Dividend dividend) {
dividendRepository.save(dividend);
}
}
- 컨트롤러와 리포지토리 사이의 중간 계층입니다.
- 배당금 조회 및 저장을 수행하는 메소드를 정의합니다.
4. 컨트롤러 생성
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class DividendController {
private final DividendService dividendService;
@Autowired
public DividendController(DividendService dividendService) {
this.dividendService = dividendService;
}
@GetMapping("/dividends")
public String listDividends(Model model) {
List<Dividend> dividends = dividendService.getAllDividends();
model.addAttribute("dividends", dividends);
return "dividend/list";
}
@GetMapping("/dividends/new")
public String newDividendForm(Model model) {
model.addAttribute("dividend", new Dividend());
return "dividend/new";
}
@PostMapping("/dividends/new")
public String saveNewDividend(Dividend dividend) {
dividendService.saveDividend(dividend);
return "redirect:/dividends";
}
}
- 클라이언트의 요청을 처리하는 컨트롤러입니다.
- 배당금 목록 조회와 신규 배당금 추가를 처리하는 메소드를 정의합니다.
5. Thymeleaf 템플릿 작성 (list.html, new.html)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Dividend List</title>
</head>
<body>
<h1>Dividend List</h1>
<table>
<tr>
<th>Company Name</th>
<th>Dividend Date</th>
<th>Amount</th>
</tr>
<tr th:each="dividend : ${dividends}">
<td th:text="${dividend.companyName}"></td>
<td th:text="${dividend.dividendDate}"></td>
<td th:text="${dividend.amount}"></td>
</tr>
</table>
<a href="/dividends/new">Add New Dividend</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>New Dividend</title>
</head>
<body>
<h1>New Dividend</h1>
<form method="post" th:object="${dividend}" th:action="@{/dividends/new}">
<label for="companyName">Company Name:</label>
<input type="text" id="companyName" th:field="*{companyName}"/><br/>
<label for="dividendDate">Dividend Date:</label>
<input type="date" id="dividendDate" th:field="*{dividendDate}"/><br/>
<label for="amount">Amount:</label>
<input type="number" id="amount" th:field="*{amount}"/><br/>
<button type="submit">Save</button>
</form>
<a href="/dividends">Back to List</a>
</body>
</html>
- 웹 페이지 템플릿입니다.
- 배당금 목록과 신규 배당금 추가 폼을 보여줍니다.
- Thymeleaf 문법을 사용하여 데이터를 동적으로 표시합니다.
728x90