TIL)23.06.27_Spring boot 오류

주민·2023년 6월 27일
0

TIL

목록 보기
27/84

파일 다 만들고 Spring boot 실행했는데 아래와 같은 오류가 나왔다.

내용을 보니 Application 실행 실패 Bean 생성 오류라고 해서 Application 오류로 찾아봤는데 전혀 다른 사유였다... 오류 해결한 내용 정리해서 써봤다.

오류 해결

우선 튜터님께 도움을 구했다. 오류 내용 중에 findAllByOrderByCreatedAtDesc(), createAt이 있어 그 부분을 확인해보니 1.필드명과 DB 컬럼명이 다른 부분이 있었고 2. findAllByOrderByCreatedAtDesc의 필드명이 잘 못 들어가 있었다.

  1. 필드명 == DB 컬럼명
    java은 보통 camelCase 방식으로 쓰고 DB는 snake_case 방식으로 쓰기 때문에 두 개의 필드명은 다른 경우가 많다. 그걸 맞춰주기 위해 java에 @Column에 name으로 지정해주면 된다.
@Column(name="created_at") => 컬럼명 
private LocalDateTime createAt; => 필드명

@Column(name="modified_at")
private LocalDateTime modifiedAt;
  1. findAllByOrderByCreatedAtDesc
    Repository를 통해 DB 조회, 수정 등이 가능하다. 위의 메서드의 경우 CreateAt 필드 전체를 내림차순으로 정렬 하라는 내용인데 필드명인 CreateAt에 오타가 있었다.
* 적용하는 법
▶ BlogRepository interpace

public interface BlogRepository extends JpaRepository<Blog,Long> {
    List<Blog> findAllByOrderByCreateAtDesc();
}

▶ BlogService class

public List<BlogResponseDto> getBlogList() {
    List<Blog> blogList = blogRepository.findAllByOrderByCreateAtDesc();
    List<BlogResponseDto> blogResponseDtoList = new ArrayList<>();
    for(Blog blog : blogList){
        blogResponseDtoList.add(new BlogResponseDto(blog));
    }
    return blogResponseDtoList;
}

0개의 댓글

관련 채용 정보