인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

빈빈님의 프로필 이미지
빈빈

작성한 질문수

스프링 DB 2편 - 데이터 접근 활용 기술

MyBatis 적용2 - 설정과 실행

MyBatis 테스트 오류 질문입니다

작성

·

1.6K

·

수정됨

0

MyBatis 적용2 - 설정과 실행 부분 테스트 오류 발생해서 질문 드립니다

JdbcTemplateV3 까지 잘 되었는데 MyBatis 적용하면 오류 발생합니다.

MyBatis 관련된 모든 Bean 들이 생성안된 것 같아요. 오류 로그가 너무 길어서 일부만 첨부드립니다

 

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController' defined in file

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hello.itemservice.config.MyBatisConfig':

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemMapper'

Caused by: java.lang.ClassNotFoundException: Cannot find class: Item

 

=======ItemMapper.xml=========

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="hello.itemservice.repository.mybatis.ItemMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into item (item_name, price, quantity)
        values (#{itemName}, #{price}, #{quantity})
    </insert>
    <update id="update">
        update item
        set item_name = #{updateParam.itemName},
            price = #{updateParam.price},
            quantity = #{updateParam.quantity}
        where id = #{id}
    </update>
    <select id="findById" resultType="Item">
        select id, item_name, price, quantity
        from item
        where id = #{id}
    </select>
    <select id="findAll" resultType="Item">
        select id, item_name, price, quantity
        from item
        <where>
            <if test="itemName != null and itemName != ''">
                and item_name like concat('%',#{itemName},'%')
            </if>
            <if test="maxPrice != null">
                and price &lt;= #{maxPrice}
            </if>
        </where>
    </select>
</mapper>

답변 2

0

안녕하세요, 빈빈 님. 공식 서포터즈 y2gcoder 입니다.

보내주신 프로젝트 살펴봤고, 말씀해주시는 현상 확인했습니다.

원인은 간단하게 Item이 들어가있는 위치를 잘못 지정해주신 것입니다.

application.properties를 보시면

#MyBatis
mybatis.type-aliases-package=hello.itemservice.domain

해당 설정이 있습니다. 해당 설정은 강의에서도 설명하듯이 타입 정보를 사용할 때 패키지를 지정해주기 위해서 쓰는 설정입니다. 위와 같이 지정하면 hello.itemservice.domain 하위에 있는 클래스들을 Mybatis의 타입으로 사용할 때, 패키지명까지 적어주지 않아도 자동으로 인식하는 것입니다.

보내주신 프로젝트를 보면 domain package가

강의와 같이 hello.itemservice 에 있는 것이 아니라

hello.itemservice.repository 하위에 위치하고 있습니다. 그래서 Mybatis가 Item 을 찾지 못해 에러가 발생했습니다.

강의와 같이 hello.itemservice으로 domain 패키지를 옮겨준 후 다시 테스트를 실행하시면 정상적으로 통과하는 것을 보실 수 있습니다.

image

감사합니다.

빈빈님의 프로필 이미지
빈빈
질문자

저런 실수가 있었네요ㅠㅠ 감사합니다!!!

 

파이팅입니다!

0

안녕하세요. 빈빈님, 공식 서포터즈 y2gcoder입니다.

 

보여주신 부분만으로는 Item을 매핑해주는 쪽에 문제가 생긴 것 같은데 정확한 판단이 어려울 것 같습니다.

혹시 ItemMapper에서 import가 제대로 되었는지 보시고 문제가 없는 것 같으시다면

전체 프로젝트를 압축한 뒤
구글 드라이브로 공유해서 링크를 남겨주세요.

1. 구글 드라이브 업로드 방법

구글 드라이브 업로드 방법
링크

2. 주의사항
구글 드라이브 공유시
권한을 반드시 확인해주세요

3. 아래 내용을 작성 부탁드립니다.
a. 프로젝트 실행 방법을 알려주세요.
b. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

빈빈님의 프로필 이미지
빈빈
질문자

드라이브 링크 공유드립니다

https://drive.google.com/file/d/17jjyuLGg8OEWdtciYkwIBw_Jj_bJol-e/view?usp=sharing

 

MyBatis 적용한 후로

test > java > hello.itemservice > domain > ItemRepositoryTest 실행 시 오류납니다.

테스트 실행 시 콘솔에 오류 로그 발생합니다.

빈빈님의 프로필 이미지
빈빈

작성한 질문수

질문하기