Spring

73일차//spring/ Tiles를 활용한 게시판만들기 _3(게시판 작성, 수정, 삭제, 디테일 )

aesup 2021. 4. 23. 16:30
728x90

게시판 글작성

로그인 성공 시 게시판으로 이동

왼쪽 배너 메뉴에서 글쓰기를 입력시 입력가능

	@RequestMapping(value = "bbsWrite.do", method = RequestMethod.GET)
	public String bbsWrite(Model model) {
		logger.info("BbsController bbsWrite() " + new Date());
		
		model.addAttribute("doc_title", "글작성");
		
		return "bbswrite.tiles";
	}
	
	

<!--글쓰기  -->
<insert id="addBbs" parameterType="bit.com.a.dto.BbsParam">
INSERT INTO BBS (SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT) 
VALUES( SEQ_BBS.NEXTVAL, #{id}, (SELECT NVL(MAX(REF), 0)+1  FROM BBS), 0, 0,  #{title},
 #{content}, SYSDATE, 0, 0)
</insert>

BbsDaoImpl - addBbs

@Override
	public boolean addBbs(BbsDto dto) {
		
		int a = session.insert(ns + "addBbs", dto);
		
		return a>0?true:false;
	}

BbsServiceImpl -addBbs

	@Override
	public boolean addBbs(BbsDto dto) {
		// TODO Auto-generated method stub
		return dao.addBbs(dto);
	}

 

@RequestMapping(value = "bbsWriteAf.do", method = RequestMethod.POST)
	public String bbsWriteAf(BbsDto bbs) {
		
		boolean a = service.addBbs(bbs);
		
		if(a) {
			logger.info("글이 추가되었습니다 " + new Date());
			return "redirect:/bbslist.do";
		}
		logger.info("글이 추가 되지 않았습니다 " + new Date());
		
		return "redirect:/bbsWrite.do";
	}

 

게시판 글 수정

글수정은 디테일 페이지에서 수정가능

게시판에서 글 제목을 클릭시 디테일 페이지로 이동

글수정 버튼을 누를시 BbsDto 클래스 변수들로 파람을 받는다

 

BbsController

 

	@RequestMapping(value = "updateBbs.do", method = RequestMethod.GET)
	public String updateBbs(Model model, int seq) {
		logger.info("BbsController updateBbs() " + new Date());
		
		BbsDto dto = service.getBbs(seq);
		model.addAttribute("dto", dto);
		model.addAttribute("doc_title", "게시물 수정");
		
		
		return "bbsupdate.tiles";
	}
	

	@RequestMapping(value = "updateBbsAf.do", method = RequestMethod.GET)
	public String updateBbsAf(Model model, BbsDto bbsdto) {
		logger.info("BbsController updateBbs() " + new Date());
		
		
		boolean a =service.updateBbs(bbsdto);
		
		if(a) {
			logger.info("글이 수정되었습니다 " + new Date());
			return "redirect:/bbslist.do";
		}
		logger.info("글이 수정되지않았습니다 " + new Date());
		
		
		return "redirect:/bbslist.do";
	}

Bbs.xml

<!-- 게시물수정 -->

<update id="updateBbs"  parameterType="bit.com.a.dto.BbsDto">
		UPDATE BBS
		SET TITLE=#{title}, CONTENT=#{content} 
		WHERE SEQ=#{seq}
</update>

BbsDaoImpl - updateBbs

@Override
	public boolean updateBbs(BbsDto dto) {
		
		int a = session.update(ns + "updateBbs", dto);
		return a>0?true:false;
	}

 

게시판 글 삭제

Bbs.xml

<!-- 게시물삭제 -->
<update id="deleteBbs" parameterType="int">

 UPDATE BBS 
 SET DEL=1 
 WHERE SEQ=#{seq}

</update>

BbsDaoImpl - bbsdelete

@Override
	public boolean bbsdelete(int seq) {
		
		int a = session.update(ns + "deleteBbs", seq);
		
		return a>0?true:false;
	}

BbsServiceImpl -bbsdelete

@Override
	public boolean bbsdelete(int seq) {
		// TODO Auto-generated method stub
		return dao.bbsdelete(seq);
	}

클래스 별 전체 코드

(인터페이스는 미업로드 어차피 상속받기에 유추가능)

BbsDaoImpl

package bit.com.a.dao.impl;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import bit.com.a.dao.BbsDao;
import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;

@Repository
public class BbsDaoImpl implements BbsDao {

	@Autowired
	SqlSessionTemplate session;
	
	String ns = "Bbs.";
		
	@Override
	public List<BbsDto> getBbslist(BbsParam bbs) {		
		return session.selectList(ns + "bbslist", bbs);
	}

	@Override
	public int getBbsCount(BbsParam bbs) {		
		return session.selectOne(ns + "getBbsCount", bbs);
	}

	@Override
	public BbsDto getBbs(int seq) {
		// TODO Auto-generated method stub
		return session.selectOne(ns + "getBbs", seq);
	}

	@Override
	public void readCount(int seq) {
		
		 session.update(ns + "readcount", seq);
	}

	@Override
	public boolean addBbs(BbsDto dto) {
		
		int a = session.insert(ns + "addBbs", dto);
		
		return a>0?true:false;
	}

	@Override
	public boolean updateBbs(BbsDto dto) {
		
		int a = session.update(ns + "updateBbs", dto);
		return a>0?true:false;
	}

	@Override
	public boolean bbsdelete(int seq) {
		
		int a = session.update(ns + "deleteBbs", seq);
		
		return a>0?true:false;
	}

	@Override
	public boolean answer(int seq, BbsDto dto) {
		
		//행번호 세팅
		int a = session.update( ns + "updateStep", seq );
		int b = session.insert(ns + "insertAns", dto);
		
		return a>0 && b>0?true:false;
	}

	
	
}








 

BbsServiceImpl

package bit.com.a.service.impl;

import java.util.List;

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

import bit.com.a.dao.BbsDao;
import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;
import bit.com.a.service.BbsService;

@Service
public class BbsServiceImpl implements BbsService {

	@Autowired
	BbsDao dao;
	
	@Override
	public List<BbsDto> getBbslist(BbsParam bbs) {		
		return dao.getBbslist(bbs);
	}

	@Override
	public int getBbsCount(BbsParam bbs) {		
		return dao.getBbsCount(bbs);		
	}

	@Override
	public BbsDto getBbs(int seq) {
		// TODO Auto-generated method stub
		return dao.getBbs(seq);
	}

	@Override
	public void readCount(int seq) {
		dao.readCount(seq);
	}

	@Override
	public boolean addBbs(BbsDto dto) {
		// TODO Auto-generated method stub
		return dao.addBbs(dto);
	}

	@Override
	public boolean updateBbs(BbsDto dto) {
		// TODO Auto-generated method stub
		return dao.updateBbs(dto);
	}

	@Override
	public boolean bbsdelete(int seq) {
		// TODO Auto-generated method stub
		return dao.bbsdelete(seq);
	}

	@Override
	public boolean answer(int seq, BbsDto dto) {
		// TODO Auto-generated method stub
		return dao.answer(seq, dto);
	}
	
	
}





BbsController

package bit.com.a.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;
import bit.com.a.service.BbsService;

@Controller
public class BbsController {
	
	private static Logger logger = LoggerFactory.getLogger(BbsController.class);
	
	
	
	@Autowired
	BbsService service;
	
	@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
	public String bbslist(Model model) {		
		model.addAttribute("doc_title", "글목록");
		return "bbslist.tiles";
	}

	@ResponseBody
	@RequestMapping(value = "bbslistData.do", method = RequestMethod.GET)
	public List<BbsDto> bbslistData(BbsParam param) {		
		//검색처리
		
		String choice = param.getChoice();
		String search = param.getSearch();
		int page = param.getPage();
		
		if(choice == null) {
			choice = "";
		}
		if(search == null) {
			search = "";
		}
		
		
		param.setChoice(choice);
		param.setSearch(search);
		
		
		// paging 처리
		int sn = param.getPage();
		int start = sn * 10 + 1;	// 1 	11
		int end = (sn + 1) * 10; 	// 10   20
		
		param.setStart(start);
		param.setEnd(end);
				
		List<BbsDto> list = service.getBbslist(param);		
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value = "bbslistCount.do", method = RequestMethod.GET)
	public int bbslistCount(BbsParam param) {
		int count = service.getBbsCount(param);
		return count;
	}
	
	@RequestMapping(value = "bbsdetail.do", method = RequestMethod.GET)
	public String bbsdetail(int seq, Model model) {
		
		BbsDto dto = service.getBbs(seq);
		service.readCount(seq);
		
		
		model.addAttribute("dedto", dto);
		model.addAttribute("doc_title", "디테일");
		
		return "bbsdetail.tiles";
	}
	
	@RequestMapping(value = "bbsWrite.do", method = RequestMethod.GET)
	public String bbsWrite(Model model) {
		logger.info("BbsController bbsWrite() " + new Date());
		
		model.addAttribute("doc_title", "글작성");
		
		return "bbswrite.tiles";
	}
	
	
	@RequestMapping(value = "bbsWriteAf.do", method = RequestMethod.POST)
	public String bbsWriteAf(BbsDto bbs) {
		
		boolean a = service.addBbs(bbs);
		
		if(a) {
			logger.info("글이 추가되었습니다 " + new Date());
			return "redirect:/bbslist.do";
		}
		logger.info("글이 추가 되지 않았습니다 " + new Date());
		
		return "redirect:/bbsWrite.do";
	}
	
	@RequestMapping(value = "updateBbs.do", method = RequestMethod.GET)
	public String updateBbs(Model model, int seq) {
		logger.info("BbsController updateBbs() " + new Date());
		
		BbsDto dto = service.getBbs(seq);
		model.addAttribute("dto", dto);
		model.addAttribute("doc_title", "게시물 수정");
		
		
		return "bbsupdate.tiles";
	}
	

	@RequestMapping(value = "updateBbsAf.do", method = RequestMethod.GET)
	public String updateBbsAf(Model model, BbsDto bbsdto) {
		logger.info("BbsController updateBbs() " + new Date());
		
		
		boolean a =service.updateBbs(bbsdto);
		
		if(a) {
			logger.info("글이 수정되었습니다 " + new Date());
			return "redirect:/bbslist.do";
		}
		logger.info("글이 수정되지않았습니다 " + new Date());
		
		
		return "redirect:/bbslist.do";
	}
	
	@RequestMapping(value = "answer.do", method = RequestMethod.GET)
	public String answer(Model model, int seq) {
		logger.info("BbsController answer() " + new Date());
		
		BbsDto dto = service.getBbs(seq);
		
		model.addAttribute("parentdto", dto);
		model.addAttribute("doc_title", "답글달기");
		
		
		return "answer.tiles";
	}
	
	@RequestMapping(value = "answerAf.do", method = RequestMethod.GET)
	public String answerAf(Model model, BbsDto dto) {
		logger.info("BbsController answerAf() " + new Date());
		
		System.out.println("컨트롤러 답글 확인" + dto.toString());
		
		
		boolean a = service.answer(dto.getSeq(), dto);

		
		
		return "redirect:/bbslist.do";
	}
	
	@RequestMapping(value = "deleteBbs.do", method = RequestMethod.GET)
	public String deleteBbs(int seq) {
		
		boolean a = service.bbsdelete(seq);
		
	

		if(a) {
			logger.info("글이 삭제되었습니다 " + new Date());
			return "redirect:/bbslist.do";
		}
		logger.info("글이 삭제되지않았습니다 " + new Date());
		
		return "redirect:/bbslist.do";
	}
	
}







728x90