QueryDSL

์ด์˜ยท2023๋…„ 10์›” 23์ผ

๐Ÿ”— ์ฐธ๊ณ ํŽ˜์ด์ง€_์˜์กด์„ฑ
๐Ÿ”— ์ฐธ๊ณ ํŽ˜์ด์ง€_querydslPredicateExcutor

์ €๋ฒˆ ํ”„๋กœ์ ํŠธ์— ์ด์–ด์„œ ์ด๋ฒˆํ”„๋กœ์ ํŠธ์—๋„ ๋‹ค์ค‘๊ฒ€์ƒ‰์ด ํ•„์š”ํ•˜์—ฌ ์ •๋ฆฌํ•˜๊ฒŒ๋˜์—ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ ์™ธ์—๋„ ์‚ฌ์šฉ๋˜๋Š”์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜ ์›น๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๋Š” ํ•œ ๊ณ„์† ์‚ฌ์šฉํ•  ๊ฒƒ๊ฐ™์€โ€ฆ

์ €๋ฒˆ ํ”„๋กœ์ ํŠธ, ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ ๋ชจ๋‘ ์ž๋ฐ”์™€ ์Šคํ”„๋ง๋ถ€ํŠธ๋กœ ๋ฐฑ์•ค๋“œ ์ž‘์—…์„ ํ•˜์˜€๋Š”๋ฐ ์ €๋ฒˆ MES ํ”„๋กœ์ ํŠธ๋•Œ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ๋งŽ์•„ ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ์–ธ ์ ์ด ์žˆ๋‹ค.
์กฐ๊ฑด์ด ํ•˜๋‚˜ ์ถ”๊ฐ€๋ ์ˆ˜๋ก ์ดํ”„๋ฌธ์ด ์–ผ๋งˆ๋‚˜ ๋Š˜์–ด๋‚˜๋Š”์ง€โ€ฆ 

์ด๋ ‡๊ฒŒ ํ•ด์„œ๋Š” ๋๋„ ์—†๊ฒ ๋‹ค. ์•„๋‹ˆ ๊ทธ๋Ÿฌ๋ฉด ๋„ค์ด๋ฒ„ ์‡ผํ•‘๊ฐ™์€ ์—„์ฒญ๋‚œ ๋‹ค์ค‘๊ฒ€์ƒ‰์€ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑฐ์ง€??? ๋ผ๋Š” ์˜๋ฌธ์„ ๊ฐ–๊ฒŒ ๋˜์—ˆ๊ณ  ์งฑ์ง€๋ฏผ์—๊ฒŒ ๊ณ ๋ฏผ์„
์ด์•ผ๊ธฐํ•˜๋‹ˆ ์ฟผ๋ฆฌ๋””์—์…€์ด๋ผ๋Š”๊ฒƒ์ด ์žˆ๋‹ค๋Š” ์ •๋ณด๋ฅผ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ๋‚ด๊ฐ€ ๋ฌป๊ธฐ ์ „์— ์งฑ์ง€๋ฏผ๋„ ๊ทธ๋Ÿฐ ์ƒ๊ฐ์„ ํ–ˆ๊ณ  ๊ตฌํ˜„์„ ํ•œ ๋’ค์—ฌ์„œ ๋งŽ์€ ๋„์›€์„ ์–ป์—ˆ๋‹ค.

1. ์˜์กด์„ฑ ์ฃผ์ž…

QueryDSL์ด ๋ฒ„์ „๋„ ๋‹ค๋ฅด๊ณ  ์ธํ…”๋ฆฌ์ œ์ด ๋ฒ„์ „์— ๋”ฐ๋ผ ๋ญ์— ๋”ฐ๋ผ ์˜์กด์„ฑ์ด ๋‹ฌ๋ผ์„œ ๋ธ”๋กœ๊ทธ๋ž‘ ์ฑ…์„ ๋’ค์ ธ์„œ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด๋„ ์ œ๋Œ€๋กœ ์•ˆ๋˜๋Š”๊ฒŒ ํƒœ๋ฐ˜์ด์—ˆ๋Š”๋ฐ ๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ์œ„์— ์ฐธ๊ณ ํŽ˜์ด์ง€์— ๊ฒŒ์‹œํ•œ ๋ธ”๋กœ๊ทธ์˜ ๋ฐฉ๋ฒ•์„ ๋”ฐ๋ผํ•˜๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.

1. build.gradle์— ์˜์กด์„ฑ ์ฃผ์ž…



2. gradle ๋นŒ๋“œ

๋นŒ๋“œ

์ƒ์„ฑ๋œ Qํด๋ž˜์Šค

ํ˜น์‹œ compledQuerydsl์„ ํ–ˆ๋Š”๋ฐ๋„ Qํด๋ž˜์Šค๋“ค์ด ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ทธ๋ž˜๋“ค ์ž์ฒด๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋” ๋นŒ๋“œํ•˜๋ฉด ๋จ.

2. Repository ์ƒ์† ๋ฐ›๊ธฐ

3. Service ์ฟผ๋ฆฌ์ž‘์„ฑ

