MyBatis mapperLocations、basePackage多数据路径配置

时间:2021-04-21 05:10:10

转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/55259314,建议读者阅读原文,确保获得完整的信息

单数据路径配置

    <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="mapperLocations" value="classpath*:com/loongshawn/dao/impl/mapper2/pmp/*.xml" />
<property name="typeAliasesPackage" value="com.autonavi.domain" />
</bean>

<!-- mybatis.spring自动映射 -->
<bean id="mybatisMapperScanner2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.loongshawn.pmp" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
</bean>

多数据路径配置

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<array>
<value>classpath:com/loongshawn/dao/impl/mapper/*.xml</value>
<value>classpath:com/loongshawn/dao/impl/mapper3/pmc/*.xml</value>
</array>
</property>
<property name="typeAliasesPackage" value="com.autonavi.domain" />
</bean>

<!-- mybatis.spring自动映射 -->
<bean id="mybatisMapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.loongshawn.dao.impl.mapper,com.loongshawn.dao.impl.mapper3" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

mapperLocations属性多路径配置如下:

<property name="mapperLocations">

<array>
<value>classpath:com/autonavi/dao/impl/mapper/*.xml</value>
<value>classpath:com/autonavi/dao/impl/mapper3/pmc/*.xml</value>
</array>

</property>

basePackage属性多路径配置仅需通过英文逗号”,”分隔开不同路径即可。

以上实现方式均验证无误。

常相关异常

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'censoringAlarmTask': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.autonavi.dao.impl.mapper3.pmc.CensoringMapper com.autonavi.method.pmc.AlarmTask.censoringMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.autonavi.dao.impl.mapper3.pmc.CensoringMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at com.autonavi.method.Initialization.initialize(Initialization.java:30)
at com.autonavi.PmsApp.main(PmsApp.java:41)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.autonavi.dao.impl.mapper3.pmc.CensoringMapper com.autonavi.method.pmc.AlarmTask.censoringMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.autonavi.dao.impl.mapper3.pmc.CensoringMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.autonavi.dao.impl.mapper3.pmc.CensoringMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
... 24 more

MyBatis mapperLocations、basePackage多数据路径配置

MyBatis mapperLocations、basePackage多数据路径配置

MyBatis mapperLocations、basePackage多数据路径配置