spring 整合mybatis后 若mapper.java与mapper.xml不在同一文件夹,该怎么配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 整合mybatis后 若mapper.java与mapper.xml不在同一文件夹,该怎么配置相关的知识,希望对你有一定的参考价值。

放同一文件夹就可以用,分开怎么都用不了,spring 配置文件:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lt.mapper" />
</bean>
java文件在这个包com.lt.mapper
xml文件在这个包com.lt.mapper.xml

原因如下:

1、在mybatis的配置中,有些地方是可以省略的,如果你的mapper.xml文件和mapper.java文件,如果放到同一个文件夹中,就可以省略很多配置。

2、在项目上右键,选择porperty,然后看build path,然后看source。 

3、在找我选中的这两项可以看出来,虽说在编辑器中,路径位置不一样,但是编译后运行生成的项目路径还是一样的,所以说呢,如果感觉,java 文件和xml文件怎么可能混到一起放呢,太不顺眼了吧,可以拆开来放。 

4、通过这个级别关系可以看出,resources和java是同一级别,而且,编译后的文件(见上图),都是同一个路径,所以,就可以把他拆分出来,所以,这样,即美观,又实用,配置又少。

参考技术A MapperScannerConfigurer
中,根据basePackage的value找,而mapper中,根据定义的class找
参考技术B  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" depends-on="flyway">
        <property name="dataSource" ref="dataSource" />
        <!-- 我是假定你的文件放在src/main/resource/com/lt-->
        <property name="mapperLocations" value="classpath:/com/lt/*.xml" />
    </bean>
<!-- mybatis:DAO映射 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lt.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

参考技术C 在mybatis-confi.xml配置文件中指定<mappers>或者使用spring去扫描,具体教程:http://www.luoshengsha.com/284.html,附带源码 参考技术D 这个都要在web.xml里面共同配置的

解决spring mybatis 整合后mapper接口注入失败

spring整合mybatis,在dao层我们只写一个接口,配置相应的*mapper.xml文件,

报如下错误:

 1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloController‘: Unsatisfied dependency expressed through field ‘helloService‘; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloService‘: Unsatisfied dependency expressed through field ‘helloMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 2     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
 3     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
 4     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
 5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
 6     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
 7     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
 8     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
 9     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
10     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
11     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
12     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
13     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
14     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
15     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
16     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
17     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
18     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
19     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
20     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
21     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
22     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
23     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
24     at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702)
25     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
26     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
27     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
28     at java.lang.reflect.Method.invoke(Method.java:498)
29     at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
30     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
31     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
32     at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
33     at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
34     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
35     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
36     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
37     at java.lang.reflect.Method.invoke(Method.java:498)
38     at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
39     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
40     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
41     at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
42     at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
43     at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
44     at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
45     at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
46     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
47     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
48     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
49     at java.lang.reflect.Method.invoke(Method.java:498)
50     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
51     at sun.rmi.transport.Transport$1.run(Transport.java:200)
52     at sun.rmi.transport.Transport$1.run(Transport.java:197)
53     at java.security.AccessController.doPrivileged(Native Method)
54     at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
55     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
56     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
57     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
58     at java.security.AccessController.doPrivileged(Native Method)
59     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
60     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
61     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
62     at java.lang.Thread.run(Thread.java:745)
63 Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloService‘: Unsatisfied dependency expressed through field ‘helloMapper‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
64     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
65     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
66     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
67     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
68     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
69     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
70     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
71     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
72     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
73     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
74     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
75     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
76     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
77     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
78     ... 60 more
79 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aopeng.quest.dao.IHelloMapper‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
80     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
81     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
82     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
83     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
84     ... 73 more

 

起始这是由于一个低级错误引起的,在applicationContext.xml中有如下的配置:

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2          <property name="basePackage" value="com.xxx.xxx.dao.**.*" />
3          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
4 </bean>

 

basePackage的value应该是指定包名,而我指定的文件名。

改成如下配置,编译正常通过:

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2          <property name="basePackage" value="com.xxx.xxx.dao" />
3          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
4 </bean>

 

以上是关于spring 整合mybatis后 若mapper.java与mapper.xml不在同一文件夹,该怎么配置的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis+Spring整合后Mapper测试类编写

Spring和mybatis整合 org.mybatis.spring.mapper.MapperScannerConfigurer

spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)

解决spring整合mybatis时错误 ‘org.mybatis.spring.mapper.MapperScannerConfigurer#0‘

解决spring整合mybatis时错误 ‘org.mybatis.spring.mapper.MapperScannerConfigurer#0‘

解决spring整合mybatis时错误 ‘org.mybatis.spring.mapper.MapperScannerConfigurer#0‘