화면
SQL 질의 결과
reGroup
- 답글과 일반 글을 포함한 글 덩어리를 묶는데 사용
- 새 글을 작성하면 +1 되지만 답글을 작성한다고 증가하지는 않음
reLevel, reStep
- reStep은 답글 개수를 말해주고, reLevel은 답글의 답글 개수
- 현재 여기서는 중복 답글 기능을 구현하지 않았기에 사실상 reStep만 유효한 의미를 가짐
- 처음 일반 글을 작성할 때 1로 insert 됨
- 답글을 작성하면 얘네들은 1씩 증가함
- 답글의 depth가 되는 것임
- 이걸 응용해서 css 효과를 준다 (오른쪽으로 조금씩 밀어서)
마이바티스
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jjang051.replyboard03.dao.ReplyBoardDao">
<insert id="insertBoard" parameterType="ReplyBoardDto">
INSERT INTO REPLYBOARD03 VALUES (
REPLYBOARD03_SEQ.NEXTVAL,
#{subject},
#{writer},
#{password},
SYSDATE,
#{contents},
1,
#{reGroup},
#{reLevel},
#{reStep},
0
)
</insert>
<insert id="insertReplyBoard" parameterType="ReplyBoardDto">
INSERT INTO REPLYBOARD03 VALUES (
REPLYBOARD03_SEQ.NEXTVAL,
#{subject},
#{writer},
#{password},
SYSDATE,
#{contents},
1,
#{reGroup},
#{reLevel},
#{reStep},
0
)
</insert>
<select id="getAllBoardList" resultType="ReplyBoardDto" parameterType="HashMap">
SELECT ROWNUM AS NUM, B.* from
( SELECT * FROM REPLYBOARD03
<if test="category != null and searchTxt!=''">
WHERE ${category} LIKE '%' || #{searchTxt} || '%'
</if>
ORDER BY REGROUP DESC, RELEVEL ASC ) B
</select>
<select id="getSelectOne" resultType="ReplyBoardDto" parameterType="Integer">
SELECT * FROM REPLYBOARD03 WHERE NO = #{no}
</select>
<select id="getMaxReGroup" resultType="Integer">
SELECT NVL(MAX(REGROUP),1) AS REGROUPMAX FROM REPLYBOARD03
</select>
<update id="updateHit" parameterType="Integer">
UPDATE REPLYBOARD03 SET HIT = HIT + 1 WHERE NO = #{no}
</update>
<update id="updateReLevel" parameterType="ReplyBoardDto">
UPDATE REPLYBOARD03 SET RELEVEL = RELEVEL + 1
WHERE REGROUP = #{reGroup} AND RELEVEL <![CDATA[ > ]]> #{reLevel}
</update>
<update id="deleteReplyBoard" parameterType="ReplyBoardDto">
UPDATE REPLYBOARD03 SET AVAILABLE = 0 WHERE NO = #{no} AND PASSWORD = #{password}
</update>
</mapper>
ServiceImpl
package com.jjang051.replyboard03.service;
import com.jjang051.replyboard03.dao.ReplyBoardDao;
import com.jjang051.replyboard03.dto.ReplyBoardDto;
import java.util.HashMap;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class ReplyBoardServiceImpl implements ReplyBoardService {
@Autowired
ReplyBoardDao replyBoardDao;
public ReplyBoardServiceImpl() {
log.info("===========ReplyBoardService=========");
}
public int insertBoard(ReplyBoardDto replyBoardDto) {
replyBoardDto.setReGroup(getMaxReGroup() + 1);
replyBoardDto.setReLevel(1);
replyBoardDto.setReStep(1);
int result = replyBoardDao.insertBoard(replyBoardDto);
return result;
}
public List<ReplyBoardDto> getAllBoardList(
String category,
String searchTxt
) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("category", category);
hashMap.put("searchTxt", searchTxt);
List<ReplyBoardDto> boardList = replyBoardDao.getAllBoardList(hashMap);
return boardList;
}
@Override
public ReplyBoardDto getSelectOne(int no) {
ReplyBoardDto replyBoardDto = replyBoardDao.getSelectOne(no);
return replyBoardDto;
}
@Override
public int updateHit(int no) {
int result = replyBoardDao.updateHit(no);
return result;
}
@Override
public int getMaxReGroup() {
int result = replyBoardDao.getMaxReGroup();
return result;
}
@Override
public int insertReplyBoard(ReplyBoardDto replyBoardDto) {
int reGroup = replyBoardDto.getReGroup();
int reLevel = replyBoardDto.getReLevel();
int reStep = replyBoardDto.getReStep();
updateReLevel(replyBoardDto);
replyBoardDto.setReGroup(reGroup);
replyBoardDto.setReLevel(reLevel + 1);
replyBoardDto.setReStep(reStep + 1);
int result = replyBoardDao.insertReplyBoard(replyBoardDto);
// db에가서 나보다 레벨이 높은 애들은 전부 1증가
return result;
}
public int updateReLevel(ReplyBoardDto replyBoardDto) {
return replyBoardDao.updateReLevel(replyBoardDto);
}
public int deleteReplyBoard(ReplyBoardDto replyBoardDto) {
int result = replyBoardDao.deleteReplyBoard(replyBoardDto);
return result;
}
public List<ReplyBoardDto> getSearchBoardList(String searchTxt) {
List<ReplyBoardDto> boardList = replyBoardDao.getSearchBoardList(searchTxt);
return boardList;
}
}
HTML
- 타임리프를 활용하여 클래스명에 붙는 숫자를 1씩 증가시키는 효과를 줄 것이다
- SCSS에서 답글처럼 보이게 하는 CSS를 작성하는 데에 활용할 것이다
th:class="'space'+${item.reStep}"
<tbody>
<tr th:each="item : ${boardList}">
<td scope="row" th:text="${item.no}">1</td>
<td class="space1" th:class="'space'+${item.reStep}">
<a th:href="@{/board/view(no=${item.no})}" th:text="${item.subject}" href="view.html">
제목이 들어갑니다.
</a>
</td>
<td th:text="${item.name}">글쓴 사람 이름</td>
<td th:text="${item.regDate}">2023.03.23</td>
<td th:text="${item.hit}">1</td>
</tr>
</tbody>
SCSS
.space1 {
padding-left: 0px !important;
}
.space2 {
padding-left: 30px !important;
}
.space3 {
padding-left: 60px !important;