最近在學一個東西,要使用SSM新建一個案例,是這樣滴,我有以下java
首先,我不是太清楚有了DAO還要有Service,不過這個不是本文章的重點。spring
重點是,報以下錯,兩個不一樣的錯誤吧sql
錯誤1:express
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 causeapache
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.vae.springcloud.dao.DeptDAO.findAllsession
錯誤2:mybatis
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 Code\SpringCloud\provider\target\classes\com\vae\springcloud\dao\DeptDAO.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 Code\SpringCloud\provider\target\classes\mybatis\mapper\DeptMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Java Code\SpringCloud\provider\target\classes\mybatis\mapper\DeptMapper.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 Code\SpringCloud\provider\target\classes\mybatis\mapper\DeptMapper.xml]app
很奇怪的錯誤,我看錯誤說沒有bean,我在網上搜啊搜,網上的文章要麼是抄襲的,要麼是複製粘貼的,要麼解釋的答案簡單的很,根本沒有上升到我這個層次。我糾結了好幾天,最終發現,錯誤竟然是!!!竟然是Mybatis的配置文件!!ide
我本身寫的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。到時再提一提這個由於個人無知而形成的報錯。