Search code examples
javaxmlmybatisspring-mybatisspring-context

MyBatis MapperScannerConfigurer to scan base package from classpath


I have configured MapperScannerConfigurer in spring application context as follow:

<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <property name="basePackage" value="path.to.package.from.classpath.dao" />
</bean>

Here basePackage is in a jar file that is set in classpath.

One of the mapper from the package is autowired in the one of the services I am using. Looks like MyBatis is failing here to scan package from classpath as I am getting error stating no such Bean found:

No qualifying bean of type 'path.to.package.from.classpath.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate.

I have searched online and found nothing which has similar scenario.


Solution

  • I am going to answer here my own question as it may help someone facing the similar issue.

    The problem was in the way I created jar in the first place. iBatis might have a different way of scanning the packages as it was working and I thought the same could work with MyBatis. But MyBatis may have a different mechanism for scanning packages. It appears it is looking for directory entries in the jar and if it does not find it, it will throw this exception. So make sure you choose the option Add directory entries while creating the jar as shown below:

    enter image description here