Notice
Recent Posts
Recent Comments
Link
아님말고
[JPA] QueryDSL 조회시 원하는 컬럼만 가져오기 본문
Member.java
@Data
@Entity
@Table(name = "member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
}
MemberDTO.java
@Getter
public class MemberDTO {
private String name;
private String email;
@QueryProjection
public MemberDTO(String name, String email) {
this.name = name;
this.email = email;
}
}
MemberRepository.java
public interface MemberRepository extends JpaRepository<Member, Integer>, MemberRepositoryCustom {
}
MemberRepositoryCustom.java
public interface MemberRepositoryCustom {
List<MemberDTO> findByNameOrEmail(String type, String keyword);
}
MemberRepositoryCustomImpl.java
@RequiredArgsConstructor
public class MemberRepositoryCustomImpl implements MemberRepositoryCustom {
private final JPAQueryFactory jpaQueryFactory;
private final QMember qMember = QMember.member;
@Override
public List<MemberDTO> findByNameOrEmail(String type, String keyword) {
List<MemberDTO> content = jpaQueryFactory
.select(new QMemberDTO(qMember.name, qMember.email))
.from(qMember)
.where(qMember.email.contains(keyword))
.fetch();
return content;
}
}
MemberController.java
@RestController
@RequiredArgsConstructor
public class MemberController {
private final MemberRepository memberRepository;
@GetMapping("members/search")
public ResponseEntity<List<MemberDTO>> selectMemberByNameOrEmail(
@RequestParam String type,
@RequestParam String keyword){
List<MemberDTO> list = memberRepository.findByNameOrEmail(type, keyword);
return ResponseEntity.ok(list);
}
}
'JPA' 카테고리의 다른 글
[JPA] Projection 의 다양한 방법들 (0) | 2022.09.15 |
---|---|
[JPA] QueryDSL 초기 세팅 및 예제 (0) | 2022.09.09 |
[JPA] org.hibernate.hql.internal.ast.QuerySyntaxException: 엔티티명 is not mapped (0) | 2022.08.29 |
[JPA] QueryDSL + multi datasource 설정 (0) | 2022.08.17 |
[JPA] query를 작성하는 방법들 (0) | 2022.08.10 |
Comments