ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • @Param 어노테이션과 댓글 목록
    Spring_FrameWork 2018. 12. 4. 12:04

    댓글의 목록과 페이징 처리는 기존의 게시물 페이징 처리와 유사하지만 추가적으로 특정한 게시물들의 댓글들만을 대상으로 하기 때문에 추가로 게시물의 번호가 필요하게 된다 .


    Mybatis는 두개 이상이 데이터를 파라미터로 전달하기 위해서는 

    1)  별도의 객체로 구성하거나  

    2) Map을 이용하는 방식 

    3)@Param을 이용해서 이름을 사용하는 방식이다 . 


    @Param의 속성값은 Mybatis  에서 SQL을 이용할 때 '#{ }'의 이름으로 사용이 가능합니다. 


    페이징 처리는 기존과 동일하게  Criteria를 이용한다 .

    여기에 추가적을 해당 게시물의 번호는 파라미터를 전달하도록 ReplyMapper를 구성합니다. 


    ReplyMapper 인터페이스


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package org.zerock.mapper;
     
    import java.util.List;
     
    import org.apache.ibatis.annotations.Param;
    import org.zerock.domain.Criteria;
    import org.zerock.domain.ReplyVO;
     
    public interface ReplyMapper {
        //리플 추가
        void insert(ReplyVO vo);
        //리플 조회 처리
        public ReplyVO read(Long bno);
        //삭제
        public int delete(Long bno);
        //수정 
        public int update (ReplyVO reply);
        //Criteria
        public List<ReplyVO> getListWithPageing(@Param("cri") Criteria cri,@Param("bno") Long bno);
     
    }
     
    cs

    XML로 처리할때는 cri와 bno를 모두 사용할수있다 .


    특정게시물의 댓글을 가져오는 것을 작성한다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <?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="org.zerock.mapper.ReplyMapper">
        <insert id="insert">
     
            insert into tbl_reply (rno, bno, reply, replyer)
            values (seq_reply.nextval, #{bno}, #{reply}, #{replyer})
     
        </insert>
        
        <select id="read" resultType ="org.zerock.domain.ReplyVO">
            select * from tbl_reply where rno = #{rno}
        </select>
        
        <delete id = "delete">
            delete from tbl_reply where rno =#{rno}
        </delete>
        
        <update id="update">
            update tbl_reply set reply = #{reply}, updatedate = sysdate where rno = #{rno}
        </update>
        
        <select id="getListWithPaging" resultType="org.zerock.domain.ReplyVO">
        
        select rno, bno, reply, replyer, replyDate, updatedate
        from tbl_reply
        where bno =#{bno}
        order by rno asc
        
        </select>
        
    </mapper>
    cs


    XML에서 '#{bno}'가 @Param("bno")와 매칭되어서 사용되는 점에 유의


    테스트코드로 현재 데이터 베이스에 추가 됭있는게시물번호를  확인 

    1
    2
    3
    4
    5
    6
    7
    8
    9
        //댓글목록 
        @Test
        public void testList() {
            Criteria cri = new Criteria();
            //16899L
            List<ReplyVO> replies = mapper.getListWithPaging (cri,bnoArr[0]);
            
            replies.forEach(reply -> log.info(reply));
        }
    cs


    'Spring_FrameWork' 카테고리의 다른 글

    특정 게시물의 댓글 목록 확인  (0) 2018.12.08
    서비스영역과 Controller처리  (0) 2018.12.06
    오류)junit test log가 안찍힐때  (0) 2018.12.04
    Ajax 댓글 처리  (0) 2018.11.29
    @RestController에서 파라미터  (0) 2018.11.28
Designed by Tistory.