Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

API 개발 본문

인프런/[인프런] QueryDsl

API 개발

Seung__ 2021. 9. 4. 14:03

데이터 확인을 위해서 샘플 데이터를 추가하자.

테스트케이스 실행에 영향을 주지 않기 위해 톰캣이 돌아갈 때만 샘플 데이터가 들어가도록 설정하자.

 --> 테스트케이스, 톰캣의 프로파일 분리

 

1. 프로파일 설정


  • main.resources.application.yml

  • test.resources.application.yml

 

 

 

2. 샘플 데이터 추가


  • @Profile 을 통해 application.yml의 active와 대응됨.
  • @PostConstruct : bean 생성 이후 해당 메서드 실행.
  • JPA 데이터 변경은 Transaction안에서 이루어져야함
@Profile("local")
@Component
@RequiredArgsConstructor
public class InitMember {

    private final InitMemberService initMemberService;

    @PostConstruct
    public void init(){
        initMemberService.init();
    }

    static class InitMemberService{

        @PersistenceContext
        private EntityManager em;

        @Transactional
        public void init(){
            Team teamA = new Team("teamA");
            Team teamB = new Team("teamB");
            em.persist(teamA);
            em.persist(teamB);

            for (int i=0; i<100; i++){
                Team selectedTeam = i % 2 == 0 ? teamA : teamB;
                em.persist(new Member("member" + i, i, selectedTeam));
            }
        }
    }
}

 

 

 

 

3. Controller


@RestController
@RequiredArgsConstructor
public class MemberController {

    private final MemberJpaRepository memberJpaRepository;

    @GetMapping("/v1/members")
    public List<MemberTeamDto> searchMemberV1(MemberSearchCondition condition){
        return memberJpaRepository.search(condition);
    }
}

  • 조건을 넣어볼까?
    •  

  • ? 그런데 Controller에서 paramter 받을 수 있도록 설정 안했는데..? 신기하네..
    • 사실은 @ModelAttribute가 생략된 것.
    • @ModelAttribute 가 생략되면
    • String, int같은 파라미터는 @RequestParam으로 , 복잡한 타입은 @ModelAttribute가 생략되었다고 판단함.
    • @ModelAttribute
      @RequestParam과 비슷함. @RequestParam과 다르게 String, int가 아닌 DTO와 같은 객체로 받을 경우에 사용파라미터 명이 같으면 Spring MVC에서 데이터를 바인딩 해줌.

 

 

 

 

4. GitHub : 210904 API Controller


 

GitHub - bsh6463/Querydsl

Contribute to bsh6463/Querydsl development by creating an account on GitHub.

github.com

 

Comments