springboot 시작 으로 dao 층 인 터 페 이 스 를 스 캔 하지 못 하 는 솔 루 션

오늘 spring boot 프로젝트 를 시작 하 는 데 실 패 했 습 니 다.

해결 하 다.
검사 원인 은 시작 클래스 의 MapperScan(")의 값 이 클래스 이름 에 적 혀 있 는 것 으로 밝 혀 졌 습 니 다.클래스 가 있 는 패키지 이름 오류 로 바 뀌 면 복 구 됩 니 다.
springboot 에서 dao 층 과 controller 를 검색 할 수 없습니다.
힌트
A component required a bean of type'com.imooc 2.product.category.dao.ProductCategory Dao'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);
    }
}
2.문제:
제시 controller 와 services 층 을 찾 을 수 없습니다
  • controller 에 접근 하 는 방법 은 404 오류 가 발생 했 습 니 다
  • 해결:
    1.시작 클래스 는 디 렉 터 리 아래 에 놓 습 니 다.그림 과 같이 제 contrller 와 service 는 각각 com.imooc 2.produt 의 produt 폴 더 와 category 폴 더 에 있 기 때문에 시작 클래스 는 루트 디 렉 터 리 com.imooc 2.produt 에 놓 아야 합 니 다.
    원인: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);
        }
    }
    메모:잘못된 주 해 를 사용 하지 마 십시오.
    이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

    좋은 웹페이지 즐겨찾기