[스프링 부트] 게시판 만들기 프로젝트 - 7. DTO, Mapper

2023. 4. 13. 00:34스프링부트 프로젝트

1. DTO

DTO(Data Transfer Object): 데이터 전송 객체로, 계층간 데이터 교환을 위해 사용

import java.util.Date;

import lombok.Data;

@Data
public class MemberDto {
	private String id;
	private String name;
	private String pw;
}

△ MemberDto.java

 

import java.util.Date;

import org.jsoup.nodes.Document;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

@Data
public class BoardDto {

	private Long postno;			
	private Long pgroup;		
	private Long depth;
	private Long step;
	private String id;
	private String title;
	private String content;
	private String status;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
	private Date wrtdate;
	private int commentCnt;

    private Document doc;
    
    private int beginPage;
    private int endPage;

    private String option;
    private String keyword;
    
    
}

△ BoardDto.java

 

2. Mapper

데이터베이스와 애플리케이션 간의 데이터를 변환하는 역할을 하는 객체입니다. 데이터베이스에서 조회한 결과를 객체로 변환하거나, 객체를 데이터베이스에 저장할 때 사용

일반적으로 MyBatis, Hibernate 등의 ORM 프레임워크에서 사용되며, 객체를 데이터베이스에 저장할 때는 JPA, Spring JDBC 등의 기술에서 사용

 

△ *Mapper.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.jeong.board.mapper.BoardMapper">

	<!-- 최근 게시물 번호 -->
	<select id="selectBoardMax" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="Long">
	 SELECT max(postno)
	 FROM board
	</select>
	
	<!-- 해당 게시물의 최근 댓글 번호 -->
 	<select id="selectCommentMax" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="Long">
 	 SELECT max(step) 
 	 FROM board 
 	 WHERE pgroup = #{pgroup}
 	</select>
 	
	<!-- 게시물 & 댓글 & 대댓글 작성 -->
	<insert id="insertBoard" 
     parameterType="com.jeong.board.domain.BoardDto">
	 INSERT INTO board(pgroup, depth, step, id, title, content)
	 VALUES(#{pgroup}, #{depth}, #{step}, #{id}, #{title}, #{content})
	</insert>
	
	<!-- 게시물 & 댓글 & 대댓글 수정 -->
	<update id="updateBoard" parameterType="com.jeong.board.domain.BoardDto">
	 UPDATE board
     SET title = #{title}
        ,content = #{content}
     WHERE postno = #{postno}
     AND id = #{id}
	</update>	
	
	<!-- 게시물 & 댓글 & 대댓글 삭제  -->
	<update id="deleteBoard" parameterType="com.jeong.board.domain.BoardDto">
	 UPDATE board
	 SET status = 'N'
 	 WHERE postno = #{postno}
 	 AND id = #{id}
	</update>
	
	<!-- 댓글 조회 -->
	<select id="selectCommentList" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="com.jeong.board.domain.BoardDto">
 	 SELECT postno, pgroup, depth, step, id, content, wrtdate
 	 FROM board
 	 WHERE depth != 1
 	 AND status = 'Y'
 	 AND pgroup = #{postno} 
 	 ORDER BY wrtdate DESC
 	</select>	 	

	<!-- 게시물 한개 조회 -->
	<select id="selectBoardDetail" 
     parameterType="long" resultType="com.jeong.board.domain.BoardDto">
 	 SELECT *
 	 FROM board
 	 WHERE postno = #{postno}
 	 AND depth = 1
 	 AND status = 'Y'
	</select>

	<!-- 게시물 목록 조회 -->
	<select id="selectBoardList"
     parameterType="com.jeong.board.domain.BoardDto" resultType="com.jeong.board.domain.BoardDto">
 	 SELECT *
 	 FROM (
 	  select a.*, ROW_NUMBER() OVER (ORDER BY wrtdate DESC) AS rn
 	  from board a
 	  where depth = 1
 	  <if test='option == "all"'>
 	   and (title like CONCAT('%', #{keyword}, '%')
 	   or content like CONCAT('%', #{keyword}, '%')
 	   or id like CONCAT('%', #{keyword}, '%'))
 	  </if>
 	  <if test='option == "title"'>
  	   and title like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  <if test="option == 'content'">
 	   and content like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  <if test="option == 'id'">
 	   and id like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  and  status = 'Y'
   	  ) b
 	  WHERE rn BETWEEN #{beginPage} AND #{endPage}
 	  ORDER BY wrtdate DESC, postno desc, rn
	</select>

	<select id="selectBoardTotalCount" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="int">
 	  SELECT COUNT(*)
 	  FROM board
 	  WHERE depth = 1
 	  <if test='option == "all"'>
 	   and title like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  <if test='option == "title"'>
 	   and title like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  <if test="option == 'content'">
 	   and content like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  <if test="option == 'id'">
 	   and id like CONCAT('%', #{keyword}, '%')
 	  </if>
 	  AND  status = 'Y'
	</select>

	<!-- mypost 조회  -->
	<select id="selectMypost" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="com.jeong.board.domain.BoardDto">
 	  SELECT *
 	  FROM (
 	   select a.*, ROW_NUMBER() OVER (ORDER BY wrtdate DESC) AS rn
 	   from board a
 	   where depth = 1
 	   <if test='option == "all"'>
 	    and (title like CONCAT('%', #{keyword}, '%')
 	    or content like CONCAT('%', #{keyword}, '%')
 	    or id like CONCAT('%', #{keyword}, '%'))
 	   </if>
 	   <if test='option == "title"'>
 	    and title like CONCAT('%', #{keyword}, '%')
 	   </if>
 	   <if test="option == 'content'">
 	    and content like CONCAT('%', #{keyword}, '%')
 	   </if>
 	   <if test="option == 'id'">
 	    and id like CONCAT('%', #{keyword}, '%')
 	   </if>
        and  status = 'Y'
        and id = #{id}
 	   ) b
 	   WHERE rn BETWEEN #{beginPage} AND #{endPage}
 	   ORDER BY wrtdate DESC, rn
	</select>
	
	<select id="selectMyPostTotalCount" 
     parameterType="com.jeong.board.domain.BoardDto" resultType="int">
	 SELECT COUNT(*)
	 FROM board
	 WHERE depth = 1
	 <if test='option == "all"'>
 	  and title like CONCAT('%', #{keyword}, '%')
	 </if>
	 <if test='option == "title"'>
      and title like CONCAT('%', #{keyword}, '%')
  	 </if>
     <if test="option == 'content'">
      and content like CONCAT('%', #{keyword}, '%')
	 </if>
	 <if test="option == 'id'">
      and id like CONCAT('%', #{keyword}, '%')
	 </if>
     AND  status = 'Y'
     AND  id = #{id}
	</select>
	
	<!-- 댓글 수 조회 -->
	<select id="commentCnt"  
     parameterType="com.jeong.board.domain.BoardDto" resultType="int">
	 SELECT count(*)
	 FROM board 
	 WHERE status = 'Y'
	 AND pgroup = #{postno}
	 AND depth != 1
	 ORDER BY pgroup, step, depth;  
	</select>

</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.jeong.board.mapper.MemberMapper">
	<!-- 회원 가입 -->
	<insert id="addMember" 
     parameterType="com.jeong.board.domain.MemberDto">
		INSERT INTO member(id, pw, name)
		VALUES (#{id}, #{pw},
		#{name})
	</insert>

	<!-- 아아디 중복 확인 -->
	<select id="getId" 
     parameterType="com.jeong.board.domain.MemberDto" resultType="int">
		SELECT IFNULL(COUNT(*), 0) 
		FROM member
		WHERE id = #{id}
	</select>

	<!-- 로그인 -->
	<select id="getSelectOne  
     parameterType="com.jeong.board.domain.MemberDto"
		resultType="com.jeong.board.domain.MemberDto">
		SELECT * 
		FROM member
		WHERE id = #{id}
	</select>
	
</mapper>

△ MemberMapper.xml

 

 

△ *Mapper.java 위치

 

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.jeong.board.domain.BoardDto;

@Mapper
public interface BoardMapper {

	// 최근 게시물 번호
	public Long selectBoardMax();
	// 최근 댓글 번호
	public Long selectCommentMax(BoardDto bdto);
	//게시물 등록 
	public int insertBoard(BoardDto bdto);
	// 게시물 한개 조회 
	public BoardDto selectBoardDetail(Long postno);
	// 게시물 목록 조회 
	public List<BoardDto> selectBoardList(BoardDto bdto);
	// 댓글 리스트 
	public List<BoardDto> selectCommentList(BoardDto bdto);
	// 게시글의 개수를 조회 
	public int selectBoardTotalCount(BoardDto bdto);
	// 게시물 수정 
	public int updateBoard(BoardDto bdto);
	// 게시물 삭제 
	public int deleteBoard(BoardDto bdto);
	// 내가 쓴 글 조회 
	public List<BoardDto> selectMypost(BoardDto bdto);
	// 내가 쓴 글의 개수를 조회 
	public int selectMyPostTotalCount(BoardDto bdto);
	// 댓글 수 조회 
	public int commentCnt(Long postno);
}

△ BoardMapper.java

 

import org.apache.ibatis.annotations.Mapper;

import com.jeong.board.domain.MemberDto;

@Mapper
public interface MemberMapper {
	// 회원가입
	int addMember(MemberDto mDto);
	// 아이디 중복확인
	int getId(MemberDto mDto);
	// 로그인
	public MemberDto getSelectOne(String id);
	
}

△ MemberMapper.java