스프링 4

[Spring] Entity, DTO, VO 비교하기

스프링을 처음 접할 때 Entity, DTO, VO, Dao, Repository 등등의 용어가 굉장히 헷갈렸다. 그 중에서도 데이터와 가까이(?) 있는 Entity, DTO, VO 세 가지를 비교해보려고 한다. 일단 가장 구분이 쉬운 것은 Entity이다. Entity는 DB와 가장 가깝다. 테이블과 매핑되는 객체이다. 각 id를 통해 entity를 구분한다. entity에는 @Getter와 @Setter가 없다. 대신 메소드를 활용해 변경할 수 있다. 다음으로 VO는 객체의 불변성을 보장하는 객체이다. 서로 다른 이름을 가지고 있는 객체더라도 모든 속성 값이 같다면 두 인스턴스는 같다고 할 수 있다. 따라서 VO는 Object 클래스의 equals()와 hashCode()를 오버라이딩 해야한다. (I..

스프링 2021.12.01

[Spring] directory 구성 (계층형/도메인형)

프로젝트를 진행하면 하나의 도메인에 따른 repository, controller, service가 늘어난다. 계층형: repository는 repository끼리 넣고, domain은 domain끼리 넣고.. 이런 프로젝트가 있는 반면. 도메인형: 하나의 도메인 아래 관련 controller, service, repository 등 패키지를 따로 만들어 각각 넣어둘 수도 있다. controller 패키지에 자바 파일이 열개만 넘어가도 알아보기는 힘들다. 패키지는 많아지겠지만, 도메인형으로 구성하면 파일이 많아져도 관련 코드가 모아져 있어 한 눈에 더 보기 쉽다고 느껴졌다. 다음 글이 좋아 메모해둔다. https://cheese10yun.github.io/spring-guide-directory/

스프링 2021.12.01

[Spring] Controller의 파라미터를 받는 여러 가지 방법 (Parameter Mapping 방법)

@PathVariable 중괄호를 활용하여 변수처럼 적고 http://localhost:8080/v1/product/10 과 같이 호출 가능하다. 1 2 3 4 5 6 7 8 9 10 @RestController @RequestMapping(path = "/v1") public class TestController { // 상품 조회 @GetMapping(path = "/product/{productId}") public Product selectProduct(@PathVariable(name = "productId") Integer productId) { return productService.getProductBy(productId); } cs @RequestParam http://localhost:..

스프링 2021.12.01

토비의 스프링 4장 예외

JdbcTemplate 적용 전 1 2 3 public void deleteAll() throws SQLException{ this.jdbcContext.executeSql("delete from users"); } Colored by Color Scripter cs JdbcTemplate 적용 후 1 2 3 public void deleteAll(){ this.jdbcTemplate.executeSql("delete from users"); } Colored by Color Scripter cs jdbcTemplate 적용 후, SQLException은 사라졌다. 예외가 발생하면 그것을 catch 블록을 써서 잡아내고 예외 처리를 해주어야한다. 왜냐하면 어떤 기능이 비정상적인 동작을 하거나 메모리/리소..

스프링 2021.11.22