๐ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฑ์๋ ๋ฐ๋ธ์ฝ์ค 4๊ธฐ ๊ต์ก๊ณผ์ ์ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ธ์
๋๋ค.๐
SpringDataJPA
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:~/order;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
jpa:
open-in-view: false
hibernate:
ddl-auto: create-drop
show-sql: true
properties:
hibernate.format_sql: true
- ์คํ๋ง์์ JPA๋ฅผ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ์ง์
- ์ง์ ๋ฑ๋กํด์ผ ํ๋, JPA ๊ด๋ จ ์ค์ (๋น)์ yml ํ์ผ์ ํตํด ์๋์ผ๋ก ๋ฑ๋ก
- ๋ฐ์ดํฐ ์์ค, ์ํฐํฐ ๋งค๋์ , ํธ๋์ญ์
๋งค๋์ ์ค์
public interface OrderRepository extends JpaRepository<Order, String> {
}
- ๋ฐ์ดํฐ ์ ์ฅ ๊ณ์ธต์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ง์ํด์ค์ CRUD ์์
์ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉ
๋ฉ์๋ ์ฟผ๋ฆฌ
public interface OrderRepository extends JpaRepository<Order, String> {
List<Order> findAllByOrderStatus(OrderStatus orderStatus);
List<Order> findAllByOrderStatusOrderByOrderDatetimeAsc(OrderStatus orderStatus);
List<Order> findAllByOrderStatusOrderByTotalAmountDesc(OrderStatus orderStatus);
List<Order> findAllByOrderStatusAndTotalAmountGreaterThanEqual(OrderStatus orderStatus, BigDecimal totalAmount);
List<Order> findAllByOrderStatusOrOrderStatus(OrderStatus orderStatus1, OrderStatus orderStatus2);
List<Order> findAllByOrderStatusNot(OrderStatus orderStatus);
List<Order> findAllByOrderStatusAndTotalAmountLessThan(OrderStatus orderStatus, BigDecimal totalAmount);
List<Order> findAllByOrderStatusAndOrderDatetimeBefore(OrderStatus orderStatus, LocalDateTime dateTime);
}
- ๋ฉ์๋ ์ด๋ฆ ์์ฒด๋ก ์ฟผ๋ฆฌ๋ก ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅด ์กฐํํ๋ ๋ฐฉ๋ฒ
- ๋ณ๋์ JPQL ๋๋ ๋ค์ดํฐ๋ธ SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ง ์๊ณ ๋ ๊ฐ๋จํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์กฐํ
- ๋ฉ์๋ ์ด๋ฆ์ find, read, get, query, stream, count, exists ๋ฑ์ผ๋ก ์์ํ๋ค.
- By ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์ ์กฐ๊ฑด์ ์ง์
- ๊ฒ์ ์กฐ๊ฑด์๋ ์ํฐํฐ์ ํ๋ ์ด๋ฆ ์ฌ์ฉ
- ๊ฒ์ ์กฐ๊ฑด์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ And ๋๋ Or ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์กฐํฉ
- ์ ๋ ฌ ์กฐ๊ฑด์ ์ถ๊ฐํ ์ ์์ผ๋ฉฐ, OrderBy ํค์๋ ์ฌ์ฉ
์ปค์คํ
์ฟผ๋ฆฌ
public interface OrderRepository extends JpaRepository<Order, String> {
@Query("SELECT o FROM Order o WHERE o.orderStatus = 'COMPLETED' AND o.totalAmount >= :amount AND o.memo LIKE %:keyword%")
List<Order> findCompletedOrdersByAmountAndMemoContaining(@Param("amount") BigDecimal amount, @Param("keyword") String keyword);
@Query("SELECT o FROM Order o WHERE o.user = :user AND o.orderStatus = 'PENDING' ORDER BY o.totalAmount DESC")
Order findHighestAmountPendingOrderByUser(@Param("user") User user);
@Query("SELECT o FROM Order o WHERE o.user = :user AND o.orderDatetime >= :date")
List<Order> findOrdersByUserAndOrderDatetimeAfter(@Param("user") User user, @Param("date") LocalDateTime date);
@Query("SELECT o FROM Order o JOIN o.orderItems oi WHERE oi.product = :product")
List<Order> findOrdersByProduct(@Param("product") Product product);
@Query("SELECT o FROM Order o WHERE o.user = :user AND o.orderStatus = 'CANCELED' AND o.totalAmount <= :amount")
List<Order> findCanceledOrdersByUserAndAmountLessThanOrEqual(@Param("user") User user, @Param("amount") BigDecimal amount);
@Query("SELECT o FROM Order o WHERE o.user = :user AND o.orderStatus = 'DELIVERED' AND o.orderDatetime BETWEEN :startDate AND :endDate")
List<Order> findDeliveredOrdersByUserAndOrderDatetimeBetween(@Param("user") User user, @Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
}
- ์์ ๋ฉ์๋ ์ฟผ๋ฆฌ ๋ฐฉ๋ฒ์ผ๋ก ํ ์ ์๋ ๋ณต์กํ ์กฐํ๋ ์์ ์์
์ ์ง์ @Query๋ฅผ ํตํด JPQL ๋๋ ๋ค์ดํฐ๋ธ SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ ์คํ
- ์กฐ์ธ์ ์ฌ์ฉํ๋ ๋ณต์กํ ์กฐํ
- ์ง๊ณ ํจ์๋ฅผ ์ฌ์ฉํ๋ ์กฐํ
- ๋์ ์ฟผ๋ฆฌ
- ์ฌ๋ฌ ํ
์ด๋ธ์ ํ๋ฒ์ ์์ ํด์ผ ํ๋ ๋ณต์กํ ์์ ์์
QueryDSL
QUser qUser = QUser.user;
User user = new JPAQueryFactory(entityManager)
.selectFrom(qUser)
.where(qUser.name.eq("John"))
.fetchOne();
List<User> users = new JPAQueryFactory(entityManager)
.selectFrom(qUser)
.where(qUser.age.goe(20))
.fetch();
List<Tuple> userTupleList = new JPAQueryFactory(entityManager)
.select(qUser.name, qUser.email)
.from(qUser)
.fetch();
List<Tuple> aggregatedData = new JPAQueryFactory(entityManager)
.select(qUser.age.avg(), qUser.age.max(), qUser.age.min())
.from(qUser)
.groupBy(qUser.gender)
.fetch();
- ๊ณต์ ๋ฌธ์
- ๋น๋ ํจํด ๋ฐฉ์์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ฌ SQL๊ณผ JPQL์ ์ฝ๊ฒ ์์ฑํ ์ ์๋๋ก ์ง์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- JPA์ ํจ๊ป ์ฌ์ฉ๋๋ฉฐ, ๋ณต์กํ ์ฟผ๋ฆฌ๋ฅผ ์๋ฐ ์ฝ๋๋ก ๊ฐํธํ๊ฒ ์์ฑ ๊ฐ๋ฅ
- ๋์ ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์