1.運行項目,控制台出現以下錯誤:
*************************** APPLICATION FAILED TO START 應用程序啟動失敗 *************************** Description: 描述 Field productMapper in org.spring.springboot.service.impl.ProductServiceImpl required a bean of type 'org.spring.springboot.mapper.ProductMapper' that could not be found. 在org.spring.springboot.service.impl中的字段productMapper。ProductServiceImpl需要一個“org.spring.springboot.mapper”類型的bean。沒有找到ProductMapper。 Action: 建議 Consider defining a bean of type 'org.spring.springboot.mapper.ProductMapper' in your configuration. 考慮定義一個“org.spring.springboot.mapper”類型的bean。在您的配置中的ProductMapper。
2.解決辦法:當我看到這個錯誤時,我首先認為我在ProductMapper文件中沒有寫注解,讓ProductServiceImpl調用ProductMapper是找不到bean,但我發現並沒有少寫注解。后來發現原來是主函數上@MapperScan("org.spring.springboot.mapper")沒寫明地址。當添加完畢再次運行,就完全沒有問題了。
@SpringBootApplication @MapperScan("org.spring.springboot.mapper") public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
3.總結:@MapperScan("mapper路徑") 可以掃描接口包 @MapperScan({"mapper路徑","mapper路徑"}) 掃描多個接口包