# 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">
<select id="list" resultType="board">
SELECT SEQ, TITLE, CONTENT, WRITER, TO_CHAR(REGDATE, 'YYYY-MM-DD') AS REGDATE, VIEWCNT
FROM INTERN_BOARD_TBL
</select>
<insert id="register" parameterType="board">
INSERT INTO INTERN_BOARD_TBL
VALUES( BOARD_SEQ.NEXTVAL, #{title}, #{content}, #{writer}, DEFAULT, DEFAULT)
</insert>
<select id="read" parameterType="board" resultType="board">
SELECT SEQ, TITLE, CONTENT, WRITER, TO_CHAR(REGDATE, 'YYYY-MM-DD') AS REGDATE, VIEWCNT
FROM INTERN_BOARD_TBL
WHERE SEQ = #{seq}
</select>
<delete id="delete" parameterType="board">
DELETE
FROM INTERN_BOARD_TBL
WHERE SEQ = #{seq}
</delete>
</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);
}
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 {
// Session 생성
@Resource(name="sqlSession")
private SqlSession session;
public static final String PREFIX_B = "com.sinc.intern.board";
@Override
public List<Object> listRow() {
System.out.println("Board DAO List");
// selectList
List<Object> result = session.selectList(PREFIX_B +".list");
System.out.println("DAO Board Select result : " + result);
return result;
}
@Override
public int registerRow(Object obj) {
System.out.println("Board DAO Insert:" + obj);
// insert
return session.insert(PREFIX_B + ".register", obj);
}
@Override
public Object readRow(Object obj) {
System.out.println("Board Read SEQ : " + obj);
Object result = session.selectOne(PREFIX_B + ".read", obj);
System.out.println("Board Read result : " + result);
return result;
}
@Override
public int deleteRow(Object obj) {
System.out.println("Board delete SEQ : " + obj);
// delete
return session.delete(PREFIX_B + ".delete", 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);
}
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);
}
}
# Ctrl 설정
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("/remove.do")
public String remove(BoardVO obj) {
System.out.println("Board delete : " + obj);
int result = service.delete(obj);
// 성공 시
if(result == 1) {
System.out.println("Delete Succeess");
return "redirect:/listPage.do";
}else { // 실패시
System.out.println("Delete 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>
--%>
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" name='title' class="form-control"
value="${board.title}" readonly="readonly">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Content</label>
<textarea class="form-control" name="content" rows="3"
readonly="readonly">${board.content}</textarea>
</div>
<div class="form-group">
<label for="exampleInputEmail1" >Writer</label>
<input type="text" name="writer" class="form-control"
value="${board.writer}" readonly="readonly">
</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};
});
})
</script>
<%@include file="../include/footer.jsp" %>
'Archived(Programming) > Spring #1(기초)' 카테고리의 다른 글
Spring_data 처리6(ajax 통신 통한 search) (0) | 2020.02.07 |
---|---|
Spring_data 처리5(글 수정) (0) | 2020.02.07 |
Spring_data 처리3(글 읽기) (1) | 2020.02.06 |
Spring_data 처리2(글 작성) (0) | 2020.02.06 |
Spring_data 처리(글 목록) (0) | 2020.02.06 |