You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mybatis-Plus @MapperScan通配符包路径配置失效问题排查

问题分析与解决办法

错误原因

@MapperScan注解的作用是扫描MyBatis的Mapper接口类,而非XML映射文件。你在注解里添加的"com.sage.**.mapper.xml"路径完全无效——这个路径下只有XML文件,没有需要扫描的Mapper接口类,所以该配置项起不到任何作用。

正确配置XML文件路径的方法

MyBatis-Plus需要单独配置XML映射文件的扫描路径,有两种常用实现方式:

方式一:通过配置文件(application.yml / application.properties)

在Spring Boot配置文件中添加MyBatis-Plus的XML路径配置:

mybatis-plus:
  # 匹配所有子目录下mapper/xml中的XML文件
  mapper-locations: classpath*:com/sage/**/mapper/xml/*.xml

如果使用properties格式的配置文件:

mybatis-plus.mapper-locations=classpath*:com/sage/**/mapper/xml/*.xml

方式二:在配置类中手动配置SqlSessionFactory

如果需要更自定义的配置逻辑,可以在MybatisPlusConfig中手动创建MybatisPlusSqlSessionFactoryBean并指定XML路径:

@Configuration
@MapperScan("com.sage.**.mapper")
class MybatisPlusConfig {

    @Bean
    fun sqlSessionFactory(dataSource: DataSource): MybatisSqlSessionFactoryBean {
        val factoryBean = MybatisPlusSqlSessionFactoryBean()
        factoryBean.setDataSource(dataSource)
        // 设置XML扫描路径
        factoryBean.setMapperLocations(
            PathMatchingResourcePatternResolver().getResources("classpath*:com/sage/**/mapper/xml/*.xml")
        )
        return factoryBean
    }
}

补充说明

之前XML文件放在mapper包下能正常工作,是因为MyBatis-Plus的默认扫描规则会匹配classpath*:**/mapper/*.xml,刚好覆盖了你mapper包下的XML文件;而放到mapper/xml子目录后,不在默认路径范围内,因此必须手动配置扫描路径。

内容的提问来源于stack exchange,提问作者SageJustus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:32:11