-
Notifications
You must be signed in to change notification settings - Fork 14
설만수 1주차 과제 #6
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
base: main
Are you sure you want to change the base?
설만수 1주차 과제 #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package doit.jpastudy2.JpaRepository; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
public class Champion { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
@Column(name = "Id_champion") | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private Integer cost; | ||
|
||
@Builder | ||
public Champion(String name, int cost) { | ||
this.name = name; | ||
this.cost = cost; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package doit.jpastudy2.JpaRepository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ChampionRepository extends JpaRepository<Champion, Long> { | ||
|
||
Champion findByName(String name); | ||
|
||
Champion findByCost(Integer cost); | ||
|
||
Champion findByNameAndCost(String name, Integer cost); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package doit.jpastudy2.JpaRepository; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
public class Mapping { | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
package doit.jpastudy2.JpaRepository; | ||||||
|
||||||
import jakarta.persistence.*; | ||||||
import lombok.Builder; | ||||||
import lombok.Getter; | ||||||
import lombok.NoArgsConstructor; | ||||||
|
||||||
@Entity | ||||||
@NoArgsConstructor | ||||||
@Getter | ||||||
public class Synergy { | ||||||
|
||||||
@Id | ||||||
@GeneratedValue(strategy = GenerationType.AUTO) | ||||||
@Column(name = "Id_synergy") | ||||||
private Long id; | ||||||
|
||||||
private String name; | ||||||
|
||||||
private Integer max_activate; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 디테일하게 필드 명 적어주신 부분 좋습니다! 따라서 아래처럼 작성하는 것이 보편적이니 참고하시면 될 것 같아요.
Suggested change
|
||||||
|
||||||
@Builder | ||||||
public Synergy(String name, Integer max_activate) { | ||||||
this.name = name; | ||||||
this.max_activate = max_activate; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package doit.jpastudy2.JpaRepository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface SynergyRepository extends JpaRepository<Synergy,Long> { | ||
|
||
Synergy findByName(String name); | ||
|
||
Synergy findByMaxActivate(Integer maxActivate); | ||
|
||
Synergy findByNameAndMaxActivate(String name, Integer maxActivate); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package doit.jpastudy2.Jparepository; | ||
|
||
import doit.jpastudy2.JpaRepository.Champion; | ||
import doit.jpastudy2.JpaRepository.ChampionRepository; | ||
import jakarta.transaction.Transactional; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
public class ChampionRepositoryTest { | ||
|
||
@Autowired | ||
private ChampionRepository championRepository; | ||
|
||
@Test | ||
void test() { | ||
Champion champion1 = Champion.builder() | ||
.name("누누") | ||
.cost(2) | ||
.build(); | ||
|
||
Champion champion2 = Champion.builder() | ||
.name("헤카림") | ||
.cost(3) | ||
.build(); | ||
|
||
|
||
championRepository.save(champion1); | ||
championRepository.save(champion2); | ||
|
||
Champion championTest = championRepository.findByCost(2); | ||
Champion championTest2 = championRepository.findByName("헤카림"); | ||
Champion championTest3 = championRepository.findByNameAndCost("누누", 2); | ||
Champion championTest4 = championRepository.findByCost(5); | ||
|
||
Assertions.assertThat(championTest).isNull(); | ||
Assertions.assertThat(championTest2).isNull(); | ||
Assertions.assertThat(championTest3).isNull(); | ||
Assertions.assertThat(championTest4).isNotNull(); | ||
Comment on lines
+34
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 꼼꼼하게 assertThat 작성해주신 것 좋습니다!! 작성하신 클래스에서 메소드 왼쪽에 보면 초록색 실행 버튼 있습니다. 이를 통해서 테스트 실행해보셨을까요? |
||
|
||
System.out.println("First champion in DB : " + champion1.getName()); | ||
System.out.println("Second champion in DB : " + champion2.getName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package doit.jpastudy2.Jparepository; | ||
|
||
import doit.jpastudy2.JpaRepository.Synergy; | ||
import doit.jpastudy2.JpaRepository.SynergyRepository; | ||
import jakarta.transaction.Transactional; | ||
import org.assertj.core.api.Assertions; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@Transactional | ||
@SpringBootTest | ||
public class SynergyRepositoryTest { | ||
private SynergyRepository synergyRepository; | ||
|
||
void test() { | ||
Synergy synergy1 = Synergy.builder() | ||
.name("벌꿀술사") | ||
.max_activate(7) | ||
.build(); | ||
|
||
Synergy synergy2 = Synergy.builder() | ||
.name("요새") | ||
.max_activate(6) | ||
.build(); | ||
|
||
Synergy synergy3 = Synergy.builder() | ||
.name("쇄도자") | ||
.max_activate(9) | ||
.build(); | ||
|
||
synergyRepository.save(synergy1); | ||
synergyRepository.save(synergy2); | ||
synergyRepository.save(synergy3); | ||
|
||
Synergy synergyTest = synergyRepository.findByName("벌꿀술사"); | ||
Synergy synergyTest2 = synergyRepository.findByMaxActivate(9); | ||
Synergy synergyTest3 = synergyRepository.findByNameAndMaxActivate("쇄도자", 9); | ||
Synergy synergyTest4 = synergyRepository.findByName("서리"); | ||
|
||
Assertions.assertThat(synergyTest).isNull(); | ||
Assertions.assertThat(synergyTest2).isNull(); | ||
Assertions.assertThat(synergyTest3).isNull(); | ||
Assertions.assertThat(synergyTest4).isNotNull(); | ||
Comment on lines
+39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요기도 마찬가지로 다시 작성한 뒤에, 테스트 실행해보시면 좋을 것 같아요! |
||
|
||
System.out.println("First synergy in DB : " + synergy1.getName()); | ||
System.out.println("Second synergy in DB : " + synergy2.getName()); | ||
System.out.println("Third synergy in DB : " + synergy3.getName()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
익숙한 주제로 작성하는 것 매우 좋습니다ㅎㅎㅎ
틀린 것은 아니지만! 관례 상 자주 사용되는 패턴이 있습니다.
MySQL에서는 Column 이름을 snake_case로 작성하는 것이 일반적입니다. 참고!
그리고 table명_id 구조로 PK를 작성하는 편이기도 합니다.
따라서 아래처럼 써보시는 것이 어떠할까 싶네용