본문 바로가기

Archived(Programming)/Spring #1(기초)

Spring_data 처리5(글 수정)

# Mapper 설정

boardMapper.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.sinc.intern.board">
	...
	
	<update id="update" parameterType="board">
		UPDATE  INTERN_BOARD_TBL
		SET		TITLE=#{title},
				CONTENT=#{content}
		WHERE	SEQ = #{seq}
	</update>
	
	<update id="upCnt" parameterType="board">
		UPDATE 	INTERN_BOARD_TBL
		SET		VIEWCNT = VIEWCNT + 1
		WHERE	SEQ = #{seq}
	</update>
</mapper>  

# DAO 구현

package com.sinc.intern.board.model.sql;

import java.util.List;

public interface BoardDao {
	public List<Object> listRow();
	public int 			registerRow(Object obj);
	public Object		readRow(Object obj);
	public int			deleteRow(Object obj);
	public int			updateRow(Object obj);
}
package com.sinc.intern.board.model.sql;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

@Repository("boardD")
public class BoardDaoImpl implements BoardDao {

	...
	// READ CNT
	public int upCnt(Object obj) {
		return session.update(PREFIX_B + ".upCnt", obj);
	}
	...
	@Override
	public int updateRow(Object obj) {
		System.out.println("Board update SEQ : " + obj);
		
		// update
		return session.update(PREFIX_B + ".update", obj); 
	}
}

# Service 구현

package com.sinc.intern.board.service;

import java.util.List;

public interface BoardService {
	public List<Object> list();
	public int			register(Object obj);
	public Object		read(Object obj);
	public int			delete(Object obj);
	public int			update(Object obj);
}
package com.sinc.intern.board.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sinc.intern.board.model.sql.BoardDao;

@Service("boardS")
public class BoardServiceImpl implements BoardService{

	@Resource(name="boardD")
	private BoardDao dao;

	// Service에서 DAO 호출 후 메서드 수행
	@Override
	public List<Object> list() {
		System.out.println("Board get list");
		return dao.listRow();
	}

	@Override
	public int register(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("Board service insert data");
		return dao.registerRow(obj);
	}

	@Override
	public Object read(Object obj) {
		System.out.println("Board service Read data");
		return dao.readRow(obj);
	}

	@Override
	public int delete(Object obj) {
		System.out.println("Board service delete data");
		return dao.deleteRow(obj);
	}

	@Override
	public int update(Object obj) {
		System.out.println("Board service update data");
		return dao.updateRow(obj);
	}

}

# Controller 구현

package com.sinc.intern.board.ctrl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sinc.intern.board.model.vo.BoardVO;
import com.sinc.intern.board.service.BoardService;

@Controller
public class BoardCtrl {
	...
	// 글 수정
	@RequestMapping("/modify.do")
	public String modify(BoardVO obj) {
		System.out.println("Board update : " + obj);
		int result = service.update(obj); 
		
		// 성공 시
		if(result == 1) {
			System.out.println("Update Succeess");
		}else { // 실패시
			System.out.println("Update Fail");
		}
		return "redirect:/listPage.do";		
	}
}

# Front 설정

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>

<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>


<%@include file="../include/header.jsp" %>

    <!-- Main content -->
    <section class="content">
      <div class="row">
      <!-- left column -->
      <div class="col-md-12">
        <!-- general form elements -->
        <div class="box box-primary">
        <div class="box-header">
          <h3 class="box-title">READ BOARD</h3>
        </div><!-- /.box-header -->
<%-- 
<form role="form" action="modifyPage" method="post">
    
    <input type='hidden' name='bno' value ="${boardVO.bno}">
    <input type='hidden' name='page' value ="${cri.page}">
    <input type='hidden' name='perPageNum' value ="${cri.perPageNum}">
    
</form>   
--%>    
  <input type="hidden" id="seq" value="${board.seq}"/>
  <div class="box-body">
    <div class="form-group">
      <label for="exampleInputEmail1">Title</label>
      <input type="text" name='title' class="form-control" 
         value="${board.title}" id="title">
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Content</label>
      <textarea class="form-control"  name="content" rows="3" id="content">${board.content}</textarea>
    </div>
    <div class="form-group">
      <label for="exampleInputEmail1" >Writer</label>
      <input type="text" name="writer" class="form-control" 
        value="${board.writer}">
    </div>
  </div><!-- /.box-body -->

  <div class="box-footer">
	<c:if test="${ loginUser.id == board.writer }">
	<button id="modify" type="button" class="btn btn-warning">Modify</button> 
	<button id="remove" type="button" class="btn btn-danger">REMOVE</button> 
    </c:if>
    <button id="list" type="button" class="btn btn-primary">GO LIST </button>
  </div>

<script>
</script>


  
  
        </div><!-- /.box -->
      </div><!--/.col (left) -->
 
      </div>   <!-- /.row -->
    </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

<script type="text/javascript">
	$(function() {
		// Go List
		$("#list").click(function() {
			location.href="listPage.do";
		});
		
		// REMOVE
		$("#remove").click(function() {
			location.href="remove.do?seq="+${board.seq};	
		});
		
		// MODIFY
		$("#modify").click(function(){
			location.href="modify.do?seq="+ $("#seq").val() + "&title=" + $("#title").val() + "&content=" + $("#content").val();
		});
	})
</script>

<%@include file="../include/footer.jsp" %>