AI교육과정 - Spring.2

단비·2022년 12월 6일
0

AI교육과정

목록 보기
41/69
  • Intellij 에서 sql문 사용
    • 우측 database 탭 - + 버튼 클릭 - mysql 선택 - localhost: root / password: 1234

  • 좋은 객체 지향 설계의 5가지 원칙 (로버트 마틴이 정리한 좋은 객체 지향 설계의 5가지 원칙)
    1. SRP: 단일 책임 원칙

      • 한 클래스는 하나의 책임만 가져야함
      • 변경이 있을 때 파급 효과가 적으면 단일 책임 원칙을 따르고 있다는 것
    2. OCP: 개방-폐쇄 원칙

      • 소프트웨어 요소는 확장에는 열려있으나 변경에는 닫혀 있어야 함
      public class MemberService {
      		private MemberRepository memberRepository = new MemoryMemberRepository();
      }
      public class MemberService {
      		//private MemberRepository memberRepository = new MemoryMemberRepository();
      		private MemberRepository memberRepository = new JdbcMemberRepository();
      }
      // 다형성을 사용했지만 OCP 원칙을 지킬 수 없음
      // 객체를 생성하고, 연관관계를 맺어주는 별도의 생성자가 필요!
    3. LSP: 리스코프 치환 원칙

      • 프로그램의 객체는 프로그램의 정확성을 깨뜨리지 않으면서 하위 타입의 인스턴스로 바꿀 수 있어야 함
      • 다형성에서 하위 클래스는 인터페이스 규약을 다 지켜야 한다는 것, 다형성을 지원하기 위한 원칙, 인터페이스를 구현한 구현체는 믿고 사용하려면 이 원칙이 필요함
    4. ISP: 인터페이스 분리 원칙

      • 특정 클라이언트를 위한 인터페이스 여러 개가 범용 인터페이스 하나보다 낫다
      • 여러 개의 인터페이스로 나누면 인터페이스가 명확해지고, 대체 가능성이 높아짐
    5. DIP: 의존관계 역전 원칙
      - 추상화에 의존해야 하고, 구체화에 의존하면 안됨
      - 구현 클래스에 의존하지 말고, 인터페이스에 의존

      ```java
      public class MemberService {
      		private MemberRepository memberRepository = new MemoryMemberRepository();
      }
      // DIP 위반
      ```

      🤔 다형성 만으로는 OCP, DIP를 지킬 수 없음

      🤨 다형성 만으로는 구현 객체를 변경할 때 클라이언트 코드도 함께 변경

      😏 다형성 만으로는 쉽게 부품을 갈아 끼우듯이 개발할 수 없음


  • 스프링 프로젝트 구조
    • src → main → java: 자바 코드
    • src → main → resources → application.properties : 설정 정보
    • src → main → resources → static : 정적 콘텐츠(html, css, js)
    • src → main → resources → templates : 동적 템플릿
    • src → test → java : 테스트용 자바코드 작성
    • build.gradle : 버전 관리
  • @RestController RestController 를 통해 연결
    • application.properties에 포트 번호 설정 필수
      server.port = 8888
    • 기본 문법
      @RestController
      @RequestMapping("/api") // http://localhost:8888/api 를 호출하면 실행
    • @RequestParam(name = "userpw") String password
      • userpw 값을 가져오되 password로 변수명 변경
    • Get
      • RequestMapping을 이용한 get 호출법
        @RequestMapping(method = RequestMethod.GET, path = "/getmethod")
        public String getMethod(){
            return "getMethod() 호출!";
        }
      • Get 방식의 파라미터 값 받기
        // http://localhost:8888/api/getparameter1?userid=apple&userpw=1234
        @RequestMapping(method=RequestMethod.GET, path = "getparameter1")
        public String getParameter1(@RequestParam String userid, @RequestParam String userpw){
            System.out.println("userid: " + userid);
            System.out.println("userpw: " + userpw);
            return "getParameter1() 호출!";
        }
      • @GetMapping
        • @RequestMapping GET방식에 대한 축약형

          @GetMapping("/getparameter2")
          ==
          @RequestMapping(method=RequestMethod.GET, path = "getparameter2")
      • 객체를 넘겨줄 경우 json으로 리턴 받음
        // http://localhost:8888/api/getmultiparameter2?userid=apple&userpw=1234&name=김사과&gender=여자&email=apple@apple.com&age=20
        @GetMapping("/getmultiparameter2")
        public Member getMultiParameter2(Member member){ //json으로 리턴
            System.out.println(member);
            return member;
        }
    • Post
      • RequestMapping을 이용한 post 호출법
        // http://localhost:8888/api/postmethod
        @RequestMapping(method= RequestMethod.POST, path = "/postmethod")
        public String postMethod(){
            return "postMethod() 호출!";
        }
      • Post 방식의 파라미터 값 받기
        • post 방식이라 링크에 파라미터값을 넣어 접속하는 방법은 안되고 Postman을 이용하여 확인 가능

          // http://localhost:8888/api/postparameter
          @RequestMapping(method= RequestMethod.POST, path = "/postparameter")
          public String postParameter(@RequestParam String userid, @RequestParam String userpw){
              System.out.println("userid: " + userid);
              System.out.println("userpw: " + userpw);
              return "postParameter() 호출!";
          }
      • 객체로 넘겨줘야 할 경우 json 코드로 전달해야 함
        // http://localhost:8888/api/postmultiparameter
        @PostMapping("postmultiparameter")
        public Member postMultiParameter(@RequestBody Member member){ // 객체로 보낼 때는 파라미터값을 하나하나 입력하면 안됨, json으로 보내야함
            System.out.println(member);
            return member;
        }

  • 테이블 정의서(엔티티 정의서)
    • 보통 엑셀로 작성하며 테이블 및 컬럼명과 데이터 타입 등을 작성해놓은 문서

  • ERD(Entity Relationship Diagram)
    • 개체-관계 모델, 테이블 간의 관계를 설명해주는 다이어그램

    • DB의 구조를 한 눈에 파악할 수 있음

profile
tistory로 이전! https://sweet-rain-kim.tistory.com/

0개의 댓글