본문 바로가기

Diary/WIL

2024-03-10 WIL

프로젝트 생성

기본 dependencies에 thymeleaf, lombok, spring web

추가선택)

빠른 재시작: DevTools

DB:  JPA, mySQL

보안: security, validation(이메일 형식)

 

사용을 최대한 피해야 하는 자료형:

Object[] : not restful함

 

알아두어야 할 개념

MVC

  1. @Controller, @RestController
  2. @Service
  3. @Repository
  4. @Entity, @Dto
    1. @Table
    2. @Column
      1. name = ""
      2. nullable = true or false
      3. unique = true
      4. length = 
    3. @Id
      • @GeneratedValue(strategy = GenerationType.IDENTITY) 를 통해서 기본 키 생성을 db에 위임 가능
        • IDENTITY, SEQUENCE, TABLE, AUTO 등 다양한 방법이 존재.
          • IDENTITY와 SEQUENCE의 차이는?
    4. @ManyToOne 및 @OneToMany
    5. @OneToOne 및 @JoinColumn
    6. @ManyToMany 및 @JoinTable
      • 서비스 파일에서 다음과 같이 jpql문 사용하는 대신, 양방향 관계를 구성하여 조인된 테이블을 간단하게 불러올 수 있다.
      • String jpql = "SELECT c FROM Course c LEFT JOIN c.instructor i";
    7. @Temporal
      • 엔티티 및 시간 유형 지정
      • @CreatedDate, @LastModifiedDate
  5. @Validation
    • @Pattern(regexp = 를 통해 정규식 부여 가능)
    • @Email, @NotNull, @NotEmpty, @NotBlank, @Size, @Max, @Min, @Positive, @Negative
  6. @ModelAttribute
  7. @PathVariable, @RequestBody와 @ResponseBody

IOC/DI

 

해둬야 할 설정

 

hibernate - sql log를 보여준다, enitity에 맞게 db에 스키마를 자동으로 업데이트 한다.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/{db이름}
spring.datasource.username=root
spring.datasource.password={비밀번호}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true

 

Timestamped Entity 생성하기

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Timestamped {

    @CreatedDate
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private LocalDateTime modifiedAt;
}

이후 @EnableJpaAuditing을 Application 파일에 추가해준다.

 

성공/실패 및 상태 반환용 객체

@Getter
@NoArgsConstructor
public class SuccessResult {
    private boolean success;
    private String message;

    public SuccessResult(boolean success, String message) {
        this.success = success;
        this.message = message;
    }
}

 

 

 

좀 더 찾아봐야할 내용

테스트 방법, 일반 실행과 디버깅 실행의 차이?

'Diary > WIL' 카테고리의 다른 글

2024-03-24-WIL-CI/CD의 개념  (0) 2024.03.24
2024-03-17 WIL - Websocket 에 대해서  (0) 2024.03.17
2024-03-03 Spring Boot JPA를 사용하기 위해서  (0) 2024.03.03
2024-02-25 WIL  (0) 2024.02.25
2024-02-18 WIL  (0) 2024.02.18