스프링부트 게시판 만들기 제5강. 게시글 저장 Service

스프링 부트 게시판 만들기

제5강 게시글 저장 Service 만들기

 

 

너무 여유를 부렸나... 아니, 마음의 여유가 없다.

오늘은 비즈니스 로직이 처리되는 Service을 만들 시간이다.

 

	//---* 저장
	@RequestMapping("boardarticlein.sd")
	public String in( HttpServletRequest request, @RequestParam Map<String, Object> param, ModelMap model ){

		try{
			boardarticleSvc.in( request, param, model );       // 여기에서 BoardarticleSvcImpl로 흐름이 넘어 감.
		}catch( Exception e ){
			logger.error( e.toString(), e );
		}

		return "redirect:/boardarticle/boardarticlelist.sd";
	}

 

Controller의 boardarticleSvc.in( request, param, model ); 메소드에서 BoardarticleSvcImple로 흐름이 넘어가는 것이다.

그리고 이 사이에는(?) BoardarticleSvc라는 인터페이스가 있다.

 

public interface BoardarticleSvc {

	//---*fo 등록
	public boolean add( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo 삭제
	public boolean del( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo 수정
	public boolean edit( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo 저장
	public boolean in( HttpServletRequest request, Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo 리스트map
	public boolean list( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo one row
	public boolean one( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo Map 업데이트
	public boolean up( Map<String, Object> param, ModelMap model )throws Exception;

	//---*fo 보기
	public boolean view( Map<String, Object> param, ModelMap model )throws Exception;

}

 

인터페이스가 꼭 있어야 할 필요는 없다.

인터페이스를 사용한 이유는 나중의 변화가 생겼을 때 잘 대처하기 위해서다.

이해하기 쉽도록 Map을 예로 들겠다.

Map<String, Object> map = new HashMap<>();

보통 위와 같이 선언을 한다.

Map은 인터페이스다. 나중에 HashMap보다 더 좋은 map이 등장한다면

Map<String, Object> map = new GoodMap<>();

위와 같이 사용하게 된다.

인터페이스인 Map을 상속받았기 때문에, Map의 모든 Method를 구현해야 한다.

같은 메소드명을 그대로 사용하기 때문에 코드를 변경하지 않아도 된다는 이점이 있다.

그래서 인터페이스를 사용해주는 것이 좋다.

 

	@Override
	public boolean in( HttpServletRequest request, Map<String, Object> param, ModelMap model )throws Exception{

		Map<String, Object> svcMap = new HashMap<>();
		
		//---* 게시글 저장
		svcMap.put( "title", param.get("title") );
		svcMap.put( "content", param.get("content") );
		svcMap.put( "readcnt", 0 );
		svcMap.put( "commentcnt", 0 );
		svcMap.put( "recommendcnt", 0 );
		svcMap.put( "groupnum", 0 );
		svcMap.put( "levelnum", 0 );
		svcMap.put( "stepnum", 0 );
		svcMap.put( "inip", request.getRemoteAddr() );

		boardarticleDao.in( svcMap );
		//--- 게시글 저장 끝
		
		//---* 그룹번호 업데이트
		int seq = Integer.parseInt( svcMap.get( "seq" ).toString() );
		svcMap.put( "seq", seq );
		svcMap.put( "groupnum", seq );
		
		boardarticleDao.up( svcMap );
		//---* 그룹번호 업데이트 끝
		
		return true;
	}

 

보통은 DTO나 VO에 데이터를 담아서 저장을 하는 것이 일반적이지만,

나는 Map에 담는다. 가장 편하기 때문에,

Type Safe하지 않다고 할 수 있지만, Type Safe하게 처리 하면 된다.

Form에 담겨 전달 된 Parameter는 request, param 변수에 담겨있다.

request가 있다면 param은 굳이 사용할 필요는 없다.

ip만 request.getRemoteAdd()로 받고 나머지는 param을 통해서

svcMap에 담아서 Dao로 전달 한다.

title, content, ip 정보만 전달받고 나머지는 직접 입력하는 값들이다.

groupnum, levelnum, stepnum은 모두 답글 처리를 위해 필요한 column이다.

 

boardarticleDao.in( svcMap );

int seq = Integer.parseInt( svcMap.get( "seq" ).toString() );

 

나중에 다시 확인하겠지만, MyBatis에서 저장 시에 primay key인 seq를 자동으로 리턴처리 하도록 되어 있다.

전달된 Map(svcMap)에 담긴 seq를 변수에 담아서 그룹번호를 업데이트 시킨다.

 

svcMap.put( "seq", seq );

svcMap.put( "groupnum", seq );

boardarticleDao.up( svcMap );

 

그룹번호는 답글 처리를 위해서 필요하다(원본 글과 답글은 한 그룹이 되는 것). insert 시 그룹 번호를 알 수 없기 때문에 seq를 가져와 update 시키는 것이다.

Controller와 jsp 그리고 Service로 이어지는 흐름까지 확인한 것 같다.

다음은 Dao와 mybatis를 통해서 실제 database에 insert 되는 과정을 확인하면 되겠다.

공유하기:

스프링부트 카테고리 글 :

0 Comments

Comment