解决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.ller.UserController required a bean of type
‘com.minibike.bike.user.dao.UserMapper' that could not be found.”
springframework和springboot于是在⽹上解决⽅案,都是治标不治本,不得不说现在的博客质量下降的厉害,太误导⼈了,打开好多个都是⼀样的内容,难道没⼈遇到的错误⼀样,连包名和类名也⼀样吗?有的⼈抄别⼈博客,类名和包名都懒得换⼀下?
最后解决⽅案是在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的问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。