springboot 启动报错Consider defining a bean of type 'com.example.springbootdruid.mapper.UserMapper' in your configurati

一、问题

springboot项目启动时报错:

Field userMapper in com.example.springbootdruid.service.impl.UserServiceImpl required a bean of type 'com.example.springbootdruid.mapper.UserMapper' that could not be found.

原因是因为没有扫描到对应的类

二、解决方式:

1、检查配置文件是否写对:

  在springboot的配置文件添加,mybatis的配置如下所示:

mybatis:
type-aliases-package: com.xxx.xxx.domain
mapper-locations: classpath:/mybatis/*.xml

 2、是否加上相应的注解

  在启动类上加上@MapperScan或者@ComponentScan注解,手动指定application类要扫描哪些包下的注解,如下所示: 

@SpringBootApplication
@MappertScan(basePackages = {"com.xxx.xxx.mapper"})

  或者在接口上添加@Mapper注解。

@Mapper
public interface UserMapper {
}

注意检查涉及到的包路径是否写正确。

猜你喜欢

转载自www.cnblogs.com/kingsonfu/p/10344388.html