๐Ÿ’ป ์ฝ”๋”ฉ ์ผ๊ธฐ : [Spirng] 'Spring MVC : Controller, Service, Mapper' ํŽธ

ybkยท2024๋…„ 4์›” 29์ผ

spring

๋ชฉ๋ก ๋ณด๊ธฐ
19/55
post-thumbnail

๐Ÿ”” 'Spring MVC : Controller, Service, Mapper'์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž!


๐Ÿ’Ÿ Spring MVC : Controller, Service, Mapper

  1. @Controller:
    • @Controller ์• ๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง MVC์—์„œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ํ•ด๋‹น ์š”์ฒญ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ธ์— ๋‹ด์Šต๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค ์œ„์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ DTO๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๊ณ , ์‘๋‹ต์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  1. @Service:
    • @Service ์• ๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ์„œ๋น„์Šค ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์„œ๋น„์Šค ํด๋ž˜์Šค๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ๊ฐ€๊ณต ๋ฐ ์ฒ˜๋ฆฌ, ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๋“ฑ์˜ ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ ์ปจํŠธ๋กค๋Ÿฌ๋‚˜ ๋‹ค๋ฅธ ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ๋˜์–ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • DTO๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  1. @Mapper:
    • @Mapper ์• ๋…ธํ…Œ์ด์…˜์€ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค(MyBatis) ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋งคํ•‘๋˜๋Š” ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค๋Š” SQL ์ฟผ๋ฆฌ๋ฅผ ์ •์˜ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ DAO(Data Access Object) ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, MyBatis์—์„œ๋Š” ์ด ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ XML ํŒŒ์ผ๊ณผ ์—ฐ๊ฒฐํ•˜์—ฌ SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • @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);
}
  • ๊ฐ์ฒด(dto)๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด DTO๋Š” ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ from, 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);
    }
}
  • ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ year์™€ month๋ฅผ ๊ฐ€๊ณตํ•˜์—ฌ ํ•ด๋‹น ์›”์˜ ์‹œ์ž‘์ผ๊ณผ ์ข…๋ฃŒ์ผ์„ ๋ฌธ์ž์—ด๋กœ ์ƒ์„ฑํ•˜๊ณ , Mapper ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

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);
    }

}
  • service์—์„œ ๋ฐ›์€ 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>

profile
๊ฐœ๋ฐœ์ž ์ค€๋น„์ƒ~

0๊ฐœ์˜ ๋Œ“๊ธ€