Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JDBC
- transaction
- springdatajpa
- 김영한
- Servlet
- spring
- Thymeleaf
- java
- 인프런
- Spring Boot
- 스프링
- 백준
- kotlin
- JPQL
- SpringBoot
- db
- QueryDSL
- 스프링 핵심 기능
- AOP
- Proxy
- 그리디
- 스프링 핵심 원리
- http
- 자바
- pointcut
- Exception
- Greedy
- jpa
- Android
- 알고리즘
Archives
- Today
- Total
개발자되기 프로젝트
[Server] 회원 도메인 개발 본문
- 먼저 기억을 되살리기 위해 회원 도메인부터 개발해보자.
1.Member
@Entity
@Getter
public class Member {
@Id
@GeneratedValue
private Long id;
private String loginId;
private String loginPw;
public Member() {
}
public Member(String loginId, String loginPw) {
this.loginId = loginId;
this.loginPw = loginPw;
}
}
2. MemberRepository
import java.util.List;
import java.util.Optional;
public interface MemberRepository {
void saveMember(Member member);
Optional<List<Member>> findAll();
Optional<Member> findMember(Long id);
Optional<Member> findMember(String loginId);
void deleteMember(Long id);
void deleteMember(String loginId);
}
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;
@Repository
@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepository{
@PersistenceContext
private final EntityManager em;
@Override
public void saveMember(Member member) {
em.persist(member);
}
@Override
public Optional<List<Member>> findAll() {
return Optional.ofNullable(em.createQuery("select m from Member m", Member.class).getResultList());
}
@Override
public Optional<Member> findMember(Long id) {
return em.createQuery("select m from Member m where m.id = :id",Member.class)
.setParameter("id", id)
.getResultList().stream().findAny();
}
@Override
public Optional<Member> findMember(String loginId) {
return em.createQuery("select m from Member m where m.loginId = :loginId",Member.class)
.setParameter("loginId", loginId)
.getResultList().stream().findAny();
}
@Override
public void deleteMember(Long id) {
em.remove(findMember(id));
}
@Override
public void deleteMember(String loginId) {
em.remove(findMember(loginId));
}
}
- JpaRepository를 상속받아 편하게 할 수 도 있지만.. 학습을 위해 순수 JPA를 사용했다.
3. MemberService
public interface MemberService {
Member addMember(Member member);
Member findMember(Long id);
Member findMember(String loginId);
void deleteMember(Long id);
void deleteMember(String loginId);
}
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.NoSuchElementException;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository;
@Override
@Transactional
public Member addMember(Member member) {
memberRepository.saveMember(member);
return member;
}
@Override
public Member findMember(Long id) {
return memberRepository.findMember(id).orElseThrow(NoSuchElementException::new);
}
@Override
public Member findMember(String loginId) {
return memberRepository.findMember(loginId).orElseThrow(NoSuchElementException::new);
}
@Override
@Transactional
public void deleteMember(Long id) {
memberRepository.deleteMember(id);
}
@Override
@Transactional
public void deleteMember(String loginId) {
memberRepository.deleteMember(loginId);
}
}
4. 기타 설정
- h2 db 설정, hibernate 설정 및 log설정을 해주었다.
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/commuteMap
username: sa
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
format_sql: true
logging.level:
org.hibernate.SQL: debug
5. Test
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Rollback(value = true)
class MemberServiceTest {
@Autowired
MemberRepository memberRepository;
@Autowired
MemberService memberService;
@Autowired
EntityManager em;
/**
* DB에 잘 저장되는지, 잘 불러와지는지 확인하기 위한 목적.
*/
@Test
@Transactional
void crudTest(){
//given
Member member1 = new Member("loginId", "password");
//when
memberService.addMember(member1);
//entityManager 초기화 -> 캐시삭제
em.flush();
em.clear();
Member findMember = memberService.findMember(member1.getLoginId());
//then
assertThat(findMember.getLoginId()).isEqualTo(member1.getLoginId());
assertThat(findMember.getLoginPw()).isEqualTo(member1.getLoginPw());
}
}
6. GitHub: 220115 MemberDomain
'Project > 대중교통 길찾기' 카테고리의 다른 글
[Server] Odsay API호출 문제 해결 (0) | 2022.01.18 |
---|---|
[Server] Odsay 대중교통 api 연동 (0) | 2022.01.17 |
[Server] 회원 도메인 설계 (0) | 2022.01.15 |
[Server] 도메인 분석 설계 (0) | 2022.01.15 |
[Server] ver.1 요구사항 (0) | 2022.01.15 |
Comments