springboot 启动报错Consider defining a bean of type 'com.example.springbootdruid.mapper.UserMapper' in your configurati


一、问题

springboot项目启动时报错:

Field userMapper in com.example.springbootdruid.service.impl.UserServiceImpl required a bean of type 'com.example.springbootdruid.mapper.UserMapper' that could not be found.

原因是因为没有扫描到对应的类

二、解决方式:

1、检查配置文件是否写对:

  在springboot的配置文件添加,mybatis的配置如下所示:

mybatis:
type-aliases-package: com.xxx.xxx.domain
mapper-locations: classpath:/mybatis/*.xml

 2、是否加上相应的注解

  在启动类上加上@MapperScan或者@ComponentScan注解,手动指定application类要扫描哪些包下的注解,如下所示: 

@SpringBootApplication
@MappertScan(basePackages = {"com.xxx.xxx.mapper"})

  或者在接口上添加@Mapper注解。

@Mapper
public interface UserMapper {
}

注意检查涉及到的包路径是否写正确。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM