-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π 1λ¨κ³ - μμ½ κΈ°λ₯ ꡬν #91
base: yhheo96
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μλ
νμΈμ μ€νλ, μμ½ κΈ°λ₯ ꡬν λ―Έμ
μ ν¨κ»ν μ΅νꡬμ
λλ€. πββοΈ
λͺ κ°μ§ μ½λ©νΈ λ¨κ²Όμ΅λλ€! νμΈν΄μ£ΌμΈμ~
ν μ€νΈ μ½λλ₯Ό μ΄λ λ 벨κΉμ§ μμ±μ ν΄μΌνλμ§
ν
μ€νΈ μ½λλ κ²°κ΅ 'λ΄κ° νλ‘κ·Έλ¨μ μ§μ μ€νμμΌλ³΄μ§ μλλΌλ, μ λμνλμ§ μκ³ μΆμ΄.' λΌλ μꡬλ₯Ό ν΄μνκΈ° μν΄ μμ±ν κ±°μμ.
μ€νλμ΄ μ§μ ꡬννμ controller, serivce, repository μ λν΄ λΆμν μμκ° μμΌμ κ°μ? π
μ΄λ² λ―Έμ
μ μ§ννλ λμμ νμ΅μ μν΄μ, κ° κ³μΈ΅λ³ ν
μ€νΈλ₯Ό νλμ© μμ±ν΄λ³΄λ κ²λ μ’μκ±° κ°μμ.
λ€λ§ μ€μ νλ‘κ·Έλ¨μ λ§λμ€ λ μ λ§ μ€μν ν
μ€νΈλ§ μ λΉν μμ±νλκ² μ€μνκ² λ€μ.
λ무 ν
μ€νΈ μ½λλ₯Ό μμ±νμ§ μλλ€λ©΄ νλ‘κ·Έλ¨ μμ μ±μ΄ λ¨μ΄μ§κ² μ§λ§,
λΆνμν ν
μ€νΈκ° λ무 λ§μμ§λ©΄ μ λ§ μ€μν ν
μ€νΈκ° λμ μ λμ§ μμ μλ μμΌλκΉμ!
DTOλ₯Ό μ΄λ λ 벨κΉμ§ μμ±μ ν΄μΌνλμ§
μλ‘ μ μΌλ‘λ controller - service - repository λ₯Ό κ±°μΉλ layered architecture λ₯Ό μ νν΄μ£Όμ
¨μΌλ―λ‘,
νμ¬μ κ°μ΄ κ° κ²½κ³μμ μ¬μ©λλ dto(data transfer object)λ₯Ό μ¬μ©νλκ² μ³λ€κ³ μκ°λ©λλ€.
"layer λ₯Ό λλκ³ μΆλ€." -> "layer κ° μ μ§λ³΄μμ μλ‘ μν₯μ λ°κ³ μΆμ§ μλ€."
νλμ object λ‘ μ¬λ¬ layer κ° μν΅μ μ£Όκ³ λ°λ μκ° μ μ§λ³΄μκ° μ΄λ €μμ§κ±°μμ.
μμ¦μ intellij μ κ°μ IDE μ λ¨μΆμ΄λ github copilot μ λμμ΄ κ°λ ₯ν΄μ
ν° μ΄λ €μ μμ΄ dto ν΄λμ€λ€μ μμκ°μ μ°μ΄λ΄λ κ² κ°μμ.
κΆκΈνμ μ νΈνκ² μ§λ¬Έ λ¨κ²¨μ£ΌμΈμ~
@Builder | ||
@Getter | ||
@Setter | ||
public class CreateReservationCommand { | ||
private String name; | ||
private String date; | ||
private String timeId; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setter κ° νμν μ΄μ κ° μμμκΉμ?
implementation 'org.springframework.boot:spring-boot-starter-jdbc' | ||
implementation 'org.projectlombok:lombok' | ||
annotationProcessor('org.projectlombok:lombok') | ||
runtimeOnly 'com.h2database:h2' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
둬볡μ μ μ©νμ μ΄μ κ° κΆκΈν΄μ!
@Getter | ||
@Setter | ||
public class Reservation { | ||
|
||
private Long id; | ||
private String name; | ||
private String date; | ||
private ReservationTime time; | ||
|
||
public Reservation(Long id, String name, String date, ReservationTime time | ||
) { | ||
this.id = id; | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
entity = id + vo λΌκ³ μκ°νλλ°μ, id λ₯Ό ν΅ν΄μ μλ‘ κ°μ λ°μ΄ν°λΌκ³ μλ³νκΈ° λλ¬Έμ΄μμ.
κ°λ Ή id = 1, name = "μ€ν"
κ³Ό id = 1, name = "νμ€"
μ μλ‘ κ°μ λ°μ΄ν°λ‘,
id = 1, name = "μ€ν"
κ³Ό id = 9, name = "μ€ν"
μ μλ‘ λ€λ₯Έ λ°μ΄ν°λ‘ λ³Ό μ μκ² λ€μ.
μ΄λ₯Ό ννν΄μ£ΌκΈ° μν΄ id μ λν΄μλ§ equals & hashcode override λ₯Ό ν΄μ£Όλ κ²μ μ΄λ¨κΉμ?
|
||
@Repository | ||
public interface ReservationRepository { | ||
public List<Reservation> findAllReservations(); | ||
|
||
public Reservation createReservation(Reservation reservation, ReservationTime reservationTime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μΈν°νμ΄μ€μ @Repository
μ΄λ
Έν
μ΄μ
μ΄ νμν μ΄μ κ° μμμκΉμ?!
public final RowMapper<Reservation> rowMapper = (rs, rowNum) -> { | ||
ReservationTime reservationTime = new ReservationTime( | ||
rs.getLong("time_id"), | ||
rs.getString("time_start_at") | ||
); | ||
|
||
return new Reservation( | ||
rs.getLong("reservation_id"), | ||
rs.getString("reservation_name"), | ||
rs.getString("reservation_date"), | ||
reservationTime | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private μΌλ‘ μ μΈν΄μ€ μ μκ² λ€μ! π
@Override | ||
public ReservationTime findByTimeId(String timeId) { | ||
return jdbcTemplate.queryForObject("SELECT * FROM reservation_time where id = ?", rowMapper, timeId); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ§μ½ λ¨κ±΄ μ‘°νμ μ€ν¨ν κ²½μ° (μκ°μ΄ μλ κ²½μ°) μμΈμ²λ¦¬λ μ΄λ»κ² λ κΉμ?
μκ° μ λ³΄κ° μλ κ²μ persistence μ μ±
μμΌκΉμ, business μ μ±
μμΌκΉμ?
|
||
@Override | ||
public Reservation createReservation(Reservation reservation, ReservationTime reservationTime) { | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
String sql = "insert into reservation(name, date, time_id) values(?, ?, ?)"; | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement pstmt = connection.prepareStatement(sql, new String[]{"id"}); | ||
pstmt.setString(1, reservation.getName()); | ||
pstmt.setString(2, reservation.getDate()); | ||
pstmt.setLong(3, reservationTime.getId()); | ||
return pstmt; | ||
}, keyHolder); | ||
|
||
long key = keyHolder.getKey().longValue(); | ||
reservation.setId(key); | ||
reservation.setTime(reservationTime); | ||
|
||
return reservation; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key holder νμ© π
simple jdbc insert λ₯Ό νμ©ν΄λ³΄λ κ²λ μ’κ² λ€μ π
μλ νμΈμ!
1λ¨κ³κ° λ§μ΄ λ¦μ΄μ‘λ€μ γ γ π’
μ΄λ² λ―Έμ μ ꡬννλ©΄μ κ°μ₯ κ³ λ―Όμ΄ λλ ν¬μΈνΈλ λ κ°μ§ μ λλ€.
ν μ€νΈ μ½λλ₯Ό μ΄λ λ 벨κΉμ§ μμ±μ ν΄μΌνλμ§( μμ§ λ¨μ ν μ€νΈμ κ°μ ν μ€νΈ μ½λλ μμ±νμ§ λͺ»νμμ΅λλ€ π’ )
DTOλ₯Ό μ΄λ λ 벨κΉμ§ μμ±μ ν΄μΌνλμ§