Skip to content

Table view 구현하기 #1

@chulhyun96

Description

@chulhyun96

기능 구현하기

  • prodcut 테이블과 category 테이블, detail_img 테이블을 이용해서 View 만들기

구현 과정에서 생긴 문제 및 버그 발견

  1. view를 만들었음에도 불구하고 상품 등록 기능을 통해 상품을 등록하면 product 테이블에는 데이터가 쌓이는 반면에 view에서는 데이터를 갖고 오지 못하였음
    -> 발견 시점 : Mapper 테스트 코드를 통해 ProductView를 받아오지 못한걸 확인
    @Test
    @DisplayName("리스트 출력 테스트")
    void listProduct() throws Exception {
        List<ProductView> list = repository.findAll();

        Assertions.assertThat(list).isNotEmpty();
        Assertions.assertThat(list.size()).isGreaterThan(0);
    }

문제 원인 및 해결

  1. view 만들 때 기존 코드
create view product_view
as select product.id as id,
          product.name as name,
          product.reg_date as reg_date,
          product.selling_price as selling_price,
          product.supplying_price as supplying_price,
          product.img as img,
          product.description as description,
          c.id as category_id,
          c.name as category_name,
          di.id as di_id,
          di.path as path,
          di.product_id as di_product_id
from product
join category c on product.id = c.id
join detail_img di on product.id = di.product_id;

수정코드

create view product_view
as select product.id as id,
          product.name as name,
          product.reg_date as reg_date,
          product.selling_price as selling_price,
          product.supplying_price as supplying_price,
          product.img as img,
          product.description as description,
          c.id as category_id,
          c.name as category_name,
          di.id as di_id,
          di.path as path,
          di.product_id as di_product_id
from product
left join category c on product.id = c.id
left join detail_img di on product.id = di.product_id;

테이블 join시 left join이 아닌 (inner) join으로 했기 때문에..... 두 테이블에서 일치하는 행반 반환하기 때문에 아무것도 없게 나온것.
left join으로 수정 해 준 후 왼쪽 테이블의 모든 행과 오른쪽 테이블에서 일치하는 행을 잘 반환하는 것을 볼 수 있음.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions