Spring boot 기본

정은하·2024년 1월 9일
post-thumbnail

기본 개념

  • 스프링 빈: 스프링이 컨테이너에 담아놓고 관리하는 객체
  • IoC: 프로그램의 흐름을 제어하는 주체가 정반대로 바뀜
    =객체의 흐름을 제어하는 주체가 정반대로 바뀜
    =스프링 빈의 흐름을 제어하는 주체가 정반대로 바뀜
  • 컨테이너: 스프링 빈을 담아두는 공간
  • DI: 스프링은 의존성을 주입하는 역할
    ->annotation 필요

*IoC를 구현하기 위해 DI가 필요하다!

MVC

-Model: 데이터 처리, 연산, 로직
-> 데이터를 가지고 연산을 하려면 데이터베이스와 소통이 필요함
->클래스: Service(로직 담당), Repository(데이터베이스와 소통 담당)
-View: 화면
-Control: Model과 View를 연결
->사용자와 Model의 중간 매개체

Controller

@Controller
@ResponseBody
public class ProductController {

    //상품 조회(목적: 요청)
    @RequestMapping(value="", method = RequestMethod.GET)
    public String findProduct(){
        return "Notebook";
    }

}

@ResponseBody: 자바 객체를 json 기반의 HTTP Body로 변환
@RequestMapping: value에 url이 들어오면 get 방식으로 데이터 요청

http method

  1. 조회: get
  2. 등록: post
  3. 수정
    -전체 수정: put
    -부분 수정: patch
  4. 삭제:delete

Service

ProductService

@Service
public class ProductService {
    public String findProduct(){
        return "Notebook!";
    }
}

ProductController

@Controller
@ResponseBody
public class ProductController {

    //상품 조회(목적: 요청)
    @RequestMapping(value="", method = RequestMethod.GET)
    public String findProduct(){
        ProductService productService=new ProductService();
        return productService.findProduct();
    }
}
profile
If you remain stagnant, you won't achieve anything

0개의 댓글