欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-01-01 11:29:42|栏目:JAVA代码|点击:

今天启动springboot项目时失败了

解决

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

springboot 扫描不到dao层和controller

一、提示

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

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

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
@MapperScan("com.imooc2.product.**.dao")
public class ProductApplication  {//extends SpringBootServletInitializer
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}

二、问题:

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

解决:

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

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

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

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

或者

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

二选一即可如图所示

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

注意:不要使用错误注解

上一篇:php上传文件分类实例代码

栏    目:JAVA代码

下一篇:redis setIfAbsent和setnx的区别与使用说明

本文标题:springboot启动扫描不到dao层接口的解决方案

本文地址:http://www.codeinn.net/misctech/188776.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有