Java Spring (3)

taehoyoon·2023년 8월 7일
0

Java Spring 공부

목록 보기
3/4
post-thumbnail

CRUD 구현

Java Spring으로 CRUD를 구현해보자!

❗️참고로 database를 사용하지 않고 Dto로 구현하였습니다.


Dto 선언

Dto를 사용하는 이유

post/postDto.java

public class PostDto { // Data Transfer Object 데이터를 주고받는 데에 사용되는 객체다! 라는 뜻
    private String title;
    private String content;
    private String writer;

    // 생성자, getter, setter, toString 다 쓰기 귀찮으면 Lombok 같은 라이브러리 사용할 수 있
    public PostDto(String title, String content, String writer) {
        this.title = title;
        this.content = content;
        this.writer = writer;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }

    @Override
    public String toString() {
        return "PostDto{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", writer='" + writer + '\'' +
                '}';
    }
}

기본 틀 만들기

RestController 쓰는 이유

post/PostRestController.java

@RestController
@RequestMapping("/post")
public class PostRestController {
    private static final Logger logger = LoggerFactory.getLogger(PostRestController.class);
    private final  List<PostDto> postList;

    public PostRestController() {
        this.postList = new ArrayList<>();
    }
}

POST

POST

post/PostRestController.java

    // POST /post
    @PostMapping() // 파라미터에 이무것도 안 써줘도 알아서 '/post' url로 request body에 있는 값을 집어넣는다
    public void createPost(@RequestBody PostDto postDto) {
        logger.info(postDto.toString());
        this.postList.add(postDto);
    }


GET

GET

post/PostRestController.java

    // GET /post
    @GetMapping()
    public List<PostDto> readPostAll() {
        logger.info("in read post all");
        return this.postList;
    }

    // GET /post/0
    @GetMapping("{id}")
    public PostDto readPost(@PathVariable("id") int id) {
        logger.info("in read post");
        return this.postList.get(id);
    }





PUT

PUT

post/PostRestController.java

    // PUT /post/0
    @PutMapping("{id}")
    public void updatePost(
            @PathVariable("id") int id,
            @RequestBody PostDto postDto
    ) {
        PostDto targetPost = this.postList.get(id);
        if (postDto.getTitle() != null) {
            targetPost.setTitle(postDto.getTitle());
        }
        if (postDto.getContent() != null) {
            targetPost.setContent(postDto.getContent());
        }
        this.postList.set(id, targetPost);
    }



DELETE

DELETE

    // DELETE /post/0
    @DeleteMapping("{id}")
    public void deletePost(@PathVariable("id") int id) {
        this.postList.remove(id);
    }

post/PostRestController.java



재밌다

profile
어흥🦁

0개의 댓글