우선 Member를 담아둘 공간이 필요하기 때문에 Repository를 만들어주었습니다.
Repository이므로 @Repository를 붙여주었습니다.
그리고 EntityManager에는 본래 @PersistenceContext나 @Autowired를 붙여주어야 하지만 생략가능하기때문에 생략하였습니다.
@Repository
@RequiredArgsConstructor
public class MemberRepository {
private final EntityManager em;
//저장
public void save(Member member) {
em.persist(member);
}
//아이디 조회
public Long findId(String loginId, String password) {
return em.createQuery("SELECT M FROM Member M WHERE M.loginId = :loginId AND M.password = :password", Member.class)
.setParameter("loginId", loginId)
.setParameter("password", password)
.getSingleResult()
.getId();
}
public Member findOne(Long id) {
return em.find(Member.class, id);
}
public List<Member> findAll() {
return em.createQuery("SELECT Member FROM Member", Member.class)
.getResultList();
}
//중복 체크
public List<Member> findByLoginId(String loginId) {
return em.createQuery("SELECT M FROM Member AS M WHERE M.loginId = :loginId", Member.class)
.setParameter("loginId", loginId)
.getResultList();
}
public List<Member> findByNickName(String nickName) {
return em.createQuery("SELECT M FROM Member AS M WHERE M.nickname = :nickName", Member.class)
.setParameter("nickName", nickName)
.getResultList();
}
}
findByLoginId와 findByNickNames는 나중에 중복 체크할때 사용하기때문에 사용해주었습니다.