작성 순서

1. Mapper 처리 및 테스트 (RecipeMapper, RecipeMapper.xml, RecipeMapperTest)

2. Service 처리 및 테스트

3. Controller 처리

4. View 처리

 

 

1-1. RecipeMapper

 게시판 목록을 불러오는 추상 메서드 만들기

/* 게시판 목록 */
public List<RecipeVO> getList();

 

1-2. RecipeMapper.xml

 <!-- 게시판 목록 -->
<select id="getList" resultType="RecipeVO">
     select * from recipe
</select>

 

 

1-3. RecipeMapperTest

이제 테스트 해보기

List<RecipeVO> list = recipeMapper.getList();

//반복문으로 테스트 방법3가지 

1. for 반복문

for(int i=0; i<list.size(); i++) {
   log.info("" + list.get(i));    //0~마지막까지
}

2. foreach 반복문
for(RecipeVO vo : list) {
    log.info(""+vo);
}

※  log.info("") 쌍따옴표 적은 이유는 문자열로 표시하려고

 

 

 

2-1 RecipeService에 RecipeMapper 와 같은 추상메서드 복붙

/* 게시판 목록 */
public List<RecipeVO> getList();

 

2-2 RecipeServiceImpl

@Override
public List<RecipeVO> getList() {
    return recipeMapper.getList();
}

 

3-1 Controller

@GetMapping("/list")
public String recipeListGET(Model model) {
    //boardList에 모든 게시글을 전달
    model.addAttribute("boardList", recipeService.getList());
    return "recipe_list";
}

4. view 

[ recipe_list.html ]

<div class="card">
    <table class="table align-items-center mb-0">
        <thead>
            <tr>
                <th class="text-secondary text-xxs font-weight-bolder opacity-7">NO</th>
                <th class="text-secondary text-xxs font-weight-bolder opacity-7 ps-2">TITLE</th>
                <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">AUTHOR</th>
                <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">등록일</th>
                <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">수정일</th>
                <th class="text-secondary opacity-7">기타</th>
            </tr>
            </thead>
        <tbody>
            <tr th:each="board:${boardList}">
                <td class="align-middle text-center">
                    <span class="text-secondary text-xs" th:text="${board.bno}"></span>
                </td>
                <td class="align-middle text-center">
                    <span class="text-secondary text-xs" th:text="${board.title}"></span>
                </td>
                <td class="align-middle text-center">
                    <span class="text-secondary text-xs" th:text="${board.writer}"></span>
                </td>
                <td class="align-middle text-center">
                    <span class="text-secondary text-xs" 
                    th:text="${#temporals.format(board.registerdate, 'yyyy-MM-dd a hh:mm:ss')}"></span>
                </td>
                <td class="align-middle text-center">
                    <span class="text-secondary text-xs" 
                    th:text="${#temporals.format(board.updateDate, 'yyyy-MM-dd a hh:mm:ss')}"></span>
                </td>
                <td class="align-middle">
                    <a href="#" class="text-secondary text-xs"> Edit </a>
                  </td>
            </tr>
        </tbody> 
    </table>
</div>

 

String타입을 Date타입으로 포맷하는 방법

a : 오전오후를 나타냄
H : 24시간표시, h : 12시간표시

th:text="${#temporals.format(board.registerdate, 'yyyy-MM-dd a hh:mm:ss')}"

 

Date타입을 Date타입으로 포맷하는 방법

<div th:text="${#dates.format(item.createDt, 'yyyy-MM-dd HH:mm:ss')}"></div>

 

 

 

반응형
LIST

+ Recent posts