๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฝ๏ธ ํ”„๋กœ์ ํŠธ/ํ”„๋กœ์ ํŠธ ์ผ๊ธฐ๐Ÿ‘€

[RB] ํ”„๋กœ์ ํŠธ ์ดˆ๋ฐ˜ ์ž‘์—… ์ˆœ์„œ ๊ธฐ๋ก

by OR15A 2023. 12. 29.
  • ERD ์ž‘์„ฑ
  • ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค ์ƒ์„ฑ
  • ๋ฒ ์ด์Šค ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ(์‹œ์Šคํ…œ ์ปฌ๋Ÿผ)
  • ๋กฌ๋ณต ์• ๋„ˆํ…Œ์ด์…˜(@Setter์ œ์™ธ)
  • ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘
  • ๊ฐ ์—”ํ‹ฐํ‹ฐ์— ๋งž๋Š” ์ƒ์„ฑ์ž 
  • ๋น„์ฆˆ๋‹ˆ์Šค์— ๋งž๋Š” ๋ณ€๊ฒฝ ์‚ญ์ œ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
  • JpaRepository ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค ์ ์šฉ ์ˆœ์ˆ˜ JPA๋กœ ๊ตฌํ˜„ํ•œ MemberJpaRepository ๋Œ€์‹ ์—
์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์šฉ
์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA ๊ธฐ๋ฐ˜ MemberRepository
public interface MemberRepository extends JpaRepository<Member, Long> { }
  • Querydsl์ด์šฉํ•ด์„œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋จผ์ € ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ
public class MemberRepositoryImpl implements MemberRepositoryCustom {
     private final JPAQueryFactory queryFactory;
     public MemberRepositoryImpl(EntityManager em) {
     	this.queryFactory = new JPAQueryFactory(em);
     }
     
     @Override
     //ํšŒ์›๋ช…, ํŒ€๋ช…, ๋‚˜์ด(ageGoe, ageLoe)
     public List<MemberTeamDto> search(MemberSearchCondition condition) {
         return queryFactory
                         .select(new QMemberTeamDto(
                                                     member.id,
                                                     member.username,
                                                     member.age,
                                                     team.id,
                                                     team.name))
                         .from(member)
                         .leftJoin(member.team, team)
                         .where(usernameEq(condition.getUsername()),
                         		teamNameEq(condition.getTeamName()),
                        		ageGoe(condition.getAgeGoe()),
                       	 		ageLoe(condition.getAgeLoe()))
                         .fetch();
     }
     
     private BooleanExpression usernameEq(String username) {
     	return isEmpty(username) ? null : member.username.eq(username);
     }
     
     private BooleanExpression teamNameEq(String teamName) {
     	return isEmpty(teamName) ? null : team.name.eq(teamName);
     }
     
     private BooleanExpression ageGoe(Integer ageGoe) {
     	return ageGoe == null ? null : member.age.goe(ageGoe);
     }
     
     private BooleanExpression ageLoe(Integer ageLoe) {
     	return ageLoe == null ? null : member.age.loe(ageLoe);
     }
}
  • ํ•ต์‹ฌ๊ธฐ๋Šฅ ๊ฐœ๋ฐœํ•˜๊ธฐ
  • ๊ฒŒ์ž„์นดํ…Œ๊ณ ๋ฆฌ, ๊ฒŒ์ž„๋ฐ์ดํ„ฐ, ๊ฒŒ์ž„์ปจํ…์ธ , ๊ฒŒ์ž„์ด๋ฏธ์ง€ ๋จผ์ € ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ
  • ๊ฒŒ์ž„์นดํ…Œ๊ณ ๋ฆฌ๋Š” DB์— ์ง์ ‘ ์ž…๋ ฅ
  • ๊ฒŒ์ž„๋ฐ์ดํ„ฐ, ์ปจํ…์ธ , ์ด๋ฏธ์ง€๋Š” Dto์—์„œ ๊ฐ’ ๋ฐ›์•„์„œ ๊ฒŒ์ž„๋จผ์ €์ƒ์„ฑ -> (์ปจํ…์ธ  ์ƒ์„ฑ , ์ด๋ฏธ์ง€ ์ƒ์„ฑ)  ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ
  • ํƒ€์ž„๋ฆฌํ”„๋กœ ๊ฒŒ์ž„ ๋“ฑ๋ก ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ
  • ํƒ€์ž„๋ฆฌํ”„ -> API -> ์„œ๋น„์Šค -> ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ -> ์„œ๋น„์Šค -> ์ปจํŠธ๋กค๋Ÿฌ