Spring Rest 2022/04/07

파일명 CustomerRestController.java

  • backend만 구현함. 화면구현X, vue.js 또는 react.js 연동
package com.example.restcontroller;

import java.util.HashMap;
import java.util.Map;

import com.example.dto.MemberDTO;
import com.example.mapper.MemberMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

// backend만 구현함. 화면구현X, vue.js 또는 react.js 연동
@RestController
@RequestMapping("/api/rest_customer")
public class CustomerRestController {

    @Autowired MemberMapper mMapper;


    // 127.0.0.1:9090/ROOT/api/rest_customer/join
    // {"uemail":"ggg", ...}
    @RequestMapping(
        value = "/join", 
        method = { RequestMethod.POST },
        consumes = { MediaType.ALL_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE})
    public Map<String, Object> customerJoinPost(
        @RequestBody MemberDTO member
    ){
        BCryptPasswordEncoder bcpe = new BCryptPasswordEncoder();
        member.setUpw( bcpe.encode( member.getUpw()));
        member.setUrole("CUSTOMER");

        int ret = mMapper.memberJoin( member);
        Map<String, Object> map = new HashMap<>();

        map.put("status",0);
        if(ret == 1){
            map.put("status",200);
        }
        return map;
    }    
}

파일명 BoardRestController.java

package com.example.restcontroller;

import java.util.HashMap;
import java.util.Map;

import com.example.dto.BoardDTO;
import com.example.mapper.BoardMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/board")
public class BoardRestController {

    @Autowired BoardMapper bMapper;

    // 127.0.0.1:9090/ROOT/api/board/insert/
    // {"btitle":"aaa", "bcontent":"bbb"}
    @RequestMapping(
        value = "/insert", 
        method = { RequestMethod.POST },
        consumes = { MediaType.ALL_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE})
    public Map<String, Object> boardInsertPost(
        @RequestBody BoardDTO board
    ){        
        Map<String, Object> map = new HashMap<>();
        map.put("status",0);

        int ret = bMapper.insertBoardOne(board);
        if(ret == 1){
            map.put("status",200);
        }
        return map;
    }   

    // 127.0.0.1:9090/ROOT/api/board/delete/
    // {"bno": 3}
    @RequestMapping(
        value = "/delete", 
        method = { RequestMethod.DELETE },
        consumes = { MediaType.ALL_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE})
    public Map<String, Object> boardDeletePost(
        @RequestParam(name = "bno") long bno
    ){        
        Map<String, Object> map = new HashMap<>();
        map.put("status",0);

        int ret = bMapper.deleteBoardOne(bno);
        if(ret == 1){
            map.put("status",200);
        }
        return map;
    }   
    
    // 127.0.0.1:9090/ROOT/api/board/update/
    // {"bno":2 , "btitle":"ccc", "bcontent": "ccc"}
    @RequestMapping(
        value = "/update", 
        method = { RequestMethod.PUT },
        consumes = { MediaType.ALL_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE})
    public Map<String, Object> boardUpdatePost(
        @RequestBody BoardDTO board
    ){        
        Map<String, Object> map = new HashMap<>();
        map.put("status",0);

        int ret = bMapper.updateBoardOne(board);
        if(ret == 1){
            map.put("status",200);
        }
        return map;
    }   
}

파일명 BoardMapper.java

package com.example.mapper;

import com.example.dto.BoardDTO;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface BoardMapper {

    @Insert({
        "INSERT INTO BOARD(BNO, BTITLE, BCONTENT, BHIT, BREGDATE, BTYPE, UEMAIL )",
        " VALUES( SEQ_BOARD_NO.NEXTVAL, #{brd.btitle}, #{brd.bcontent, jdbcType=CLOB},", 
        " #{brd.bhit}, CURRENT_DATE, #{brd.btype}, #{brd.uemail})"
    })
    public int insertBoardOne(@Param(value = "brd") BoardDTO board);

    @Delete({
        "DELETE FROM BOARD WHERE BNO = #{bno}"
    })
    public int deleteBoardOne(@Param(value = "bno") long bno);

    @Update({
        "UPDATE BOARD SET BTITLE = #{brd.btitle}, BCONTENT = #{brd.bcontent} ",
            " WHERE BNO = #{brd.bno}"
    })
    public int updateBoardOne(@Param(value = "brd") BoardDTO board);

}

좋은 웹페이지 즐겨찾기