Spring MVC - 댓글 구현

Programming/Java 2017.01.07 19:19 Posted by 파란크리스마스

댓글 테이블 생성

CREATE TABLE `main_board_reply` (
  `seq` int(11) NOT NULL AUTO_INCREMENT,
  `main_seq` int(11),
  `title` varchar(11) DEFAULT NULL,
  `name` varchar(11) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `count` int(11) DEFAULT NULL,
  `content` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`seq`)
)

MainBoardReply 클래스 생성

package com.iot.db.domain;

import java.sql.Date;

import bluexmas.util.DateUtils;
import net.sf.json.JSONObject;

public class MainBoardReply {

	// pk
	private int seq;

	private int main_seq;
	private String title;
	private String name;
	private Date date;
	private int count;
	private String content;

	private String delete_flag = "N";

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public int getSeq() {
		return this.seq;
	}

	public void setMain_seq(int main_seq) {
		this.main_seq = main_seq;
	}

	public int getMain_seq() {
		return this.main_seq;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return this.title;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return this.date;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return this.count;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return this.content;
	}

	public String getDelete_flag() {
		return delete_flag;
	}

	public void setDelete_flag(String delete_flag) {
		this.delete_flag = delete_flag;
	}

	public JSONObject getJSONObject() {
		JSONObject jobj = new JSONObject();
		jobj.put("seq", this.seq);
		jobj.put("main_seq", this.main_seq);
		jobj.put("title", this.title);
		jobj.put("name", this.name);
		jobj.put("date", DateUtils.datetoIsoDate(this.date));
		jobj.put("count", this.count);
		jobj.put("content", this.content);
		return jobj;
	}
}

MainBoardReplyMapper 인터페이스 생성

package com.iot.db.mapper;

import java.util.List;
import java.util.Map;

import com.iot.db.domain.MainBoardReply;

public interface MainBoardReplyMapper {

	public MainBoardReply selectMainBoardReply(Map<String, Object> params);

	public void insertMainBoardReply(MainBoardReply mainBoardReply);

	public void updateMainBoardReply(MainBoardReply mainBoardReply);

	public void deleteMainBoardReply(Map<String, Object> params);

	public int getCount();

	public int getCountFormData(Map<String, Object> params);

	public List<MainBoardReply> listMainBoardReply(Map<String, Object> map);

}

MainBoardReplyMapper.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="com.iot.db.mapper.MainBoardReplyMapper">

	<!-- selectMainBoardReply -->
	<select id="selectMainBoardReply" parameterType="map" resultType="com.iot.db.domain.MainBoardReply">
	  select seq
	         ,main_seq
	         ,title
	         ,name
	         ,date
	         ,count
	         ,content
	    from main_board_reply
	   where seq = #{seq}
	</select>
	
	<!-- updateMainBoardReply -->
	<update id="updateMainBoardReply" parameterType="com.iot.db.domain.MainBoardReply" statementType="PREPARED">
	    update main_board_reply
	      <trim prefix="SET" suffixOverrides=",">
	        <if test="main_seq != null">main_seq = #{main_seq, jdbcType=INTEGER} ,</if>
	        <if test="title != null">title = #{title, jdbcType=VARCHAR} ,</if>
	        <if test="name != null">name = #{name, jdbcType=VARCHAR} ,</if>
	        <if test="date != null">date = #{date, jdbcType=DATE} ,</if>
	        <if test="count != null">count = #{count, jdbcType=INTEGER} ,</if>
	        <if test="content != null">content = #{content, jdbcType=VARCHAR} ,</if>
	      </trim>
	   where seq = #{seq}
	</update>
	
	<!-- insertMainBoardReply -->
	<insert id="insertMainBoardReply" parameterType="com.iot.db.domain.MainBoardReply" statementType="PREPARED">
	    insert into main_board_reply(
	      <trim suffixOverrides=",">
	        <if test="main_seq != null">main_seq ,</if>
	        <if test="title != null">title ,</if>
	        <if test="name != null">name ,</if>
	        <if test="date != null">date ,</if>
	        <if test="count != null">count ,</if>
	        <if test="content != null">content ,</if>
	      </trim>
	      ) values	(
	      <trim suffixOverrides=",">
	        <if test="main_seq != null">#{main_seq, jdbcType=INTEGER} ,</if>
	        <if test="title != null">#{title, jdbcType=VARCHAR} ,</if>
	        <if test="name != null">#{name, jdbcType=VARCHAR} ,</if>
	        <if test="date != null">#{date, jdbcType=DATE} ,</if>
	        <if test="count != null">#{count, jdbcType=INTEGER} ,</if>
	        <if test="content != null">#{content, jdbcType=VARCHAR} ,</if>
	      </trim>
	      )
	</insert>
	
	<!-- deleteMainBoardReply -->
	<delete id="deleteMainBoardReply" parameterType="map" statementType="PREPARED">
	    delete from main_board_reply
	   where seq = #{seq}
	</delete>
	
	<!-- getCount -->
	<select id="getCount" resultType="int">
	  select count(*)
	    from main_board_reply
	</select>
	
	<!-- getCountFormData -->
	<select id="getCountFormData" parameterType="map" resultType="int">
	  select count(*)
	    from main_board_reply
	    <trim prefix="WHERE" prefixOverrides="AND |OR ">
	      <if test="searchstr != null">and title like '%${searchstr}%'</if>
	    </trim>
	</select>
	
	<!-- listMainBoardReply -->
	<select id="listMainBoardReply" parameterType="map" resultType="com.iot.db.domain.MainBoardReply">
	  select * 
	    from main_board_reply
	   where main_seq = #{main_seq}
	</select>

