[TIL] 2021.11.04

SeonMan Kim·2021년 11월 4일
3

TIL / WIL

목록 보기
6/7

자바 스프링을 배운지 이틀째 날이 되었다. 간략하게 오늘 배운 내용을 정리해 본다.

생성자

public class Course {
    public String title;
    public String tutor;
    public int days;

    // 이렇게 아무런 파라미터가 없는 생성자를 기본생성자 라고 부릅니다.
    public Course() {

    }

    public Course(String title, String tutor, int days) {
        // this 는 "이것" 이죠? 클래스 변수를 가리킵니다.
        this.title = title;
        this.tutor = tutor;
        this.days = days;
    }
}
  • 생성자 : 객체가 생성될 때 객체의 상태를 설정하기 위해 자동으로 호출되는 메소드
  • 기본 생성자 : 생성자가 없을 때 객체를 생성하기 위해 자바에서 암묵적으로 추가해주는 생성자

Getter/Setter

정보은닉

  • 불필요한 정보를 프로그래머에게 숨기고 필요한 정보만 보여주어 것
  • 프로그래머 입장에서 실수로 의도치 않은 버그를 발생하거나 필요한 것만 공개하여 코딩의 작업효율을 높힌다.
  • 자바에서는 멤버변수를 private 로 숨기고 메소드를 구하여 값을 얻어오거나 설정한다.
private String title;

// Getter
public String getTitle() {
    return this.title;
}

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

H2

  • In-memory 기반의 데이터베이스로 작고 가벼우며 빠르다.
  • 서버의 작동이 멈추면 내용이 모두 삭제된다.
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb

JPA

  • Java Persistence API 의 약자로 데이터베이스에 접근하고 설정하는데 향상된 지원을 제공한다.

설정

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  • build.gradle 파일
spring.jpa.show-sql=true

소스코드

@Entity // 테이블임을 나타냅니다.
public class Lecture {

		public Lecture() {}

    @Id // ID 값, Primary Key로 사용하겠다는 뜻입니다.
    @GeneratedValue(strategy = GenerationType.AUTO) // 자동 증가 명령입니다.
    private Long id;

    @Column(nullable = false) // 컬럼 값이고 반드시 값이 존재해야 함을 나타냅니다.
    private String title;

    @Column(nullable = false)
    private String tutor;

    public Long getId() {
        return id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getTutor() {
        return this.tutor;
    }

    public Lecture(String title, String tutor) {
        this.title = title;
        this.tutor = tutor;
    }
}
  • /domain/Lecture.java 파일
public interface LectureRepository extends JpaRepository<Lecture, Long> {
}
  • /domain/LectureRepository.java 파일
@Bean
public CommandLineRunner demo(LectureRepository lectureRepository) {
    return (args) -> {
        Lecture course = new Lecture("과목 01", "이름 01");
        lectureRepository.save(course);
        lectureRepository.findAll();
    };
}
  • Item01Application 파일

개념정리

POC(Proof of Concept)

  • 개념 증명이란 뜻으로 새로운 상품이나 기술을 개발하기 전에 검증하는 단계이다.
  • 개발하는 대상의 상품성이나 가치를 확인하며 실현 가능한 여부를 확인하다.

ORM(Object Relational Mapping)

  • "객체와 관계형데이터베이스를 연결한다" 라는 의미로 객체와 테이블을 서로 연결한다.
  • 관계형데이터베이스를 객체로 다루므로 객체지향적인 로직을 사용하며 관계형데이터베이스를 다둘 수 있다.
  • 소스코드와 DB간의 종속성이 줄어든다.
  • 러닝커브가 크며 대용량의 데이터를 사용할 경우 튜닝하기가 번거롭다.

참고

profile
안녕하세요

0개의 댓글