스프링 게시판 스터디 9 - 조회수 기능 추가하기
December 04, 2018
- 게시글을 클릭했다가 다시 목록으로 돌아왔을 때 조회수가 하나씩 증가해야 하는데, 깜빡했네요. 기능을 추가해볼게요.
9-1 Controller 수정
@Controller
public class BController {
// 생략
@RequestMapping("/content_view")
public String content_view(HttpServletRequest request, Model model) {
logger.info("content_view");
IDao dao = sqlSession.getMapper(IDao.class);
// 여기 수정
dao.content_count(Integer.parseInt(request.getParameter("bId")));
model.addAttribute("list", dao.content_view(Integer.parseInt(request.getParameter("bId"))));
return "content_view";
}
}
9-2 IDao 수성
package com.javalec.bbs_prac.dao;
import java.util.ArrayList;
import com.javalec.bbs_prac.dto.BDto;
public interface IDao {
public ArrayList<BDto> list();
public void delete(int bId);
public void write(String bUsername, String bTitle, String bContent);
public BDto content_view(int bId);
// 여기 추가
public void content_count(int bId);
public BDto update_view(int bId);
public void update(String bUsername, String bTitle, String bContent, int bId);
}
8-3 IDao.xml (Mapper) 작성
<?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="com.javalec.bbs_prac.dao.IDao">
<select id="list" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD ORDER BY 1 DESC
</select>
<delete id="delete">
DELETE FROM BOARD WHERE BID = #{param1}
</delete>
<insert id="write">
INSERT INTO BOARD VALUES(BOARD_SEQ.NEXTVAL, #{param1}, #{param2}, #{param3}, SYSDATE, 0)
</insert>
<select id="content_view" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD WHERE BID = #{param1}
</select>
<!-- 여기 추가 -->
<update id="content_count">
UPDATE BOARD SET BCOUNT = BCOUNT+1 WHERE BID=#{param1}
</update>
<select id="update_view" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD WHERE BID=#{param1}
</select>
<update id="update">
UPDATE BOARD SET BUSERNAME=#{param1}, BTITLE=#{param2}, BCONTENT=#{param3} WHERE BID=#{param4}
</update>
</mapper>