엄지월드

JPA 본문

java/Spring

JPA

킨글 2022. 8. 4. 09:52

 

원하는 Query가 없다면? MemberRepository에 추가

Optional<Member> findByEmailAuthKey(String emailAuthKey);

Optional<Member> findByUserIdAndUserName(String userId, String userName); // And 조건

package com.myapp.lms.member.repository;

import com.myapp.lms.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, String> {
    Optional<Member> findByEmailAuthKey(String emailAuthKey);
    Optional<Member> findByUserIdAndUserName(String userId, String userName);
    Optional<Member> findByResetPasswordKey(String resetPasswordKey);
}

 

@Builder을 활용해서 Member 값을 가져갈 수 있다. 

@Builder 추가 시, 생성자가 필요하기 때문에 @NoArgsConstructor, @AllArgsConstructor를 추가해주어야 한다. 

package com.myapp.lms.member.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.time.LocalDateTime;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Entity
public class Member {
    @Id
    private String userId;
    private String userName;
    private String phone;
    private String password;
    private LocalDateTime regDt;
    private boolean emailAuthYn;
    private String emailAuthKey;
    private LocalDateTime emailAuthDt;
}

기존 Member 소스이다. 

변경된 Member 소스를 보기 위해 다음 단락으로 이동하자. 

Member member = new Member();
member.setUserId(parameter.getUserId());
member.setUserName(parameter.getUserName());
member.setPhone(parameter.getPhone());
member.setPassword(parameter.getPassword());
member.setRegDt(LocalDateTime.now());
member.setEmailAuthYn(false);
member.setEmailAuthKey(UUID.randomUUID().toString());

기존 Member 소스를 아래와 같이 변경 가능하다. 

Member member = Member.builder()
                .userId(parameter.getUserId())
                .userName(parameter.getUserName())
                .phone(parameter.getPhone())
                .password(parameter.getPassword())
                .regDt(LocalDateTime.now())
                .emailAuthYn(false)
                .emailAuthKey(UUID.randomUUID().toString())
                .build();

 

Comments