Junit Test Application-43-입출금내역 JPQL 동적 쿼리 체크

jaegeunsong97·2023년 8월 10일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

디폴트 : 전체 내역 가져오기

입금 click : 입금에 관한 내용만 가져오기

출금 click : 출금에 관한 내용만 가져오기

동적쿼리로 해결!

package shop.mtcoding.bank.domain.transaction;

import java.util.List;

import org.springframework.data.repository.query.Param;

public class TransactionRepositoryImpl{
}

interface 추가 및 구현

package shop.mtcoding.bank.domain.transaction;

import java.util.List;

import org.springframework.data.repository.query.Param;

interface Dao {
     List<Transaction> findTransactionList(@Param("accountId") Long accountId,
               @Param("gubun") String gubun,
               @Param("page") Integer page);
}

public class TransactionRepositoryImpl implements Dao{

     @Override
     public List<Transaction> findTransactionList(Long accountId, String gubun, Integer page) {
          throw new UnsupportedOperationException("Unimplemented method 'findTransactionList'");
     }
}

package shop.mtcoding.bank.domain.transaction;

import org.springframework.data.jpa.repository.JpaRepository;

public interface TransactionRepository extends JpaRepository<Transaction, Long>, Dao {

}

추상적인 것에 의존하는 완벽한 코드 ㅎㅅㅎ

물론 TransactionRepository에 바로 작성하고 @Qeury로 하면 되지만 이렇게 할 시, 동적쿼리를 만들 수 없다. 따라서 interface를 implements하고 구현을 해야한다.

gubun 값을 가지고 동적쿼리를 생성하자

package shop.mtcoding.bank.domain.transaction;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.TypedQuery;

import lombok.RequiredArgsConstructor;

import org.springframework.data.repository.query.Param;

interface Dao {
     List<Transaction> findTransactionList(@Param("accountId") Long accountId,
               @Param("gubun") String gubun,
               @Param("page") Integer page);
}

@RequiredArgsConstructor
public class TransactionRepositoryImpl implements Dao {

     private final EntityManager em;

     @Override
     public List<Transaction> findTransactionList(Long accountId, String gubun, Integer page) {
          // DEPOSIT, WITHDRAW, ALL / JPQL
          String sql = "";
          sql += "select t from Transaction t ";

          if (gubun.equals("WITHDRAW")) {
               sql += "join fetch t.withdrawAccount wa ";
               sql += "where t.withdrawAccount.id = :withdrawAccountId";
          } else if (gubun.equals("DEPOSIT")) {
               sql += "join fetch t.depositAccount da ";
               sql += "where t.depositAccount.id = :depositAccountId";
          } else {
               sql += "left join fetch t.withdrawAccount wa ";
               sql += "left join fetch t.depositAccount da ";
               sql += "where t.withdrawAccount.id = :withdrawAccountId ";
               sql += "or ";
               sql += "t.depositAccount.id = :depositAccountId";
          }

          TypedQuery<Transaction> query = em.createQuery(sql, Transaction.class); // createQuery vs createNativeQuery

          if (gubun.equals("WITHDRAW")) {
               query = query.setParameter("withdrawAccountId", accountId);
          } else if (gubun.equals("DEPOSIT")) {
               query = query.setParameter("depositAccountId", accountId);
          } else {
               query = query.setParameter("withdrawAccountId", accountId);
               query = query.setParameter("depositAccountId", accountId);
          }

          query.setFirstResult(page * 5);
          query.setMaxResults(5);

          return query.getResultList();
     }
}

JPQL 문법을 사용, 경우에 따라서 join fetch, left join fetch를 사용

그리고 JPQL이기 때문에 em.createQuery를 해야한다. em.createNativeQuery는 말그대로 원래 우리가 알고 있는 SQL문법을 사용한다.

그리고 마지막 2줄 코드는 페이징

profile
블로그 이전 : https://medium.com/@jaegeunsong97

1개의 댓글

comment-user-thumbnail
2023년 8월 10일

정보에 감사드립니다.

답글 달기