</mapper>

MainBoardReplyService 클래스 생성

package com.iot.db.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iot.db.domain.MainBoardReply;
import com.iot.db.mapper.MainBoardReplyMapper;

@Service
public class MainBoardReplyService {

	public final static int pagerowcnt = 25;

	@Autowired
	private MainBoardReplyMapper mainBoardReplyMapper;

	public MainBoardReply selectMainBoardReply(int seq) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("seq", seq);
		return mainBoardReplyMapper.selectMainBoardReply(params);
	}

	public void insertMainBoardReply(MainBoardReply mainBoardReply) {
		mainBoardReplyMapper.insertMainBoardReply(mainBoardReply);
	}

	public void updateMainBoardReply(MainBoardReply mainBoardReply) {
		mainBoardReplyMapper.updateMainBoardReply(mainBoardReply);
	}

	public void deleteMainBoardReply(int seq) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("seq", seq);
		mainBoardReplyMapper.deleteMainBoardReply(params);
	}

	public void deleteMainBoardReply(Map<String, Object> params) {
		mainBoardReplyMapper.deleteMainBoardReply(params);
	}

	public int getCount() {
		return mainBoardReplyMapper.getCount();
	}

	public List<MainBoardReply> listMainBoardReply(int main_seq) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("main_seq", main_seq);
		return mainBoardReplyMapper.listMainBoardReply(params);
	}

}

댓글 입력 Form (jsp에 반영)

<form action="<c:url value="/insertReplyPage.iot"/>" method="post">
	<input type="hidden" name="main_seq" value="${seq}"/>
	<textArea name ="comment" cols="60" rows="20"></textArea>
	<input type="submit" value="답변 입력">
	<input type="button" value="취 소">
</form>

댓글 입력 처리 (컨트롤러에 반영)

	@Autowired
	MainBoardReplyService replyService;

	@RequestMapping(value = "/insertReplyPage.iot", method = RequestMethod.POST)
	public String insertReplyPage(
			@RequestParam("main_seq") int main_seq,
			@RequestParam("comment") String comment,
			ModelMap modelMap) throws Exception 
	{
		
		System.out.println("main_seq = " + main_seq + "/" + comment);
		
		MainBoardReply reply = new MainBoardReply();
		reply.setMain_seq(main_seq);
		reply.setContent(comment);
		
		replyService.insertMainBoardReply(reply);
		
		return "redirect:/moveToMainBoard.iot";
	}

게시판 도메인에 댓글 목록 추가

	private List<MainBoardReply> replyList;
	
	public List<MainBoardReply> getReplyList() {
		return replyList;
	}
	public void setReplyList(List<MainBoardReply> replyList) {
		this.replyList = replyList;
	}

게시판 리스트 (컨트롤러에 반영)

	@RequestMapping(value = "/moveToMainBoard.iot", method = RequestMethod.GET)
	public String moveToMainBoard(ModelMap modelMap) throws Exception {
		
		// 게시판 내용 조회
		List<main_boardDto> main_boardDtoList = service.selectMainBoardList();
		
		// 댓글 조회
		for (main_boardDto board: main_boardDtoList) {
			List<MainBoardReply> replyList = replyService.listMainBoardReply(board.getSeq());
			board.setReplyList(replyList);
		}
		
		System.out.println("main_baordDto : " +main_boardDtoList);
		modelMap.addAttribute("main_boardDto",main_boardDtoList);
		return "/main_board";
	}

게시판 리스트 (jsp에 반영)

  <c:forEach items="${main_boardDto}" var="data" varStatus="loop">
  	<tr>
  		<td><input type="checkbox" name="delete_seqs" value="${data.seq}"/></td>
  		<td>${data.seq}</td>
  		<td class="underline" onclick="javascript:readContent(${data.seq});">${data.title}</td>
  		<td onclick="javascript:modifyExplanation_content(${data.seq});">${data.name}</td>
  		<td>${data.date}</td>
  		<td>${data.count}</td>
    </tr>
    <c:forEach items="${data.replyList}" var="reply" varStatus="loop">
  	  <tr>
  		<td><input type="checkbox" name="delete_seqs" value="${reply.seq}"/></td>
  		<td>${reply.seq}</td>
  		<td>  -> ${reply.content}</td>
  		<td>${reply.name}</td>
  		<td>${reply.date}</td>
  		<td>${reply.count}</td>
      </tr>
    </c:forEach>
  </c:forEach>
저작자 표시
신고


 

티스토리 툴바