最近在学一个东西,要使用SSM新建一个案例,是这样滴,我有如下
- DeptDAO
- DeptService
- DeptServiceImpl
- DeptController
- Dept
- Mybatis
首先,我不是太清楚有了DAO还要有Service,不过这个不是本文章的重点。
重点是,报如下错,两个不同的错误吧
错误1:
2019-04-02 22:09:08.697 ERROR 12080 --- [nio-8001-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.vae.springcloud.dao.DeptDAO.findAll] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.vae.springcloud.dao.DeptDAO.findAll
错误2:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptController': Unsatisfied dependency expressed through field 'deptService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl': Unsatisfied dependency expressed through field 'deptDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptDAO' defined in file [D:Java CodeSpringCloudprovider argetclassescomvaespringclouddaoDeptDAO.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 org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:Java CodeSpringCloudprovider argetclassesmybatismapperDeptMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:Java CodeSpringCloudprovider argetclassesmybatismapperDeptMapper.xml]'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.vae.springcloud.dao.DeptDAO.findAll. please check mybatis/mapper/DeptMapper.xml and file [D:Java CodeSpringCloudprovider argetclassesmybatismapperDeptMapper.xml]
很奇怪的错误,我看错误说没有bean,我在网上搜啊搜,网上的文章要么是抄袭的,要么是复制粘贴的,要么解释的答案简单的很,根本没有上升到我这个层次。我纠结了好几天,最终发现,错误居然是!!!居然是Mybatis的配置文件!!
我自己写的mybatis文章里面,说道,mybatis加载mapper.xml需要这样
<!-- 加载映射文件-->
<mappers>
<mapper resource="config/Message.xml"/>
</mappers>
这样是可行,但是,这就是造成我错误的原因。这种方式,我当时使用的是SqlSession来主动加载Mybatis文件的,现在我想自动的去映射,就不能这么写了,需要在yml配置文件里面写
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件
总之,这次的错误,完全是因为我不熟悉Mybatis造成的,为此困惑了好多天。
顺便一提,我正在学习的这个项目是SpringCloud,学会之后,我会专门去详细的讲SpringCloud。到时再提一提这个因为我的无知而造成的报错。