广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot启动扫描不到dao层接口的解决方案
  • 237
分享到

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

2024-04-02 19:04:59 237人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

今天启动SpringBoot项目时失败了 解决 检查原因发现是启动类的MapperScan("")的值写到类名了,改成类所在的包名错误就修复了。 springboot 扫描不到da

今天启动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);
    }
}

注意:不要使用错误注解

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

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

本文链接: https://www.lsjlt.com/news/130081.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作