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