Back-End/Java

[Spring] Legacy Project 게시판 구현하기

CJun 2021. 6. 10. 23:48
반응형
BoardVO.java

 

package com.example.domain;

import java.util.Date;

import lombok.Data;

@Data
public class BoardVO {
	
	private int num;
	private String mbrid;
	private String title;
	private String content;
	private Date regDate;
	private int readcount;
	private String ip;
	private int reRef;  // 글 그룹 번호
	private int reLev;  // 글 들여쓰기 레벨
	private int reSeq;  // 글 그룹 내에서의 순번
}

 

BoardMapper.java :
Controller에서 기능을 수행할 함수

 

package com.example.mapper;

import java.util.List;

import com.example.domain.BoardVO;

public interface BoardMapper {

	
	int insert(BoardVO boardVO); // 글 한개 등록하기
	
	int getTotalCount(); // 전체 글 개수 가져오기
	
	int nextBoardNum(); // 다음 insert할 글번호 생성하기
	
	void updateReadcount(int num); // 글번호에 해당하는 글의 조회수 1 증가시키기
	
	BoardVO getBoard(int num); // 글번호에 해당하는 글 한개 가져오기
	
	List<BoardVO> getBoards();
	
	int deleteBoard(int num); // 글번호에 해당하는 글 한개 삭제하기
	
	int deleteAll();
}

 

BoardMapper.xml :
SQL 실행

 

<?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.example.mapper.BoardMapper">

	<insert id="insert">
		<![CDATA[
		INSERT INTO board (num, mbrid, title, content, reg_date, readcount, ip, re_ref, re_lev, re_seq) 
		VALUES (#{num}, #{mbrid}, #{title}, #{content}, #{regDate}, #{readcount}, #{ip}, #{reRef}, #{reLev}, #{reSeq})
		]]>
	</insert>
	
	<select id="getTotalCount" resultType="int">
		<![CDATA[
			SELECT COUNT(*) FROM board
		]]>
	</select>
	
	<select id="nextBoardNum" resultType="int">
		<![CDATA[
			SELECT NVL(MAX(num), 0) + 1 AS bnum FROM board
		]]>
	</select>
	
	<update id="updateReadcount">
		<![CDATA[
			UPDATE board
			set readcount = readcount + 1
			WHERE num = #{num};
		]]>
	</update>
	
	<!-- boardVO -->
	<select id="getBoard" resultType="boardVO">
		<![CDATA[
			SELECT * FROM board WHERE num = #{num};
		]]>
	</select>
	
	<select id="getBoards" resultType="boardVO">
		<![CDATA[
			SELECT * FROM board ORDER BY num DESC
		]]>
	</select>
	
	<delete id="deleteBoard">
		<![CDATA[
			DELETE FROM board
			WHERE num = #{num}
		]]>
	</delete>
	
	<delete id="deleteAll">
		<![CDATA[
		DELETE FROM board
		]]>
	</delete>
	
</mapper>

 

BoardService.java :
트랜잭션을 위해서 Service 설정

 

package com.example.service;

import java.util.List;

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

import com.example.domain.BoardVO;
import com.example.mapper.BoardMapper;

import lombok.Setter;

@Service
// @Transactional
public class BoardService {

	@Setter(onMethod_ = @Autowired)
	private BoardMapper boardMapper;
	
	public int register(BoardVO boardVO) {
		return boardMapper.insert(boardVO);
		
	}
	
	// 전체 글 개수 가져오기
	public int getTotalCount() {
		return boardMapper.getTotalCount();
	}
	
	public int nextBoardNum() {
		return boardMapper.nextBoardNum();
	}
	
	public void modifyReadcount(int num) {
		boardMapper.updateReadcount(num);
	}
	
	public BoardVO getBoard(int num) {
		return boardMapper.getBoard(num);
	}
	
	public List<BoardVO> getBoards() {
		return boardMapper.getBoards();
	}
	
	public int removeBoard(int num) {
		return boardMapper.deleteBoard(num);
	}
	
	public int removeAll() {
		return boardMapper.deleteAll();
	}
	
	
}

 

src/main/test => BoardService.java :
글이 들어오는지 Test

 

package com.example.service;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.example.domain.BoardVO;

import lombok.Setter;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class BoardServiceTests {

	@Setter(onMethod_ = @Autowired)
	private BoardService boardService;
	
	@Test
	public void testRegister() {
		
		boardService.removeAll();
		
		// 샘플 주 글 여러개 생성하기
		for(int i = 0; i < 100; i++) {
			BoardVO boardVO = new BoardVO();
			
			int num = boardService.nextBoardNum(); // insert할려는 글번호 가져오기
					
			boardVO.setNum(num);
			boardVO.setMbrid("aaa");
			boardVO.setTitle("글제목" + num);
			boardVO.setContent("글내용" + num + "\n안녕하세요.!!");
			boardVO.setRegDate(new Date());
			boardVO.setReadcount(0);
			boardVO.setIp("127.0.0.1");
			boardVO.setReRef(num); // 주 글은 글그룹번호가 자신의 글번호와 동일하다.
			boardVO.setReLev(0); // 주 글은 들여쓰기 레벨이 0 (들여쓰기 안함)
			boardVO.setReSeq(0); // 주 글은 같은 글그룹 안에서 순번이 0 (오름차순 정렬시 최상단 정렬을 위해)
			
			System.out.println(boardVO);
			
			boardService.register(boardVO);
			
		} //for
		
		
		
	} //testRegister()
}
SELECT * FROM board;

반응형