一、問題
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 { }
注意檢查涉及到的包路徑是否寫正確。