MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[ํ”„๋กœ์ ํŠธ] Mybatis(DTO) -> JPA(Entity) ๋กœ ๋ณ€ํ™˜ ํ•˜๊ธฐ

HYEJU01 2024. 12. 30. 21:54
 

๋ชฉ์ฐจ

 

     

     

     

     

    ์˜ˆ์ „์— ์ง„ํ–‰ํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ(Mybatis DTO)๋ฅผ JPA ๋กœ ๋ณ€ํ™˜ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. 

    ์ „์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ์—” ์‹œ๊ฐ„์ ์œผ๋กœ ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์•„์„œ๐Ÿ˜ฐ

    ์—ฐ์Šต์ฐจ์›์—์„œ ๊ฐ„๋‹จํ•œ ์กฐํšŒ ๊ตฌ๋ฌธ๋งŒ ๋ณ€ํ™˜ ํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.

     

     

     

     

     

    ์ฐธ๊ณ ์šฉ ERD ์™€ ๊ธฐ์กด Mapper

     

     

     

    1)  ๊ธฐ์กด EmployeeDTO ๋ฅผ Entity ๋กœ ๋ณ€๊ฒฝ

     

    ์ด๋•Œ DTO ์—์„œ๋Š” authorityName ์„ ์ง์ ‘ ๋ฐ›์•˜์—ˆ๋Š”๋ฐ 

     

     

    JPA ์—์„œ๋Š” Authority ๊ฐ์ฒด๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ์ „๋ถ€ ์ˆ˜์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

     


     

    2) Employee Repository ์ƒ์„ฑ

     

    • JpaRepository ๋ฅผ ์ƒ์† ๋ฐ›์œผ๋ฉด , ๊ธฐ๋ณธ CRUD ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์ œ๊ณต๋ผ์„œ ๊ธฐ๋ณธ์ ์ธ crud ๋Š” ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ex) findAll() , findByAll() ๋“ฑ๋“ฑ 

     

     

     

     

     

    ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ปค์Šคํ…€ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ์„ค๋ช…. 

    • ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ(Query Methods) ๋ผ๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ๋Š”๋ฐ,  ๋ฉ”์„œ๋“œ ์ด๋ฆ„๋งŒ์œผ๋กœ ์ž๋™์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ๊ตฌ์„ฑํ•˜๋ฉด ์ž๋™์œผ๋กœ JPA ๊ฐ€ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์›๋ฆฌ์ด๋‹ค.

    ์ •๋ ฌ์€ ์•ˆ๋˜์–ด์žˆ๊ณ  ์กฐํšŒ + ํ™œ์„ฑํ™”๋œ(Y) ๋œ ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜จ ์ƒํƒœ 

     


    • @Query ์• ๋„ˆํ…Œ์ด์…˜์€ ๊ฐœ๋ฐœ์ž๊ฐ€ JPQL(Java Persistence Query Language) ๋˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. 

     

    ์ •๋ ฌ์€ ์•ˆ๋˜์–ด์žˆ๊ณ  ์กฐํšŒ+ ํ™œ์„ฑํ™”๋œ(Y) + ์ •๋ ฌ๊นŒ์ง€ ๋œ ์ƒํƒœ (๊ธฐ์กด ์ฟผ๋ฆฌ๋ž‘ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๊ฒŒ ๋จ)


     

     

    package com.project.tobe.repository;
    
    import com.project.tobe.entity.Employee;
    import com.project.tobe.entity.Authority;
    import org.springframework.context.annotation.Primary;
    import org.springframework.stereotype.Repository;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.criteria.*;
    import java.util.List;
    
    @Repository
    @Primary
    public class EmployeeCustomRepositoryImpl implements EmployeeCustomRepository {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        @Override
        public List<Employee> findEmployeesByAuthorityOrdered() {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
            Root<Employee> employee = cq.from(Employee.class);
    
            // Authority ์—”ํ‹ฐํ‹ฐ์™€์˜ Join
            Join<Employee, Authority> authority = employee.join("authority", JoinType.INNER);
    
            // authorityGrade์— ๋Œ€ํ•œ ์ •๋ ฌ ๊ธฐ์ค€
            Expression<Object> authorityOrder = cb.selectCase()
                    .when(cb.equal(authority.get("authorityGrade"), "S"), 1)
                    .when(cb.equal(authority.get("authorityGrade"), "A"), 2)
                    .when(cb.equal(authority.get("authorityGrade"), "B"), 3)
                    .when(cb.equal(authority.get("authorityGrade"), "C"), 4)
                    .when(cb.equal(authority.get("authorityGrade"), "D"), 5);
    
            // ์ฟผ๋ฆฌ ์ž‘์„ฑ
            cq.select(employee)
                    .where(cb.equal(employee.get("emplYn"), "Y")) // ์ง์› ์ƒํƒœ๊ฐ€ 'Y'์ธ ๊ฒฝ์šฐ๋งŒ ํ•„ํ„ฐ๋ง
                    .orderBy(cb.asc(authorityOrder)); // authorityGrade์— ๋”ฐ๋ฅธ ์ •๋ ฌ
    
            // ์ฟผ๋ฆฌ ์‹คํ–‰ ๋ฐ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
            return entityManager.createQuery(cq).getResultList();
        }
    }

     

     

    • Criteria API๋ฅผ ํ†ตํ•ด JPA์—์„œ ๋™์ ์œผ๋กœ ์ฟผ๋ฆฌ  ์ž‘์„ฑ (์ปค์Šคํ…€ ๋ ˆํฌ์ง€ํ† ๋ฆฌ)  :  ์ปค์Šคํ…€ ๋ ˆํฌ interface + ๊ตฌํ˜„ ํ›„  ๊ธฐ๋ณธ ๋ ˆํฌ์— interface ๋ฅผ ์ƒ์† ๋ฐ›๋Š” ๋ฐฉ์‹์ด๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ @Query ๋กœ๋Š” ๊ตฌํ˜„์ด ํž˜๋“  ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋‚˜ ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค .

    ๊ธฐ์กด ์ฟผ๋ฆฌ๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž˜ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

     


     

    3) DTO๋กœ ์‚ฌ์šฉ๋˜๋˜ ๋ถ€๋ถ„๋“ค์„ ์ „๋ถ€ Entity๋กœ ๋ณ€๊ฒฝ

     

     

    • controller

     

     

    • service

     

     

    • serviceImpl

     

    repository ์‚ฌ์šฉ์„ ์œ„ํ•ด ์ฃผ์ž…์„ ํ•ด์ค๋‹ˆ๋‹ค.

     

     

     

    jpa ๋ฉ”์„œ๋“œํ˜ธ์ถœ

     

     

     

    jpa ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ์ž๋™์œผ๋กœ ์ด๋ ‡๊ฒŒ ์ฟผ๋ฆฌ๋ฌธ์ด ์ฝ˜์†”์ฐฝ์— ๋œฌ๋‹ค.

     

     

     

    (+) ์ฟผ๋ฆฌ๋ฌธ์€ application.properties ์— ํ•ด๋‹น ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ณผ ์ˆ˜ ์žˆ๋‹ค

    # Hibernate์˜ SQL ์ฟผ๋ฆฌ ์ถœ๋ ฅ ํ™œ์„ฑํ™”
    spring.jpa.show-sql=true
    
    # SQL ์ฟผ๋ฆฌ์˜ ํ˜•์‹์„ ์ข€ ๋” ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅ (์˜ต์…˜)
    spring.jpa.properties.hibernate.format_sql=true
    
    # ์‹คํ–‰๋œ SQL ์ฟผ๋ฆฌ์™€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถœ๋ ฅ (Hibernate์˜ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ DEBUG๋กœ ์„ค์ •)
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql=trace

     

     

     


     

    (+) React ์š”์ฒญ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ณ€๊ฒฝ

     

     

     

    select ๋Š” ์ƒ๊ด€ ์—†์ง€๋งŒ 

    entity ์™€ ๋™์ผํ•œ ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝ => authority ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋ฐ›๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผํ•œ๋‹ค.