QueryDSL 시작하기

By | 2022년 7월 22일
Table of Contents

QueryDSL 시작하기

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

QueryDslConfig

@Configuration
public class QueryDslConfig {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}

Company

@Getter
@Setter
@Entity
@NoArgsConstructor
@Table(name = "tbl_company", catalog = "db_account")
public class Company extends BaseTimeEntity implements Persistable<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long companyId;

    private String companyLoginId;

    private String companyName;

    @Column(nullable = false)
    @Convert(converter = YNTypeConverter.class)
    private YNType isUsing = YNType.Y;

    @Override
    public Long getId() {
        return companyId;
    }

    @Override
    public boolean isNew() {
        return getRegdate() == null;
    }
}

CompanyRepositoryCustom

클래스명은 아무 이름이나 지어도 상관없지만, 보통은 Repository명 + Custom 으로 합니다.

public interface CompanyRepositoryCustom {

    List<Company> search();
}

CompanyRepositoryImpl

클래스명은 Repository명 + Impl 이어야 합니다.

import static kr.pe.skyer9.warehouse.account.domain.QCompany.company;

public class CompanyRepositoryImpl extends QuerydslRepositorySupport implements CompanyRepositoryCustom {

    private final JPAQueryFactory jpaQueryFactory;

    public CompanyRepositoryImpl(JPAQueryFactory jpaQueryFactory) {
        super(Company.class);
        this.jpaQueryFactory = jpaQueryFactory;
    }

    @Override
    public List<Company> search() {
        System.out.println("DSL Called");
        return jpaQueryFactory
                .selectFrom(company)
                .where(company.companyLoginId.eq("string")).fetch();
    }
}

CompanyRepository

public interface CompanyRepository extends JpaRepository<Company, Long>, CompanyRepositoryCustom {
}

답글 남기기