스프링 기초_댓글처리1

bitna's study note·2022년 7월 29일

스프링

목록 보기
37/54

7월 30일

Rest방식을 가장 많이 사용하는 곳은 브라우저나 모바일 앱 등에서 Ajax를 이용해서 호출하는 방식이다.

1.댓글 처리를 위한 영속 영역
(1)테이블 생성과 처리

create SEQUENCE seq_reply; --순차적으로 번호를 줌

create table t_reply(
rno NUMBER(10,0),
bno NUMBER(10,0) not null,
reply VARCHAR2(1000) not null,
replyer VARCHAR2(50) not null,
replydate date default sysdate,
updatedate date default sysdate
);
alter table t_reply add constraint pk_reply primary key(rno); 
alter table t_reply add constraint fk_reply_board foreign key(bno) references t_board(bno);
commit;

(2)ReplyVO 클래스 만들기

package com.keduit.domain;

import java.util.Date;

public class ReplyVO {
	
	private Long rno;
	private Long bno;
	
	private String reply;
	private String replyer;
	private Date replyDate;
	private Date updateDate;

}

(3)ReplyMapper인터페이스 만들기

package com.keduit.mapper;

public interface ReplyMapper {

}

(4)ReplyMapper.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.keduit.mapper.ReplyMapper">
  </mapper>

(5)ReplyMapper 테스트
CRUD작업을 테스트하기 전에 t_reply테이블이 t_bpard테이블과 FK의 관계로 처리되어 있다는 점을 기억해야한다.

package com.keduit.mapper;

@RunWith(SpringJUnit4ClassRunner.class)//이 클래스로 잘돌아가는지 test해볼꺼야
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")//참고할 경로는 여기야
@Log4j //화면에 뿌려주는건 Log4j가 관리 할거야 
public class ReplyMapperTest {
	
	@Setter(onMethod_ = @Autowired)
	private ReplyMapper mapper;
	
	@Test
	public void testMapper() {
		log.info("mapper: "+mapper);
	}

}

2.CRUD 작업
(1)Create
(1-1)Create를 위한 ReplyMapper인터페이스 추가하기

package com.keduit.mapper;

import com.keduit.domain.ReplyVO;

public interface ReplyMapper {
	
	public int insert(ReplyVO vo);

}

(1-2)Create를 위한 ReplyMapper.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.keduit.mapper.ReplyMapper">
  
  <insert id="insert">
  	insert into t_reply (rno, bno, reply, replyer)
  	values (seq_reply.nextval, #{bno}, #{reply}, #{replyer})
  </insert>
  
  </mapper>

(1-3)Create를 위한 ReplyMapperTest 작성

package com.keduit.mapper;

@RunWith(SpringJUnit4ClassRunner.class)//이 클래스로 잘돌아가는지 test해볼꺼야
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")//참고할 경로는 여기야
@Log4j //화면에 뿌려주는건 Log4j가 관리 할거야 
public class ReplyMapperTest {
	
	private Long[] bnoArr= {81L,62L,55L,54L,52L};
	
	@Setter(onMethod_ = @Autowired)
	private ReplyMapper mapper;
	
	@Test
	public void testCreate() {
		
		IntStream
		.range(1, 10)
		.forEach(i->{
			ReplyVO vo = new ReplyVO();
			
			vo.setBno(bnoArr[i%5]);
			vo.setReply("댓글 테스트중이다요"+i);
			vo.setReplyer("졸리당당"+i);
			
			mapper.insert(vo);
			
		});
	}

}

(2)Read
(2-1)Read를 위한 ReplyMapper인터페이스 추가하기

package com.keduit.mapper;

public interface ReplyMapper {
	
	public ReplyVO read(Long bno); 

}

(2-2)Read를 위한 ReplyMapper.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.keduit.mapper.ReplyMapper">
  
  <select id="read" resultType="com.keduit.domain.ReplyVO">
  	select*from t_reply where rno=#{rno}
  </select>
  
  </mapper>

(2-3)Read를 위한 ReplyMapperTest 작성

package com.keduit.mapper;

@RunWith(SpringJUnit4ClassRunner.class)//이 클래스로 잘돌아가는지 test해볼꺼야
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")//참고할 경로는 여기야
@Log4j //화면에 뿌려주는건 Log4j가 관리 할거야 
public class ReplyMapperTest {
	
	private Long[] bnoArr= {81L,62L,55L,54L,52L};
	
	@Setter(onMethod_ = @Autowired)
	private ReplyMapper mapper;
	
	@Test
	public void testRead() {
		Long targetRno=65L;
		
		ReplyVO vo= mapper.read(targetRno);
		
		log.info(vo);
	}
}

(3)Delete
(3-1)Delete를 위한 ReplyMapper인터페이스 추가하기

package com.keduit.mapper;

public interface ReplyMapper {
	
	public int delete(int rno);
}

(3-2)Delete를 위한 ReplyMapper.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.keduit.mapper.ReplyMapper">
  
  <delete id="delete">
  	delete from t_reply where rno=#{rno}
  </delete>
  
  </mapper>

(3-3)Delete를 위한 ReplyMapperTest 작성

package com.keduit.mapper;

@RunWith(SpringJUnit4ClassRunner.class)//이 클래스로 잘돌아가는지 test해볼꺼야
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")//참고할 경로는 여기야
@Log4j //화면에 뿌려주는건 Log4j가 관리 할거야 
public class ReplyMapperTest {
	
	private Long[] bnoArr= {81L,62L,55L,54L,52L};
	
	@Setter(onMethod_ = @Autowired)
	private ReplyMapper mapper;
	
	@Test
	public void testDelete() {
		int targetRno=47;
		mapper.delete(targetRno);
	}
}

(4)update
(4-1)update를 위한 ReplyMapper인터페이스 추가하기

package com.keduit.mapper;

import com.keduit.domain.ReplyVO;

public interface ReplyMapper {
	
	public int update(ReplyVO reply);

}

(4-2)update를 위한 ReplyMapper.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.keduit.mapper.ReplyMapper">
  
  <update id="update">
  	update t_reply set reply= #{reply}, updatedate=sysdate where rno=#{rno}
  </update>
  
  </mapper>

(4-3)update를 위한 ReplyMapperTest 작성

package com.keduit.mapper;

@RunWith(SpringJUnit4ClassRunner.class)//이 클래스로 잘돌아가는지 test해볼꺼야
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")//참고할 경로는 여기야
@Log4j //화면에 뿌려주는건 Log4j가 관리 할거야 
public class ReplyMapperTest {
	
	private Long[] bnoArr= {81L,62L,55L,54L,52L};
	
	@Setter(onMethod_ = @Autowired)
	private ReplyMapper mapper;
	
	@Test
	public void testUpdate() {
		Long targetRno =52L;
		ReplyVO vo=mapper.read(targetRno);
		vo.setReply("수정했음...7월30일..");
		int count=mapper.update(vo);
		log.info("update count:"+count);
	}
}
profile
좋은개발자가 되기위한 삽질기록 노트

0개의 댓글