๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ/Spring

domain, entity, dao, dto, vo ??

Salenote์˜ ๊ฒฝ์šฐ WAS๊ฐ€ DB๋ฅผ ๊ตฌ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์ž‘์—…๋งŒ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— (MVP ๊ธฐ์ค€)

JPA๋ฅผ ์‚ฌ์šฉํ•  ์ด์œ ๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ด JDBC Templete๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. 

 

๋ง‰์ƒ Salenote ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋‹ˆ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ class๋ฅผ ๋ถ„๋ฅ˜ํ• ์ง€, ์ด๋ฆ„์€ ๋ญ˜๋กœ ํ• ์ง€ ๊ณ ๋ฏผ์ด ๋๋‹ค.

domain, entity, dao, dto, vo์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•œ ๊ฐœ๋…์ด ์žก๊ธฐ์œ„ํ•ด ๋‹ค์–‘ํ•œ ํฌ์ŠคํŒ…์„ ๋‘˜๋Ÿฌ๋ณธ ๋’ค ๋‚˜๋ฆ„๋Œ€๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.

 

๊ฒฐ๋ก ์ ์œผ๋กœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋กœ์ง์„ ํ˜•์„ฑํ–ˆ๋‹ค.

 

0. client์—๊ฒŒ ์š”์ฒญ์ด ์˜ด

1. controller๊ฐ€ ๋ฐ›์•„ service ๋กœ์ง ์‹คํ–‰

2. service๋Š” dao๋ฅผ ์‹คํ–‰

(์„œ๋น„์Šค ํŠน์„ฑ ์ƒ WAS์—์„œ schema๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์•„ domain์€ ํ•„์š” ์—†์Œ!)

3. SQL๋ฅผ ์ž‘์„ฑํ•ด๋‘” dao์—์„œ query ์‹คํ–‰

3. dao๋กœ ์กฐํšŒํ•œ ๊ฐ์ฒด๋Š” dto๋กœ ๋ฐ˜ํ™˜

4. ๋‹ค์‹œ service์—์„œ dto๋ฅผ client๊ฐ€ ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๋ณ€๊ฒฝ

5. ์ด์˜๊ฒŒ ์ •๋ฆฌํ•œ ๊ฐ์ฒด๋ฅผ client์—๊ฒŒ -> controller

 

 

 

domain & entity
์ฃผ๋กœ JPA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ schema๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
์‹ค์ œ DB์™€ ๋งค์นญ
@Getter
@NoArgsConstructor
@Entity
public class Member {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    private String name;

    private String password;
    
    private String email;
}

 

 

dao
DB ์ปค๋„ฅ์…˜ ํด๋ž˜์Šค
query๋ฅผ ์ž‘์„ฑํ•ด ๋ฐ์ดํ„ฐ๋ฅผ CRUD ํ•˜๋Š” ์šฉ๋„
@Repository
public class GameInfoDao {

    private final JdbcTemplate jdbcTemplate;

    public GameInfoDao(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public GameInfo gameDetailInfoFindById(Long id) {

        String sql = "select * from public.game_id_name where app_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, gameRowMapper(), id);
        } catch (IncorrectResultSizeDataAccessException e) {
            throw new CommonException(ResponseCode.INVALID_GAME_ID);
        }
    }

    private RowMapper<GameInfo> gameRowMapper() {
        return (rs, rowNum) -> {
            GameInfo game = new GameInfo();
            game.setId(rs.getString("id"));
            game.setName(rs.getString("name"));
            game.setDetailJson(rs.getString("detail"));
            return game;
        };
    }
}

 

 

 

๐Ÿ’ฌ dto์™€ vo๊ฐ€ ์ƒ๋‹นํžˆ ์• ๋งคํ•œ ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ ๋‚ด๊ฐ€ ์ฐธ์—ฌํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” dto๋Š” ์™ธ๋ถ€์—์„œ ๋“ค์–ด์˜ค๊ณ  ๋‚˜๊ฐ€๋Š” ํ˜•์‹์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๊ณ  vo๋Š” ์„œ๋น„์Šค ๋กœ์ง ๋‚ด๋ถ€์—์„œ ์˜ค๊ณ  ๊ฐ€๋Š” ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค.

dto
์„œ๋น„์Šค ๋กœ์ง์—์„œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„
vo์™€ ์—ญํ• ์ด ์—„๋ฐ€ํžˆ ๋‚˜๋ˆ„์–ด์ ธ์žˆ์ง€ ์•Š๋‹ค
@Setter
@Builder
public class GameDetailInfoDto {

    @JsonProperty
    private String id;

    @JsonProperty
    private String name;

    @JsonProperty
    private String headerImage;
    
    public static GameDetailInfoDto of(GameDetailInfoVo vo) {
        return builder()
                .id(vo.getId())
                .name(vo.getName())
                .headerImage(vo.getHeaderImage())
                .build();
    }
}

 

 

vo
์„œ๋น„์Šค ๋กœ์ง์—์„œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„
dto์™€ ๋น„์Šทํ•˜์ง€๋งŒ read only๋ผ๋Š” ๊ฐœ๋ฐœ์ž ์‚ฌ์ด์˜ ์•”๋ฌต์ ? ๋ฃฐ์„ ๊ฐ€์ง
@Builder
@Getter
@ToString
public class GameDetailInfoVo {

    private final String id;

    private final String name;

    private final String headerImage;
}

 

 

 

 

๋ฐ˜์‘ํ˜•