Skip to content

강수빈 1주차 과제 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/main/java/doit/jpastudy2/repository/CategoryRepository.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Professor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package doit.jpastudy2.repository;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
public class Professor {

@Id // PK임을 나타낸다.
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다.
@Column(name = "prof_id") // 컬럼명을 지정한다.
private Long id;

// @Column(name = "type")이 생략된 경우 필드명이 컬럼명이 된다. snake_case로 변환된다.
@Column(name = "prof_name")
private String name;

// @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다.
@Column(name = "prof_email")
private String email;
Comment on lines +19 to +28
Copy link
Member

@jjunhub jjunhub Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다!

짜 예전에는 MySQL 용량 차지한다고 DB Column 이름을 줄여서 사용하곤 했는데, 요즘은 그냥 이름 길게길게 사용하는게 일반적이라고 하더라구요. 물론 이건 어디나 프로젝트 바이 프로젝트이니, 참고만 하시면 될 것 같습니다

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엥 이거 왜 줄이 그어지지
일부로 그은거 아닙니다..


@Builder // 빌더 패턴을 사용할 수 있게 한다.
public Professor(String name, String email) {
this.name = name;
this.email = email;
}
}
10 changes: 10 additions & 0 deletions src/main/java/doit/jpastudy2/repository/ProfessorRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProfessorRepository extends JpaRepository<Professor, Long> {
Professor findByEmail(String email);
Professor findByName(String name);
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

규칙 지켜서 잘 작성해주셨습니다!

Professor findByProfId(Long id);

위의 쿼리 메소드와 비교해서, 이 메소드에 대해서 항상 유일하게 Professor 개체가 1개만 나오는가?를 고려해보신 뒤에
다음과 같은 사항을 고민해보시는 것도 좋을 것 같습니다.

  1. 어떻게 이를 변경하면 좋을지
  2. 언제가 항상 1개이고, 언제가 여러 개일수 있는 경우인지


Professor findByNameAndEmail(String name, String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
@Entity
@NoArgsConstructor
@Getter
public class Category {
public class Subjects {

@Id // PK임을 나타낸다.
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다.
@Column(name = "category_id") // 컬럼명을 지정한다.
@Column(name = "sub_id") // 컬럼명을 지정한다.
private Long id;

// @Column(name = "type")이 생략된 경우 필드명이 컬럼명이 된다. snake_case로 변환된다.
private String type;
@Column(name = "sub_name")
private String name;

// @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다.
private String description;
@Column(name = "sub_unit")
private String unit;

@Builder // 빌더 패턴을 사용할 수 있게 한다.
public Category(String description, String type) {
this.description = description;
this.type = type;
public Subjects(String name, String unit) {
this.name = name;
this.unit = unit;
}
}
10 changes: 10 additions & 0 deletions src/main/java/doit/jpastudy2/repository/SubjectsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface SubjectsRepository extends JpaRepository<Subjects, Long> {
Subjects findByUnit(String unit);
Subjects findByName(String name);

Subjects findByNameAndUnit(String name, String unit);
}
108 changes: 0 additions & 108 deletions src/test/java/doit/jpastudy2/repository/CategoryRepositoryTest.java

This file was deleted.

121 changes: 121 additions & 0 deletions src/test/java/doit/jpastudy2/repository/ProfessorRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package doit.jpastudy2.repository;

import jakarta.transaction.Transactional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Transactional
@SpringBootTest
class ProfessorRepositoryTest {
@Autowired
private ProfessorRepository professorRepository;

@DisplayName("save test")
@Test
void test(){
Professor professor1 = Professor.builder()
.name("노병희")
.email("[email protected]")
.build();
Professor professor2 = Professor.builder()
.name("조현석")
.email("[email protected]")
.build();
Professor professor3 = Professor.builder()
.name("이슬")
.email("[email protected]")
.build();
Professor professor4 = Professor.builder()
.name("변광준")
.email("[email protected]")
.build();

professorRepository.save(professor1);
professorRepository.save(professor2);
professorRepository.save(professor3);
professorRepository.save(professor4);

List<Professor> professors = professorRepository.findAll();
Assertions.assertThat(professors).hasSize(4);
Assertions.assertThat(professors.get(2).getName()).isEqualTo("이슬");
Assertions.assertThat(professors.get(2).getEmail()).isEqualTo("[email protected]");

System.out.println("2번째 교수 이름 : " + professors.get(2).getName());
}

@DisplayName("Name을 이용한 조회")
@Test
void findByName(){
Professor professor1 = Professor.builder()
.name("노병희")
.email("[email protected]")
.build();
Professor professor2 = Professor.builder()
.name("조현석")
.email("[email protected]")
.build();
Professor professor3 = Professor.builder()
.name("이슬")
.email("[email protected]")
.build();
Professor professor4 = Professor.builder()
.name("변광준")
.email("[email protected]")
.build();

professorRepository.save(professor1);
professorRepository.save(professor2);
professorRepository.save(professor3);
professorRepository.save(professor4);

Professor result1 = professorRepository.findByName("노병희");
Professor result2 = professorRepository.findByName("조현석");

Assertions.assertThat(result1).isNull();
Assertions.assertThat(result2).isNotNull();
Assertions.assertThat(result2.getName()).isEqualTo("노병희");
}

// @DisplayName("description과 type을 이용한 조회")
// @Test
// void findByTypeAndDescription() {
// // Given
// Category category1 = Category.builder()
// .type("양식")
// .description("데이트")
// .build();
//
// Category category2 = Category.builder()
// .type("한식")
// .description("한국인의 정")
// .build();
//
// Category category3 = Category.builder()
// .type("중식")
// .description("철가방")
// .build();
//
// Category category4 = Category.builder()
// .type("미식")
// .description("축구ㅋㅋ")
// .build();
//
// categoryRepository.saveAll(List.of(category1, category2, category3, category4));
//
// // When
// Category result1 = categoryRepository.findByTypeAndDescription("양식", "데이트");
// Category result2 = categoryRepository.findByTypeAndDescription("중식", "데이트"); // null
// Category result3 = categoryRepository.findByTypeAndDescription("미식", "축구ㅋㅋ");
//
// // Then
// Assertions.assertThat(result1.getType()).isEqualTo("양식");
// Assertions.assertThat(result2).isNull();
// Assertions.assertThat(result3.getDescription()).isEqualTo("축구ㅋㅋ");
// }

}
Loading