• [Mybatis] org.xml.sax.SAXParseException; 에러 해결

    2021. 2. 4.

    by. 꼬마봄이

    Mybatis 사용 중 생겼던 간단한 에러 로그 정리

     

     

    ▶ 에러 발생 상황

    Mybatis의 기본 xml 파일인 configuration.xml 과 mapper.xml 파일을 만들고

    Main 클래스 파일을 실행했더니 에러 발생!

     

     

     

    ▶ 에러 내용

    org.apache.ibatis.exceptions.PersistenceException:

    ### Error building SqlSession.

    ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 16; 문서 루트 요소 "configuration"은(는) DOCTYPE 루트 "mapper"과(와) 일치해야 합니다.

     

     

    대충 봤을 때 mapper와 configuration 파일의 오류인 것 같은 느낌이었다.

    거기에 DOCTYPE를 언급한 것 같아서 두 파일의 DOCTYPE을 확인했다.

     

     

    mapper.xml
    1
    2
    3
    4
    <?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">
    cs

     

    configuration.xml
    1
    2
    3
    4
    <?xml version="1.0" encoding="UTF-8"?>
     
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    cs

     

     

    ▶ 문제 해결

    configuration.xml의 DOCTYPE 앞에 쓰인 mapper -> configuration 으로 고쳐주면 된다.

     

     

    수정 후 configuration.xml
    1
    2
    3
    4
    <?xml version="1.0" encoding="UTF-8"?>
     
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    cs

     

     

    DOCTYPE을 복붙 해서 쓰다가 생긴 에러였다.. ㅜㅜ

    mapper로 되어있는 부분은 모두 config 또는 configuration 으로 바꿔서 기입해야 한다.

     

    그리고 Mybatis를 사용할 때, 두 DOCTYPE 링크는 자주 쓰이니 따로 메모장에 적어두고 사용할 때마다 복붙을 해주면 편리하다.

    (특히 mapper DOCTYPE은 유용하다.)

     

    댓글