// ์ˆ˜์‹œ์ ๊ฒ€ ์ „์ฒด์กฐํšŒ ๊ฒ€์ƒ‰
    public Map<String, Object> searchList(String spePart, String speFacility, LocalDateTime speStartDateTime,
                                          LocalDateTime speEndDateTime, SpeStatus speComplete, String spePerson, String speEmpNum){
        Map<String, Object> searchSpeList = new HashMap<>();
        QSpecialInspection qSpecialInspection = QSpecialInspection.specialInspection;
        BooleanBuilder builder = new BooleanBuilder();

        if (spePart != null) {
            builder.and(qSpecialInspection.spePart.eq(spePart));
        }
        if (speFacility != null) {
            builder.and(qSpecialInspection.speFacility.eq(speFacility));
        }
        if (speStartDateTime != null && speEndDateTime != null) {
            builder.and(qSpecialInspection.speDate.between(speStartDateTime, speEndDateTime));
        }
        if (speComplete != null) {
            builder.and(qSpecialInspection.speComplete.eq(speComplete));
        }
        if (spePerson != null) {
            builder.and(qSpecialInspection.spePerson.eq(spePerson));
        }
        if (speEmpNum != null) {
            builder.and(qSpecialInspection.speEmpNum.eq(speEmpNum));
        }

        Sort sort = Sort.by(Sort.Direction.DESC, "speId");
        List<SpecialInspection> searchSpeData = (List<SpecialInspection>) specialInspectionRepository.findAll(builder, sort);
        List<SpeInsFormDTO> searchSpeDataDTOList = SpeInsFormDTO.of(searchSpeData);
        searchSpeList.put("searchSpeDataDTOList", searchSpeDataDTOList);

        return searchSpeList;

    }

4. Controller

// ์ˆ˜์‹œ์ ๊ฒ€ ์ „์ฒดํ˜„ํ™ฉ ๊ฒ€์ƒ‰
   @GetMapping("/user/frequentinspection")
   public ResponseEntity<?> speFullList(@RequestParam (value = "spePart", required = false) String spePart,
                                        @RequestParam (value = "speFacility",required = false) String speFacility,
                                        @RequestParam (value = "speStartDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate speStartDate,
                                        @RequestParam (value = "speEndDate",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate speEndDate,
                                        @RequestParam (value = "speComplete",required = false) SpeStatus speComplete,
                                        @RequestParam (value = "spePerson",required = false) String spePerson,
                                        @RequestParam (value = "speEmpNum",required = false) String speEmpNum
   ) {

    Map<String, Object> responseData = new HashMap<>();

    // ๋‚ ์งœ ๋ณ€ํ™˜
    LocalDateTime speStartDateTime = null;
    LocalDateTime speEndDateTime = null;
    if(speStartDate != null && speEndDate != null){
     speStartDateTime =  LocalDateTime.of(speStartDate, LocalTime.MIN);
     speEndDateTime =  LocalDateTime.of(speEndDate, LocalTime.MAX);
    }

    Map<String, Object> searchSpeList = specialInspectionService.searchList(spePart, speFacility, speStartDateTime, speEndDateTime, speComplete, spePerson, speEmpNum);
    responseData.put("searchSpeList", searchSpeList);

    Map<String, Object> searchPartAndFacList = specialInspectionService.getPartAndFacilityDataList();
    responseData.put("searchPartAndFacList", searchPartAndFacList);

    return new ResponseEntity<>(responseData, HttpStatus.OK);
   }

์šฐ๋ฆฌ ๋””๋น„์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ๋‚ ์งœ๋Š” LocalDateTime์ด์–ด์„œ ํ™”๋ฉด์—์„œ๋ฐ›์€ LocalTime์„ ๋ณ€ํ™˜ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์–ด์„œ ํ•ด๋‹น ์ฝ”๋“œ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณ€ํ™˜ํ•œ ๋’ค ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค.

์‚ฌ์‹ค ์ฟผ๋ฆฌ๋””์—์…€์˜ ํฐ ์žฅ์ ์€

โœ๏ธ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ (์ž๋ฐ”)์ฝ”๋“œ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด ์ปดํŒŒ์ผ ์‹œ์ ์— ๋ฌธ๋ฒ• ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธ ๊ฒƒ ๊ฐ™์€๋ฐโ€ฆ.. ๋ญ ์–ด์จŒ๋“  ๋‚˜๋Š” ๋‹ค์ค‘๊ฒ€์ƒ‰์—์„œ ๋„์›€์„ ๋ฐ›์•˜๋‹ค.



๐Ÿ‘ŒOwO๐Ÿ‘Œ

  1. ์˜์กด์„ฑ ์ถ”๊ฐ€ํ•˜๊ณ 
  2. Repository์— querydslPredicateExcutor ์ƒ์†๋ฐ›๊ณ 
  3. Service์—์„œ ๊ฒ€์ƒ‰ํ•  ์กฐ๊ฑด์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•œ๋‹ค.
    : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฒ€์ƒ‰์กฐ๊ฑด์„ ์ „๋‹ฌ๋ฐ›๊ณ , ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ์žˆ์œผ๋ฉด ํ•ด๋‹น ์กฐ๊ฑด์„ builder ๊ฐ์ฒด์— ๋‹ด์€๋‹ค์Œ Qํด๋ž˜์Šค๋กœ ๋„˜๊ฒจ์ค€๋‹ค.
    Qํด๋ž˜์Šค์—์„œ ํ•ด๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค.










    2023๋…„ 8์›” 22์ผ ์˜คํ›„ 3:34
profile
๋‚˜์•ผ๋‚˜

0๊ฐœ์˜ ๋Œ“๊ธ€