[org.mybatis.spring.mapper.ClassPathMapperScanner]-No MyBatis mapper was found in '[com.xxx.xxx....

项目启动后,控制台输出中有以下内容:

[org.mybatis.spring.mapper.ClassPathMapperScanner]-No MyBatis mapper was found in '[com.xxx.xxx.dao]' package. Please check your configuration.


这是因为 spring-mybatis.xml 配置文件中多了以下:

	<!-- DAO接口所在包名,Spring会自动查找其下的类
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xxx.xxx.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	 -->
把它注释掉或删除就可以了。

2025-07-07 16:02:14.361 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;astDisposeBillBossDao&#39; and &#39;com.pty.ast.dao.AstDisposeBillBossDao&#39; mapperInterface. Bean already defined with the same name! 2025-07-07 16:02:14.361 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - No MyBatis mapper was found in &#39;[com.pty.ast.dao]&#39; package. Please check your configuration. 2025-07-07 16:02:14.387 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - No MyBatis mapper was found in &#39;[com.pty.authsync.dao]&#39; package. Please check your configuration. 2025-07-07 16:02:14.518 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - No MyBatis mapper was found in &#39;[com.pty.doc.dao]&#39; package. Please check your configuration. 2025-07-07 16:02:14.676 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;wfTaskDao&#39; and &#39;com.pty.workflow.dao.WfTaskDao&#39; mapperInterface. Bean already defined with the same name! 2025-07-07 16:02:14.676 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;actTaskStatusDao&#39; and &#39;com.pty.workflow.dao.ActTaskStatusDao&#39; mapperInterface. Bean already defined with the same name! 2025-07-07 16:02:14.676 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;actRuExecutionDao&#39; and &#39;com.pty.workflow.dao.ActRuExecutionDao&#39; mapperInterface. Bean already defined with the same name! 2025-07-07 16:02:14.676 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;actHiVarInstDao&#39; and &#39;com.pty.workflow.dao.ActHiVarInstDao&#39; mapperInterface. Bean already defined with the same name! 2025-07-07 16:02:14.676 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner:44 - Skipping MapperFactoryBean with name &#39;actRuIdentityLinkDao&#39; and &#39;com.pty.workflow.dao.ActRuIdentityLinkDao&#39; mapperIn
最新发布
07-08
<think>我们正在解决MyBatisSpring中扫描Mapper时出现的警告和重复Bean定义问题。具体警告信息类似于:“Skipping MapperFactoryBean with name &#39;xxxMapper&#39; because it is already defined”。这通常是由于重复扫描导致的,可能的原因包括: 1.Spring配置中多次扫描了同一个包。 2. 使用了多个扫描器(如同时使用了`@MapperScan`和`MapperScannerConfigurer`)。 3. 不同的模块或配置类扫描了相同的包。 根据引用[1]中的堆栈信息,它可能涉及Bean定义加载的问题。虽然引用[1]的具体内容与当前问题不直接相关,但我们可以推断在加载Bean定义时出现了重复。 解决方案: 1. **检查扫描配置**:确保没有重复的扫描配置。例如,如果你在XML配置中使用了`MapperScannerConfigurer`,同时在Java配置中使用了`@MapperScan`,并且它们扫描的包有重叠,就会导致重复。 2. **统一扫描方式**:只使用一种扫描方式(要么使用`@MapperScan`,要么使用`MapperScannerConfigurer`)。 3. **指定不同的包**:如果确实需要多个扫描器,确保它们扫描的包没有重叠。 例如,假设你的配置如下: ```java @Configuration @MapperScan("com.example.mapper") public class MyBatisConfig { // ... } ``` 同时,在XML配置中又有: ```xml <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> <!-- other properties --> </bean> ``` 这样就会导致重复扫描。解决方法是移除其中一个。 4. **检查子包扫描**:如果扫描的包存在父子关系,也可能导致重复扫描。例如,扫描了`com.example`和`com.example.mapper`,而`com.example`包含了`com.example.mapper`。 5. **使用`excludeFilters`**:如果使用组件扫描(`@ComponentScan`)并且扫描了Mapper接口所在的包,由于Mapper接口通常被注解为`@Repository`(或Spring的其他注解),它们会被Spring扫描并注册为Bean,而MyBatis的扫描器也会注册它们。这会导致重复。解决方法是在组件扫描中排除Mapper接口: ```java @ComponentScan(basePackages = "com.example", excludeFilters = @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Mapper.class})) ``` 注意:MyBatis的`@Mapper`注解不是Spring的注解,所以上面的方法可能不直接适用。另一种方法是确保组件扫描不扫描Mapper接口所在的包,或者将Mapper接口放在单独的包中,然后只由MyBatis的扫描器扫描。 6. **检查模块依赖**:如果是多模块项目,确保每个模块只扫描自己的Mapper接口包,避免其他模块也扫描了相同的包。 7. **升级MyBatis-Spring版本**:有时,旧版本的MyBatis-Spring可能存在一些已知问题,升级到最新版本可能解决。 8. **检查Spring的配置**:引用[2]中提到了Nacos配置,虽然与当前问题无关,但提醒我们检查配置文件的正确性。确保没有在多个配置文件中重复配置扫描。 9. **使用`@Primary`或`@Qualifier`解决冲突**:如果确实需要多个相同类型的Mapper Bean(通常不需要),可以使用`@Primary`指定一个主要的,或者使用`@Qualifier`按名称注入。但这不是根本解决办法,根本办法是避免重复定义。 10. **检查是否手动定义了Mapper Bean**:如果在配置中手动定义了Mapper Bean(例如通过`@Bean`方法),同时扫描器又自动注册了,那么就会重复。应该移除手动定义。 根据上述分析,请检查你的项目配置,特别是扫描相关的配置,确保没有重复扫描同一个包。 相关问题: 1. MyBatisMapperScannerConfigurer和@MapperScan有什么区别? 2. 如何避免Spring中Bean定义的重复? 3. MyBatisSpring Boot中如何配置多数据源?</think>### MyBatis Mapper扫描重复Bean定义问题解决方案 当出现`MapperScannerConfigurer Skipping MapperFactoryBean Bean already defined`警告时,表明MyBatis在扫描Mapper接口时检测到重复的Bean定义。以下是常见原因和解决方案: #### 核心原因分析 1. **重复扫描配置** - 同时使用了`@MapperScan`注解和XML配置的`MapperScannerConfigurer` - 多个`@MapperScan`注解扫描了相同的包路径 - 配置了多个`MapperScannerConfigurer`实例 2. **包路径重叠** - 扫描路径存在包含关系(如同时扫描`com.a`和`com.a.b`) - 不同模块扫描了相同的Mapper接口包 3. **Spring Boot自动配置冲突** - 手动配置与`mybatis-spring-boot-starter`的自动配置冲突 #### 解决方案 ##### 方案1:统一扫描方式(推荐) ```java // 仅保留一种扫描配置(二选一) @Configuration // 方式1:使用注解扫描(删除XML中的MapperScannerConfigurer) @MapperScan("com.example.mapper") public class MyBatisConfig { // ... } ``` ```xml <!-- 方式2:使用XML配置(删除所有@MapperScan) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> ``` ##### 方案2:精确控制扫描范围 ```java // 确保不同模块扫描不同包 @MapperScan("com.module1.mapper") public class Module1Config {} @MapperScan("com.module2.mapper") public class Module2Config {} ``` ##### 方案3:排除自动配置 ```java // Spring Boot项目中排除自动配置 @SpringBootApplication(exclude = { MybatisAutoConfiguration.class }) public class Application { ... } ``` ##### 方案4:检查依赖传递 在`pom.xml`中检查是否有重复的MyBatis依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> <!-- 排除可能冲突的依赖 --> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency> ``` #### 验证步骤 1. 启动时添加调试参数: ```bash -Dlogging.level.org.springframework=DEBUG ``` 2. 在日志中搜索: ```log MapperFactoryBean - Skipping MapperFactoryBean ``` 3. 检查输出的Bean定义列表确认重复项 #### 最佳实践 1. **单一扫描原则**:整个项目只使用一种扫描方式(注解或XML) 2. **包路径隔离**:按模块划分Mapper接口包,避免交叉扫描 3. **版本兼容**:保持MyBatis-SpringSpring版本匹配 - Spring 5.x → MyBatis-Spring 2.x - Spring 6.x → MyBatis-Spring 3.x > 通过精确控制扫描范围可避免重复定义问题,同时确保MyBatis版本与Spring框架兼容[^1][^3]。 --- ### 相关问题 1. 如何为MyBatis配置多数据源并避免Bean冲突? 2. Spring Boot中如何自定义MyBatis的SqlSessionFactory? 3. MyBatis Mapper接口注入失败的可能原因有哪些? 4. 如何解决MyBatis与JPA共存时的Bean定义冲突?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值