IT/development

[mybatis] selectkey값 return(Oracle)

알 수 없는 사용자 2023. 5. 6. 23:39
반응형
<!--게시글 작성-->
    <insert id="insertBoard" parameterType="BoardVo">
        <selectKey keyProperty="boardNo" resultType="int" order="BEFORE">
            SELECT BOARD_NO_SEQ.NEXTVAL FROM DUAL
        </selectKey>
        /* 게시글 작성 */
        INSERT INTO BOARD
            (
                BOARD_NO
              , TITLE
              , CONTENT
              , WRITER
              , CREATE_DATE
            )
        VALUES
            (
                #{boardNo}
              , #{title}
              , #{content}
              , #{writer}
              , SYSDATE
            )
    </insert>
/* 게시글 등록(mapper interface) */
void insertBoard(BoardVo boardVo);
    /**
     * 게시글 등록(service)
     * @param boardVo
     * @throws Exception
     */
    public void regBoard(BoardVo boardVo) throws Exception{
        boardMapper.insertBoard(boardVo);
    }
/**
     * 게시글 등록 처리(controller)
     * @param boardVo
     * @return
     */
    @PostMapping("reg")
    public String reg(@ModelAttribute BoardVo boardVo,
                      @RequestParam("files") List<MultipartFile> files,
                      HttpServletRequest request) {

        try {
            boardService.regBoard(boardVo);
            //selectKey로 return한 boardNo가 vo에 세팅되어 있어서 get으로 꺼냄
            fileService.saveFile(file, boardVo.getBoardNo());
        } catch (IOException ie) {
            ie.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/";
    }
반응형