🟠 요구사항
- 답글은 최근에 등록된 것일수록 위에 있게 한다.
- 위의 이미지에서 18번 답글이 17번 답글보다 더 최신 답글이다.
- 16번 글은 18, 17번 답글의 부모 게시글이다.
- reboard 답글 테이블의 필드
- article_no 답글이 달린 조상의 번호
- depth 쉽게 생각하면, 들여쓰기
- step 답글의 정렬 순서
- parent_no 원글의 번호(누구의 답글인지?) => 내 바로 위 부모의 번호
🟢 MyBatis 및 서비스
1. 새 글을 작성했을 때
@Transactional
@Override
public void writeArticle(Reboard reboard) throws Exception {
reboardMapper.writeArticle(reboard);
reboardMapper.newReplyArticle(reboard);
}
<insert id="writeArticle" parameterType="reboard">
insert into board (user_id, subject, content, hit, register_time, bcode)
values (#{userId}, #{subject}, #{content}, 0, now(), #{bcode})
<selectKey resultType="int" keyProperty="articleNo" order="AFTER">
select last_insert_id()
</selectKey>
</insert>
<insert id="newReplyArticle" parameterType="reboard">
insert into reboard (article_no, group_no, depth, step, parent_no, reply)
values (#{articleNo}, #{articleNo}, #{depth}, #{step}, #{parentNo}, 0)
</insert>
N, N, 0, 0, 0 이런 식으로 파라미터 안에 값이 들어간다.
2. 답글을 작성했을 때
@Transactional
@Override
public void replyArticle(Reboard reboard) throws Exception {
reboardMapper.updateStep(reboard);
reboardMapper.writeArticle(reboard);
reboardMapper.replyArticle(reboard);
reboardMapper.updateReply(reboard.getParentNo());
}
<update id="updateStep" parameterType="reboard">
update reboard
set step = step + 1
where group_no = #{groupNo} and step > #{step}
</update>
같은 그룹 내에서 더 큰 step을 가진 답글들의 step을 1씩 증가시킨다.
<insert id="writeArticle" parameterType="reboard">
insert into board (user_id, subject, content, hit, register_time, bcode)
values (#{userId}, #{subject}, #{content}, 0, now(), #{bcode})
<selectKey resultType="int" keyProperty="articleNo" order="AFTER">
select last_insert_id()
</selectKey>
</insert>
글 작성
<insert id="replyArticle" parameterType="reboard">
insert into reboard (article_no, group_no, depth, step, parent_no, reply)
values (#{articleNo}, #{groupNo}, #{depth} + 1, #{step} + 1, #{parentNo}, 0)
</insert>
답글 등록
글에 달린 답글의 깊이가 1 증가
답글들끼리의 번호 1 증가
<update id="updateReply" parameterType="int">
update reboard
set reply = reply + 1
where article_no = #{patendNo}
</update>
답글이 달린 게시글의 reply를 1 올려줌으로써 답글의 갯수가 증가한다.
3. 전체 게시글 조회
<select id="listArticle" parameterType="map" resultMap="reply">
select b.article_no, b.user_id, b.subject, b.content, b.hit, b.register_time, m.user_name,
r.reboard_no, r.group_no, r.depth, r.step, r.parent_no, r.reply
from board b join users m
on b.user_id = m.user_id
join reboard r
on b.article_no = r.article_no
where b.bcode = #{bcode}
<include refid="search"/>
order by r.group_no desc, r.step
limit #{start}, #{listsize}
</select>
<sql id="search">
<if test="word != null and word != ''">
<if test="key == 'subject'">
and subject like concat('%', #{word}, '%')
</if>
<if test="key != 'subject'">
and ${key} = #{word}
</if>
</if>
</sql>
그룹번호를 내림차순(최근에 작성된 게시글 순), 답글 안에서의 순서 번호가 작은 순으로 정렬한다.
반응형