본문 바로가기

Archived(Programming)/Spring #1(기초)

Spring_data 처리2(글 작성)

# JSP Form 태그 변경

register.jsp


<form role="form" method="post" action="register.do">
	<div class="box-body">
		<div class="form-group">
			<label for="exampleInputEmail1">Title</label> 
			<input type="text"
				name='title' class="form-control" placeholder="Enter Title">
		</div>
		<div class="form-group">
			<label for="exampleInputPassword1">Content</label>
			<textarea class="form-control" name="content" rows="3"
				placeholder="Enter ..."></textarea>
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">Writer</label> 
			<input type="text"
				name="writer" class="form-control" readonly value="${loginUser.id}">
		</div>
	</div>
	<!-- /.box-body -->

	<div class="box-footer">
		<button type="submit" class="btn btn-primary">Submit</button>
	</div>
</form>

	<div class="box-footer">
		<button type="submit" class="btn btn-primary">Submit</button>
	</div>
</form>

 

CF) @RequestParam(value="title") String title > String title 도 가능

통해서 이렇게 받아올 수 있음, 3개를 던져주므로 Map을 통해 Mybatis에 던져주기

# 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.sinc.intern.board">
	<select id="list" resultType="board">
		SELECT 	SEQ, TITLE, CONTENT, WRITER, TO_CHAR(REGDATE, 'YYYY-MM-DD') AS REGDATE, VIEWCNT
		FROM	INTERN_BOARD_TBL 
	</select>
	
	<insert id="register" parameterType="board">
		INSERT INTO INTERN_BOARD_TBL
		VALUES( BOARD_SEQ.NEXTVAL, #{title}, #{content}, #{writer}, DEFAULT, DEFAULT)
	</insert>
</mapper>  

# BoardDAO Register 구현

package com.sinc.intern.board.model.sql;

import java.util.List;

public interface BoardDao {
	public List<Object> listRow();
	public int 			registerRow(Object obj);
}
package com.sinc.intern.board.model.sql;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

@Repository("boardD")
public class BoardDaoImpl implements BoardDao {
	
	// Session 생성
	@Resource(name="sqlSession")
	private SqlSession session;
	public static final String PREFIX_B = "com.sinc.intern.board";
	
	@Override
	public List<Object> listRow() {
		System.out.println("Board DAO List");
		
		// selectList
		List<Object> result = session.selectList(PREFIX_B +".list");
		System.out.println("DAO Board Select result : " + result);
		return result;
	}

	@Override
	public int registerRow(Object obj) {
		System.out.println("Board DAO Insert:" + obj);
		
		// insert
		return session.insert(PREFIX_B + ".register", obj);
	}
}

# BoardService Register 구현

package com.sinc.intern.board.service;

import java.util.List;

public interface BoardService {
	public List<Object> list();
	public int			register(Object obj);
}
package com.sinc.intern.board.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sinc.intern.board.model.sql.BoardDao;

@Service("boardS")
public class BoardServiceImpl implements BoardService{

	@Resource(name="boardD")
	private BoardDao dao;

	// Service에서 DAO 호출 후 메서드 수행
	@Override
	public List<Object> list() {
		System.out.println("Board get list");
		return dao.listRow();
	}

	@Override
	public int register(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("Board service insert data");
		return dao.registerRow(obj);
	}

}

# BoardController 설정

package com.sinc.intern.board.ctrl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sinc.intern.board.model.vo.BoardVO;
import com.sinc.intern.board.service.BoardService;

@Controller
public class BoardCtrl {
	@Resource(name="boardS")
	private BoardService service;
	
	// HttpSession으로 받아오기
	@RequestMapping("/listPage.do")
	public String login(HttpSession session) {
		System.out.println("board result ");
		List<Object> board = service.list();
		if(board != null) {
			session.setAttribute("list", board) ;
		}
		return "board/listPage";
	}
	
	// 작성창 띄우기
	@RequestMapping("/registerForm.do")
	public String registerForm() {
		System.out.println("Board RegisterForm");
		return "board/register";
	}
	// 실제 글 등록(BoardVO로 받을 수 있다)
	@RequestMapping("/register.do")
	public String register(BoardVO obj) {
		System.out.println("Board Register");
		int result = service.register(obj);
		
		// 성공(데이터 정상반환)
		if(result == 1) {
			System.out.println("Board insert succeess");
			return "redirect:/listPage.do";
		}
		// 실패
		else {
			System.out.println("Board insert fail");
			return "board/register";
		}
	}
}

 

정상적으로 글 작성 완료

'Archived(Programming) > Spring #1(기초)' 카테고리의 다른 글

Spring_data 처리4(글 삭제)  (0) 2020.02.07
Spring_data 처리3(글 읽기)  (1) 2020.02.06
Spring_data 처리(글 목록)  (0) 2020.02.06
Spring_Mybatis 연동  (0) 2020.02.04
Web_0204  (0) 2020.02.04