解决SpringBoot整合Mybatis扫描不到Mapper的问题
闲来无事,想学学springboot,开始搭建一个项目,但是一直显示mapper扫描不到的错误:
“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.minibike.bike.user.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}”
“Field userMapper in com.minibike.bike.user.controller.UserController required a bean of type ‘com.minibike.bike.user.dao.UserMapper' that could not be found.”
于是在网上找解决方案,都是治标不治本,不得不说现在的博客质量下降的厉害,太误导人了,打开好多个都是一样的内容,难道没人遇到的错误一样,连包名和类名也一样吗?有的人抄别人博客,类名和包名都懒得换一下?
最后解决方案是在Application.java主类上加注解@MapperScan(“com.minibike.bike.user.dao”)。括号中引号里边是mapper所在包路径。
加上注解后不再报错,也能正常输出oooo
第一次接触springboot,昨天下午到今早一直没找到问题所在,后来查文档、注解,然后不断尝试,最后终于解决。
当然这个只是我个人项目开发中遇到的问题解决办法,不一定通用,但是如过遇到这样的问题,也可以一试。
补充知识:springboot 项目配置扫描mapper接口类的两种方法
一种是在启动类上加@MapperScan({"com.**.dao","com.**.dao.*"}) 扫描mapper接口文件所在的包,多个包的时候用英文半角逗号隔开;
一种是在mapper接口文件上加@Mapper注解。如下
import org.apache.ibatis.annotations.Mapper;
以上这篇解决SpringBoot整合Mybatis扫描不到Mapper的问题就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- SpringBoot整合Mybatis扫描不到Mapper的问题
- springboot整合Mybatis扫描不到mapper问题
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- SpringBoot集成Mybatis时无法扫描Mapper问题
- Spring Boot 整合mybatis mapper扫描(坑)
- Springboot+mybatis整合中找不到*mapper.xml的映射问题
- 使用idea学习SpringBoot整合Mybatis,遇到mapper无法注入的问题
- springboot多模块化整合mybatis,出现mapper自动注入失败问题
- springboot扫描不到mapper中的xml文件(解决办法)
- springboot整合mybatis mapper注入时显示could not autowire的解决
- IDEA中解决spring整合mybatis后使用MapperScannerConfigurer扫描mapper包时“ Invalid bound statement (not found)”错误
- springmvc 整合mybatis dao层不写实现类,及MapperScannerConfigurer 引起的PropertyPlaceholderConfigurer无效问题解决方法
- 创建Spring-Mybatis web项目,解决IDEA下找不到XXXmapper.xml文件的问题
- spring 整合mybatis时候 MapperScannerConfigure的配置问题
- springboot+mybatis+jta+atomikos解决多数据源事务问题
- spring与RabbitMQ整合 消费者消费不到消息 重启才能消费到的问题解决
- 解决SpringBoot 整合 FreeMaker,不能js,css静态资源的问题
- Spring boot项目整合mybatis,xml静态资源文件的放置及路径配置问题
- 解决spring+Mybatis的整合测试问题