IT/development

[mybatis] map을 list로 받아서 화면에 그리기

알 수 없는 사용자 2022. 11. 19. 05:58
반응형

목차

    미래의 내가 보기 위해 기록함

    테스트 테이블 😃

    CREATE TABLE `tmp_board`(
                              `board_seq` bigint auto_increment,
                              `title` varchar (30),
                              `contents` varchar (30),
                              `name` varchar (30),
                              `reg_date` timestamp,
                              `update_date` timestamp,
                              primary key(board_seq)
    );

     

    mapper xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="study.tmp11.mapper.BoardMapper">
        <resultMap id="boardMap" type="map">
            <id column="board_seq" property="boardSeq"/>
            <result column="title" property="title"/>
            <result column="contents" property="contents"/>
            <result column="name" property="name"/>
            <result column="reg_date" property="regDate"/>
            <result column="update_date" property="updateDate"/>
        </resultMap>
    
        <select id="findListMap" resultMap="boardMap">
            SELECT * FROM t_board
            ORDER BY reg_date DESC
        </select>
    </mapper>

     

    mapper interface 😊

    package study.tmp11.mapper;
    
    import org.apache.ibatis.annotations.Mapper;
    import study.tmp11.domain.BoardVO;
    
    import java.util.List;
    import java.util.Map;
    
    @Mapper
    public interface BoardMapper {
        List<Map<String, Object>> findListMap();
    }

     

    service 😁

    package study.tmp11.service;
    
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    import study.tmp11.domain.BoardVO;
    import study.tmp11.mapper.BoardMapper;
    
    import java.util.List;
    import java.util.Map;
    
    @Service
    @Slf4j
    @RequiredArgsConstructor
    public class BoardService {
    
        private final BoardMapper boardMapper;
    
        public List<Map<String, Object>> findListMap() {
            return boardMapper.findListMap();
        }
    }

     

    controller 😎

    package study.tmp11.controller;
    
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import study.tmp11.domain.BoardVO;
    import study.tmp11.service.BoardService;
    
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @RequiredArgsConstructor
    @Slf4j
    public class BoardController {
    
        private final BoardService boardService;
    
        @GetMapping("/board/listMap")
        public String boardListMap(Model model) {
            List<Map<String, Object>> list = boardService.findListMap();
            model.addAttribute("list", list);
            return "board/listMap";
        }
    }

     

    view(thymeleaf)

    <!doctype html>
    <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>listMap</title>
    </head>
    <body>
    <div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>번호</th>
                <th>제목</th>
                <th>내용</th>
                <th>작성자</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="item : ${list}">
                <td th:text="${item[boardSeq]}"></td>
                <td th:text="${item[title]}"></td>
                <td th:text="${item[contents]}"></td>
                <td th:text="${item[name]}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    </body>
    </html>

     

    결과화면 😍

    반응형