进行springboot多模块工程开发,其中一个模块,测试mapper使用情况,springbootest测试类中用@Autowared注解注入该mapper,但测试程序始终报错说没有定义该mapper bean,也在相应的配置类中添加了@MapperScan扫描mapper,配置类如下图:

单元测试类如下图:

从网上查到有时候需要在启动类上加mapperscan注解,但试过了,始终不行。报错信息如下:

求相救,可以留下联系方式,我加您,协助我解决,非常感谢!
进行springboot多模块工程开发,其中一个模块,测试mapper使用情况,springbootest测试类中用@Autowared注解注入该mapper,但测试程序始终报错说没有定义该mapper bean,也在相应的配置类中添加了@MapperScan扫描mapper,配置类如下图:

单元测试类如下图:

从网上查到有时候需要在启动类上加mapperscan注解,但试过了,始终不行。报错信息如下:

求相救,可以留下联系方式,我加您,协助我解决,非常感谢!
关注让【宝码香车】来帮你解答,本回答参考DeepSeek编写,并整理提供。
如果还有疑问可以点击头像关注私信或评论。如果答案让您满意,请采纳、点赞、关注,非常感谢!
根据您提供的信息,您在使用Spring Boot多模块工程时遇到了Mapper无法注入的问题。以下是详细的解决方案:
@SpringBootTest(classes = CourseBaseMapperTest.class),这会导致Spring只加载测试类本身,而不会加载完整的应用上下文您的测试类应该指定主配置类或启动类,而不是测试类本身:
@SpringBootTest(classes = YourMainApplication.class)
public class CourseBaseMapperTest {
@Autowired
private CourseBaseMapper courseBaseMapper;
@Test
void testCourseBaseMapper() {
System.out.println(courseBaseMapper);
}
}
您的MybatisPlus配置类看起来是正确的,但需要确保:
@MapperScan注解指定了正确的Mapper接口包路径@Configuration
@MapperScan("com.xuecheng.content.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
如果是多模块项目,请确保:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
确保您的启动类位于根包下,或者至少比Mapper接口所在的包更高层级:
@SpringBootApplication
@MapperScan("com.xuecheng.content.mapper")
public class YourMainApplication {
public static void main(String[] args) {
SpringApplication.run(YourMainApplication.class, args);
}
}
@Mapper注解:如果使用@MapperScan可以不加,但确保至少有一种方式如果以上方法都不能解决您的问题,可以提供更多信息:
您也可以尝试在测试类上添加@ContextConfiguration注解来显式指定配置类:
@SpringBootTest
@ContextConfiguration(classes = {MybatisPlusConfig.class, YourDataSourceConfig.class})
public class CourseBaseMapperTest {
// ...
}