gpt4 book ai didi

springboot启动扫描不到dao层接口的解决方案

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章springboot启动扫描不到dao层接口的解决方案由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

今天启动springboot项目时失败了 。

springboot启动扫描不到dao层接口的解决方案

解决

检查原因发现是启动类的MapperScan("")的值写到类名了,改成类所在的包名错误就修复了.

springboot 扫描不到dao层和controller

1、提示

A component required a bean of type ‘com.imooc2.product.category.dao.ProductCategoryDao' that could not be found即dao层找不到了 。

解决:使用@MapperScan 注解或者@MapperScans注解 。

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.ComponentScans;
  6. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  7. @EnableSwagger2
  8. @SpringBootApplication
  9. @MapperScan("com.imooc2.product.**.dao")
  10. public class ProductApplication {//extends SpringBootServletInitializer
  11. public static void main(String[] args) {
  12. SpringApplication.run(ProductApplication.class, args);
  13. }
  14. }

2、问题:

  1. 提示controller和services层找不到
  2. 访问controller的方法显示404错误

解决:

1、启动类放到跟目录下面,如图,我的controller和service分别在com.imooc2.product的product文件夹和category文件夹里面,所以启动类要放在根目录com.imooc2.product下 。

原因:sprigboot 会自动扫描根目录以下的全部包 。

springboot启动扫描不到dao层接口的解决方案

2、有可能是缓存还是项目启动类识别不了controller层和service层,或者你不想把启动类放到根目录下去默认扫描,可以更新至springboot 2.0.5.RELEASE,使用自定义扫描包注解 。

  1. @ComponentScan(basePackages = {“com.imooc2.product.product”, com.imooc2.product.category”})

或者 。

  1. @SpringBootApplication(scanBasePackages = {“com.imooc2.product.product”, com.imooc2.product.category”})

二选一即可如图所示 。

  1. @SpringBootApplication(scanBasePackages = {"com.imooc2.product","com.imooc2.product.**.dao"})//二选一
  2. @ComponentScan(basePackages = {"com.imooc2.product.product", "com.imooc2.product.category"})//二选一
  3. @MapperScan("com.imooc2.product.**.dao")
  4. public class ProductApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ProductApplication.class, args);
  7. }
  8. }

注意:不要使用错误注解 。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们.

原文链接:https://blog.csdn.net/weixin_42065545/article/details/80976524 。

最后此篇关于springboot启动扫描不到dao层接口的解决方案的文章就讲到这里了,如果你想了解更多关于springboot启动扫描不到dao层接口的解决方案的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com