mybatis配置mapper-locations的坑及解决

 更新时间:2022年06月07日 14:25:50   作者:辽大-海浪  
这篇文章主要介绍了mybatis配置mapper-locations的坑及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

mybatis配置mapper-locations的坑

很多时候想把xml文件与mapper类放在一起。

mapper-locations: com.demo.dao.*.xml

这么配置之后 发现报错找不到xml中id

pom.xml配置这句话就可以了!!

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

mapperLocations配置失效

xml文件的目录结构:分oracle数据库的mapper.xml文件夹和瀚高数据库的mapper.xml文件夹

mybatis配置: 加载瀚高的xml文件

启动项目报错信息:

09:00:26.392 [main] ERROR o.s.b.SpringApplication - [reportFailure,837] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MPUserInfoDao' defined in file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\com\jiahe\datasource\dao\signatureapp\MPUserInfoDao.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 [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.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:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:878)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.jiahe.datasource.JiaheYlqDatasourceApplication.main(JiaheYlqDatasourceApplication.java:20)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.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:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1509)
    ... 18 common frames omitted
Caused by: 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:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:652)
    ... 31 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.buildSqlSessionFactory(MybatisSqlSessionFactoryBean.java:594)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.afterPropertiesSet(MybatisSqlSessionFactoryBean.java:433)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.getObject(MybatisSqlSessionFactoryBean.java:627)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration.sqlSessionFactory(MybatisPlusAutoConfiguration.java:197)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$71f733e0.CGLIB$sqlSessionFactory$2(<generated>)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$71f733e0$$FastClassBySpringCGLIB$$7bf95d7a.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration$$EnhancerBySpringCGLIB$$71f733e0.sqlSessionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 32 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\idea-project\jiahe\yunlianqian\Jiahe-SignatureCloud\jiahe-modules\jiahe-ylq-datasource\target\classes\mapper\oracle\admin\CaDlrqzDao.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
    at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.buildSqlSessionFactory(MybatisSqlSessionFactoryBean.java:592)
    ... 45 common frames omitted
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.jiahe.datasource.dao.signaturepc.CaDlrqzDao.caQyqzResult
    at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:992)
    at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:948)
    at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:704)
    at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:209)
    at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:47)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:289)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
    ... 47 common frames omitted

我配置加载的是highgo文件夹下的xml文件,但他却还是加载oracle文件夹下的xml文件。

问题根源

项目的配置文件里面mybatis和mybatis-plus都配置了,而mybatis-plus的配置信息优先级高于mybatis的配置,所以导致mybatis的配置无效。 

解决

使用mybatis-plus的配置,移除mybatis的配置。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java深入探索线程安全和线程通信的特性

    Java深入探索线程安全和线程通信的特性

    这篇文章主要介绍了Java线程安全和线程通信的特性,线程安全是多线程编程时的计算机程序代码中的一个概念。在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况
    2022-05-05
  • LoggingEventAsyncDisruptorAppender类执行流程源码解读

    LoggingEventAsyncDisruptorAppender类执行流程源码解读

    这篇文章主要介绍了LoggingEventAsyncDisruptorAppender类执行流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java实现简单的弹球游戏

    Java实现简单的弹球游戏

    这篇文章主要为大家详细介绍了Java实现简单的弹球游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • springboot+mybatis plus实现树形结构查询

    springboot+mybatis plus实现树形结构查询

    实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,本文主要介绍了springboot+mybatis plus实现树形结构查询,感兴趣的可以了解一下
    2021-07-07
  • Java 读取excel 文件流代码实例

    Java 读取excel 文件流代码实例

    这篇文章主要介绍了Java 读取excel 文件流代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • 一篇文章轻松搞懂Java中的自旋锁

    一篇文章轻松搞懂Java中的自旋锁

    随着互联网的蓬勃发展,越来越多的互联网企业面临着用户量膨胀而带来的并发安全问题。这篇文章主要给大家介绍了关于Java中自旋锁的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • java8学习教程之lambda表达式的使用方法

    java8学习教程之lambda表达式的使用方法

    Java8最值得学习的特性就是Lambda表达式,下面这篇文章主要给大家介绍了关于java8学习教程之lambda表达式使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-09-09
  • java File类的基本使用方法总结

    java File类的基本使用方法总结

    这篇文章主要介绍了java File类的基本使用方法总结,为大家分享了java实现上传代码,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • mybatis插入数据不返回主键id的可能原因及解决方式

    mybatis插入数据不返回主键id的可能原因及解决方式

    这篇文章主要介绍了mybatis插入数据不返回主键id的可能原因及解决方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Java使用Ajax异步上传文件

    Java使用Ajax异步上传文件

    使用Ajax上传文件的应用场景颇多,比如上传用户头像、博客文章中插入图片、对认证用户相关身份进行校验等等很多很多。本文提供一个简单的示例供大家参考
    2021-05-05

最新评论