废话不多说,今天在用springboot做一个东西的时候用到了mybatis
炸了炸了,我们以前用spring-boot加载mybatis都没什么问题,可今天就出现了问题,下面是我报错的问题:
下面是两段程序的报错,我中途修改代码以为成功了,没想到一运行就又报错=
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController':
Unsatisfied dependency expressed through field 'service'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceImpl':
Unsatisfied dependency expressed through field 'usermapper'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usermapper'
defined in file [D:\TestMaven\springboot4\target\classes\com\xiaoran\springboot4\demo\mapper\Usermapper.class]:
Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined
in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation
via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception;
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/mybatis-config.xml]
2021-11-15 21:39:21.249 ERROR 13816 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] :
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing
failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
com.xiaoran.springboot4.demo.mapper.Usermapper.getUser] with root cause
大家先看看网上的怎么说的把我简单给说一下:
(1)就是你们的mybatis-mapper.xml中的 amespace和你们标签的id对不上,
(2)还有一种就是你们的service注解,忘记标在你们的接口实现类上面
(3)接下来这个是我自己总结的,全网独一份:我们在用spring-boot引入mybatis的时候,按照以前在spring中学习的时候我们都要求去写:两个xml一个是Mybatis-config.xml和mybatis-mapper.xml,我也是这样写的哈哈哈哈,但是我们后面在application.yml中写的时候我也把两个都注入进去了。
application.yml中写的是
mybatis:
mapper-locations: mybatis/mapper/Usermapper.xml
config-location: mybatis/mybatis-config.xml
如果你也是这样写的话,那么恭喜你写错了,我们的mybatis-config.xml在这里本身就是空文件夹,如果你配置进去的化,springboot会按照你写入的mybatis-config.xml去找mapper,那么肯定是找不到的,所以这里我们就只能写一个mybatis-mapper.xml进去。
mybatis:
mapper-locations: mybatis/mapper/Usermapper.xml