Error creating bean with name


最近在學一個東西,要使用SSM新建一個案例,是這樣滴,我有如下

  1. DeptDAO
  2. DeptService
  3. DeptServiceImpl
  4. DeptController
  5. Dept
  6. 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 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]

很奇怪的錯誤,我看錯誤說沒有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。到時再提一提這個因為我的無知而造成的報錯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM