5. Jpa 를 이용한 UserEntity 생성 및 공통 annotation 생성
처음으로 userEntity를 생성해보았다. 아래와 같다
@Entity
@Table(name = "user")
@Data
@EqualsAndHashCode(callSuper = true) //부모클래스도 비교 가능하게 된다
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class UserEntity extends BaseEntity {
@Column(length = 50, nullable = false)
private String name;
@Column(length = 100, nullable = false)
private String email;
@Column(length = 100, nullable = false)
private String password;
@Column(columnDefinition = "varchar(50)", nullable = false) //예외 중요!!
@Enumerated(EnumType.STRING)
private UserStatus status;
@Column(length = 150, nullable = false)
private String address;
private LocalDateTime registeredAt;
private LocalDateTime unregisteredAt;
private LocalDateTime lastLoginAt;
}
다음으로 UserStatus를 정의하는 enum클래스를 만들었다.
@AllArgsConstructor
public enum UserStatus {
REGISTERED("Register"),
UNREGISTERED("UnRegister"),
;
private final String description;
}
UserRepository 는 다음과 같다
public interface UserRepository extends JpaRepository<UserEntity,Long> {
//select * from user where id = ? and status = ? order by id desc limit 1
Optional<UserEntity> findFirstByIdAndStatusOrderByIdDesc(Long userId, UserStatus status);
//select * from user where email = ? and password = ? and status =? order by id desc limit 1
Optional<UserEntity> findFirstByEmailAndPasswordAndStatusOrderByIdDesc(String email, String password , UserStatus status);
}
공통 어노테이션 만들기
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Service
public @interface Converter {
@AliasFor(annotation = Service.class)
String value() default "";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Service
public @interface Business {
@AliasFor(annotation = Service.class)
String value() default "";
}