Java/오류노트

[Java/오류노트] Solved - org.apache.ibatis.binding.BindingException : Type interface xxx is not known to the MapperRegistry.

adjh54 2024. 1. 9. 18:05
728x170
해당 문제에서는 Mybatis에서 발생하는 BindingException에 대해 해결 방법을 알아봅니다.




1) 문제점


💡 문제점

- 최근 프로젝트에서 패키지를 재구성하였습니다.
- 이에 따라 IDE 툴 내에서 자동 Refactor가 되었는 줄 알았으나 아래와 같은 문제가 발생하였습니다.

org.apache.ibatis.binding.BindingException : Type interface xxx is not known to the MapperRegistry.

- 해당 문제는 말 그대로 **Mapper로 구성한 xxx 경로에 있는 파일이 MapperRegistry에 등록되지 않은 문제입니다.
- 다시 말해 Mapper Resource 세팅의 경로가 문제가 생겨서 이를 수정 해줘야 합니다.
org.apache.ibatis.binding.BindingException : Type interface xxx is not known to the MapperRegistry.

 

 

💡 아래와 같이 src/ 경로 아래에 controller, mapper, model, service 형태로 구성이 되어 있었습니다.

 

 

💡 아래와 같이 src/ 하위에 v1, v2 형태로 디렉터리가 구성되어 경로를 못 찾는 문제였습니다.

 

 

 

 

 

 

2) 해결방법


 

1. **Mapper.xml 파일 namespace 경로 확인


💡 **Mapper.xml 파일 namespace 경로 확인

- 아래와 같이 xml Mapper 파일과 interface Mapper 파일의 경로가 매핑이 잘 되었는지 확인합니다.

 

 

2. DBConfig.java 파일을 확인합니다.


💡 DBConfig.java 파일을 확인합니다

- setMapperLocations : 매핑되는 xml 파일을 확인했는데 기존의 패키지 구조에 mapper/v1/*.xml 형태로 변경을 하였는데 하드코딩 된 문자열이 변경되지 않았습니다.
- setTypeAliasesPackage: xxx.model 해당 부분 역시 xxx.model 형태에서 xxx.v1.model 형태로 변경하였는데 하드코딩 된 문자열이 변경되지 않았습니다.
@Configuration
@PropertySource("classpath:/application.properties")
public class DBConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariConfig hikariConfig() {
        return new HikariConfig();
    }

    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(hikariConfig());
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean session = new SqlSessionFactoryBean();
        session.setDataSource(dataSource);
        session.setMapperLocations(applicationContext.getResources("classpath:mapper/*.xml"));
        session.setTypeAliasesPackage("xxx.model");
        session.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:config/common-mybatis-config.xml"));
        return session.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

 

 

 

3. 문제가 된 부분 수정


💡 문제가 된 부분 수정

- 위에 파일을 확인하였을때 문제가 되었던 setMapperLocations(), setConfigLocation() 부분을 수정하여 해결하였습니다.
@Configuration
@PropertySource("classpath:/application.properties")
public class DBConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariConfig hikariConfig() {
        return new HikariConfig();
    }

    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(hikariConfig());
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean session = new SqlSessionFactoryBean();
        session.setDataSource(dataSource);
        session.setMapperLocations(applicationContext.getResources("classpath:mapper/v1/*.xml"));
        session.setTypeAliasesPackage("xxx.v1.model");
        session.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:config/common-mybatis-config.xml"));
        return session.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

 

 

 

💡 [참고] MyBatis 관련 설정에 대해 궁금하시면 아래의 글을 참고하시면 도움이 됩니다.
 

[JAVA] Spring Boot내에서 MyBatis & PostgreSQL 연동하기

해당 글에서는 Spring Boot내에 Mybatis와 PostgreSQL을 연결하고 로직 처리를 위한 환경 설정 및 파일 구성에 대해서 공유합니다. 💡 최하단에 해당 환경 구성과 관련하여 '로컬 DB 구성 방법', 'HikariCP'

adjh54.tistory.com

 

 

 

 

 

오늘도 감사합니다. 😀

 

 

 

그리드형