diff --git a/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepository.java b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepository.java new file mode 100644 index 0000000..2f1fb3e --- /dev/null +++ b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepository.java @@ -0,0 +1,7 @@ +package umc.spring.repository.restaurantRepository; + +import org.springframework.data.jpa.repository.JpaRepository; +import umc.spring.domain.Restaurant; + +public interface RestaurantRepository extends JpaRepository, RestaurantRepositoryCustom { +} diff --git a/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryCustom.java b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryCustom.java new file mode 100644 index 0000000..6e9ee88 --- /dev/null +++ b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryCustom.java @@ -0,0 +1,9 @@ +package umc.spring.repository.restaurantRepository; + +import umc.spring.domain.Restaurant; + +import java.util.List; + +public interface RestaurantRepositoryCustom { + List dynamicQueryWithBooleanBuilder(String name, Float score); +} \ No newline at end of file diff --git a/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryImpl.java b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryImpl.java new file mode 100644 index 0000000..99d09de --- /dev/null +++ b/spring/src/main/java/umc/spring/repository/restaurantRepository/RestaurantRepositoryImpl.java @@ -0,0 +1,36 @@ +package umc.spring.repository.restaurantRepository; + +import com.querydsl.core.BooleanBuilder; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; +import umc.spring.domain.QRestaurant; +import umc.spring.domain.Restaurant; + + +import java.util.List; + +@Repository +@RequiredArgsConstructor +public class RestaurantRepositoryImpl implements RestaurantRepositoryCustom{ + private final JPAQueryFactory jpaQueryFactory; + private final QRestaurant restaurant = QRestaurant.restaurant; + + @Override + public List dynamicQueryWithBooleanBuilder(String name, Float score) { + BooleanBuilder predicate = new BooleanBuilder(); + + if (name != null) { + predicate.and(restaurant.name.eq(name)); + } + + if (score != null) { + predicate.and(restaurant.score.goe(4.0f)); + } + + return jpaQueryFactory + .selectFrom(restaurant) + .where(predicate) + .fetch(); + } +} \ No newline at end of file