SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段
Posted 二木成林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段相关的知识,希望对你有一定的参考价值。
异常
启动SpringBoot项目报错:
2021-06-25 15:32:39.540 WARN 23108 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2021-06-25 15:32:39.541 INFO 23108 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing ...
2021-06-25 15:32:39.548 INFO 23108 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
2021-06-25 15:32:39.552 INFO 23108 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-06-25 15:32:39.569 INFO 23108 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-06-25 15:32:39.606 ERROR 23108 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userMapper in com.example.demo.controller.UserController required a bean of type 'com.example.demo.mapper.UserMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.demo.mapper.UserMapper' in your configuration.
错误代码
UserMapper.java
@Mapper
public interface UserMapper {
@Select("select * from user where username=#{username}")
User selectByUsername(String username);
}
这里没有service层,是直接在controller层调用的Mapper类中的方法,所以UserController.java
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/mybatis")
public String table(String username,Model model){
System.out.println(username);
User user = userMapper.selectByUsername(username);
System.out.println(user);
model.addAttribute("users",new ArrayList<>().add(user));
return "table";
}
}
DemoApplication.java
@SpringBootApplication
@MapperScan("com.exapmle.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
原因
上面的代码为什么要把主启动类DemoApplication和配置文件application.properties的代码拿出来说呢?
因为这是SpringBoot整合Mybatis的代码,所以在Mapper接口类使用的是@Mapper注解。
但如果每个Mapper接口类都添加@Mapper注解比较麻烦,所以用了@MapperScan注解批量扫描Mapper接口类。
可以说冲突了,但事实上并不是,真正的原因是我@MapperScan中的值的路径写错了:
解决
只选择一种注解方式,要么使用@Mapper,要么使用@MapperScan注解,不能都使用。
经过测试,两种方式都能够一起使用,但是使用的时候@MapperScan的属性值(即Mapper接口类所在的路径)一定要正确,否则一定会报错,所以建议只使用一种方式。
只使用@MapperScan
只使用@Mapper
@MapperScan和@Mapper注解一起使用
以上是关于SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段的主要内容,如果未能解决你的问题,请参考以下文章
翻车现场:解决SpringBoot项目启动访问后台JSP页面显示404报错