21.02.23 Untact프로젝트(개발환경 셋팅부터 게시물 추가까지 구현)

2021. 2. 23. 19:55JAVA/Spring & Vue APP 프로젝트(백엔드)

# NOTE

프레임워크?
- 이미 완성된 프로젝트
- 특정 틀이 비워져있고 그 틀을 채워가는 개념
- 이미 정해진 틀(소스코드 양식)에 맞게 작성하고 끼워 넣으면 됨
- 스프링부트에는 톰캣이 이미 내장되어있음
고객? => 브라우저

컨트롤러
 - 거름망
 - 업무배분 

서비스
 - 핵심로직

DAO(Data Access Object)
 - 문서고지기
 - 창고지기

DTO(Data Transfer Object)

DB
 - 창고
어노테이션
- 이 클래스가 컨트롤러 클래스라는것을 알려주는 것
- 어떤 클래스를 만들면 스프링에게 이 클래스의 용도를 알려주어야 함

# 소스코드

<UsrHomeController.java>

package com.sbs.untact.controller;

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

@Controller
//어노테이션
//이 클래스가 컨트롤러 클래스라는것을 알려주는 것
//어떤 클래스를 만들면 스프링에게 이 클래스의 용도를 알려주어야 함
public class UsrHomeController {
	
	// http://localhost:8024/usr/home/main
	@RequestMapping("/usr/home/main")
	@ResponseBody
	// 만약 "/usr/home/main" 요청이 들어오면 showMain()가 실행되도록 하는 것
	public String showMain() {
		System.out.println("hello test");
		return "Hello!!!!????!!!!!";
	}
}

 

<UsrArticleController.java>

package com.sbs.untact.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.sbs.untact.dto.Article;

@Controller
public class UsrArticleController {

	private List<Article> articles;
	private int articleLastId;

	public UsrArticleController() {
		// 멤버변수 초기화
		articleLastId = 0;
		articles = new ArrayList<>();

		articles.add(new Article(++articleLastId, "2021-02-23 18:18:18", "제목1", "내용1"));
		articles.add(new Article(++articleLastId, "2021-02-23 18:18:18", "제목2", "내용2"));
	}

	@RequestMapping("/usr/article/detail")
	@ResponseBody
	// 스프링부트: 알아서 json형태로 바꿔 출력값을 리턴해준다.
	public Article showDetail(int id) {
		return articles.get(id - 1);
	}

	@RequestMapping("/usr/article/list")
	@ResponseBody
	public List<Article> showList() {
		return articles;
	}

	@RequestMapping("/usr/article/doAdd")
	@ResponseBody
	public Map<String, Object> doAdd(String regDate, String title, String body) {
		articles.add(new Article(++articleLastId, regDate, title, body));

		Map<String, Object> rs = new HashMap<>();
		rs.put("resultCode", "S-1");
		rs.put("msg", "성공");
		rs.put("id", articleLastId);
		
		return rs;
	}
}