
@Controller:@Controller ์ ๋
ธํ
์ด์
์ ์คํ๋ง MVC์์ ์น ์ ํ๋ฆฌ์ผ์ด์
์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.@Service:@Service ์ ๋
ธํ
์ด์
์ ์คํ๋ง์์ ๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๋ ์๋น์ค ํด๋์ค๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.@Mapper:@Mapper ์ ๋
ธํ
์ด์
์ ๋ง์ด๋ฐํฐ์ค(MyBatis) ํ๋ ์์ํฌ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋งคํ๋๋ ๋งคํผ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
@Controller๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์๋ต์ ๋ฐํํ๋ ์ญํ@Service๋ ๋น์ฆ๋์ค ๋ก์ง์ ์ฒ๋ฆฌํ๊ณ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ์ญํ@Mapper๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ํธ ์์ฉ์ ๋ด๋นํ๋ Mapper ์ธํฐํ์ด์ค ์ ์DTO: ์๋ฐ๋น์ด๋ฉฐ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ์บก์ํํ๋๋ฐ ์ฌ์ฉํฉ๋๋ค.
Mapper05.java
@Mapper
public interface Mapper05 {
@Data
static class CustomerIncome{
private String customerName;
private String income;
}
@Select("""
SELECT CustomerName, SUM(od.Quantity * p.Price) AS income
FROM Customers c JOIN Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od ON o.OrderID = od.OrderID
JOIN Products p ON p.ProductID = od.ProductID
WHERE o.OrderDate BETWEEN #{from} AND #{to}
GROUP BY c.CustomerID
ORDER BY income desc
""")
List<CustomerIncome> selectCustomerIncomeList(String from, String to);
}
List<CustomerIncome>์ ๋ด์ ๋ฐํํฉ๋๋ค.Service.java
@Service
@RequiredArgsConstructor
public class Service01 {
private final Mapper05 mapper05;
public List<Mapper05.CustomerIncome> customerIncomeList(Integer year, Integer month){
String from = "%d-%02d-01".formatted(year, month);
String to = "%d-%02d-31".formatted(year, month);
return mapper05.selectCustomerIncomeList(from, to);
}
}
Controller.java
@Controller
@RequestMapping("main34")
@RequiredArgsConstructor
public class Controller34 {
private final Service01 service;
// ํด๋น์์ ๊ณ ๊ฐ๋ณ ๊ตฌ๋งค๊ธ์ก ์กฐํ(๊ตฌ๋งค๊ธ์ก์ด ๋์ ์)
// /main34/sub2?year=1996&month=7
@GetMapping("sub2")
public void method3(Integer year, Integer month, Model model){
List<Mapper05.CustomerIncome> customerIncomeList = service.customerIncomeList(year, month);
model.addAttribute("incomeList", customerIncomeList);
model.addAttribute("year", year);
model.addAttribute("month", month);
}
}
customerIncomeList๋ฅผ model(customerIncomeList)์ ์ ์ฅํ๊ณ ์ด๋ฅผ view๋ก ํฌ์๋ฉํฉ๋๋ค.sub2.jsp(view)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<html>
<head>
<title>Title</title>
<style>
table, th, td, th {
border-collapse: collapse;
border: 1px solid black;
}
</style>
</head>
<body>
<h3>๊ณ ๊ฐ ๊ตฌ๋งค๊ธ์ก ์กฐํ</h3>
<form>
<div>
๋
๋
<select name="year">
<option value="1996" ${year == 1996 ? selected : ''}>1996</option>
<option value="1997" ${year == 1997 ? selected : ''}>1997</option>
</select>
์
<select name="month">
<option value="1" ${month == 1 ? selected : ''}>1</option>
<option value="2" ${month == 2 ? selected : ''}>2</option>
<option value="3" ${month == 3 ? selected : ''}>3</option>
<option value="4" ${month == 4 ? selected : ''}>4</option>
<option value="5" ${month == 5 ? selected : ''}>5</option>
<option value="6" ${month == 6 ? selected : ''}>6</option>
<option value="7" ${month == 7 ? selected : ''}>7</option>
<option value="8" ${month == 8 ? selected : ''}>8</option>
<option value="9" ${month == 9 ? selected : ''}>9</option>
<option value="10" ${month == 10 ? selected : ''}>10</option>
<option value="11" ${month == 11 ? selected : ''}>11</option>
<option value="12" ${month == 12 ? selected : ''}>12</option>
</select>
<input type="submit" value="์กฐํ">
</div>
</form>
<hr>
<c:if test="${empty incomeList}">
<div>
์กฐํ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.
</div>
<div>
1996๋
7์๋ถํฐ 1997๋
11์ ์ค์ ์กฐํ ํด์ฃผ์ธ์.
</div>
</c:if>
<c:if test="${not empty incomeList}">
<h3>${year}๋
${month}์ ์กฐํ ๊ฒฐ๊ณผ</h3>
<table>
<thead>
<tr>
<th>CustomerName</th>
<th>income</th>
</tr>
</thead>
<tbody>
<c:forEach items="${incomeList}" var="incomeL">
<tr>
<td>${incomeL.customerName}</td>
<td>${incomeL.income}</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</body>
</html>
