SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“

编程知识 更新时间:2023-05-02 05:24:16

异常

启动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

本文发布于:2023-04-26 05:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/15a6d54abdf50ff8c0b0b0b1cd51dc92.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   bean   defining   SpringBoot   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104369文章数
  • 26212阅读数
  • 0评论数