공부기록

5월 12일 (2) Spring - JDBC 연동 CRUD(출력, 상세보기)

project100 2023. 5. 12. 20:26

1. Dao에 데이터 목록을 가져오는 메소드 선언

 

주의사항!

Mybatis에서 List는 resultType으로 사용하지 않는다.

resultType에는 목록에 저장되는 하나의 데이터 타입을 작성한다.

예) LIst<String> -> resultType="String"

     LIst<XxxDto> -> resultType="XxxDto"

 

2. dao의 메소드명 눌러서 xml create 

<select id="selectList" resultType="TestDto">
SELECT tcode, tname FROM testtb1
</select>

3. sevice에 메소드 만들기

public ModelAndView getList(){
        log.info("getList()");
        mv = new ModelAndView();
        List<TestDto> tList = tDao.selectList();
        mv.addObject("tList", tList);
        mv.setViewName("list");
        
        return mv; // String 일때만 "" 붙이기
    }

4. controller에 메소드 만들기

@GetMapping("outputData")
    public ModelAndView outputData(){
        log.info("outputData()");
        mv = tServ.getList();
        return mv;
    }

5. view폴더에 list.jsp만들기

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Data 목록</title>
    <style>
        table, th, td {
            border-collapse: collapse;
            border: 1px solid blueviolet;
        }
    </style>
</head>
<body>
<h1>데이터 목록</h1>
<table>
    <thead>
    <tr>
        <th>번호</th>
        <th>이름</th>
    </tr>
    </thead>
    <tbody>
    <%--목록이 없을 경우--%>
    <c:if test="${empty tList}">
        <tr>
            <td colspan="2">데이터가 없습니다.</td>
        </tr>
    </c:if>
    <%--목록이 있을 경우--%>
    <c:if test="${!empty tList}">
    <c:forEach var="item" items="${tList}">
        <tr>
            <td>${item.tcode}</td>
            <td>${item.tname}</td>
        </tr>
    </c:forEach>
    </c:if>
    </tbody>
</table>
</body>
</html>

 

목록에서 이름 누르면 상세 페이지로 갈 수 있도록 

<a href="detail(mapping)?tcode(변수명)=(코드숫자 가지고오기)${item.tcode}">${item.tname}</a>
 
<c:if test="${!empty tList}">
<c:forEach var="item" items="${tList}">
<tr>
<td>${item.tcode}</td>
<td>
<a href="detail?tcode=${item.tcode}">${item.tname}</a>
</td>
</tr>
</c:forEach>
</c:if>
 
원래는 dao - controller - sevice 순으로 만드는 것이 편할 수 있다.

6. controller에서 상세페이지 처리

@GetMapping("detail")
public ModelAndView detail(Integer tcode){
log.info("detail() - tcode:" + tcode);
mv = null;
return mv;
}
7. dao 상세페이지 보기 메소드 선언
TestDto selectData(Integer tcode);
dao.xml
<select id="selectData" resultType="TestDto">
SELECT * FROM testtb1 WHERE tcode=#{tcode}
</select>

8. sevice

public ModelAndView getData(Integer tcode){
        log.info("getData()");
        mv = new ModelAndView();
        TestDto td = tDao.selectData(tcode);
        mv.addObject("data", td);
        mv.setViewName("detail");
        return mv;
    }

9. view폴더에 detail.jsp파일 만들기

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>상세보기</title>
    <style>
        table, th, td {
            border-collapse: collapse;
            border: 1px solid cadetblue;
            text-align: center;
        }
    </style>
</head>
<body>
<h1>데이터 상세 보기</h1>
<table>
    <tr>
        <th>번호</th>
        <td>${data.tcode}</td>
    </tr>
    <tr>
        <th>이름</th>
        <td>${data.tname}</td>
    </tr>
    <tr>
        <th>나이</th>
        <td>${data.tage}</td>
    </tr>
    <tr>
        <th>연락처</th>
        <td>${data.tphone}</td>
    </tr>
</table>
</body>
</html>

+ 연락처 null일 경우

  <tr>
        <th>연락처</th>

        <c:if test="${empty data.tphone}">
            <td>데이터가 없습니다.</td>
        </c:if>
        <c:if test="${!empty data.tphone}">
            <td>${data.tphone}</td>
        </c:if>
    </tr>