本来呢,直接使用mybatis-spring-boot-starter还是挺好的,但是我们系统比较复杂,有多个数据源,其中一个平台自己的数据源,另外一些是动态配置出来的,两者完全没有关系。所以直接使用mybatis-spring-boot-starter就很麻烦了,会报下列错误:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: dataSource,branchta
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

具体是init()中基于类型获取dataSource的原因:
@PostConstruct
public void init() {
if (!this.properties.isInitialize()) {
logger.debug("Initialization disabled (not running DDL scripts)");
return;
}
if (this.applicationContext.getBeanNamesForType(DataSource.class, false,
false).length > 0) {
this.dataSource = this.applicationContext.getBean(DataSource.class);
}
if (this.dataSource == null) {
logger.debug("No DataSource found so not initializing");
return;
}
runSchemaScripts();
}

就只能蜕回去使用mybatis-spring了。启动的时候发现死活注入不进去,报下列错误:

参考了http://www.cnblogs.com/insaneXs/p/9270071.html和https://blog.csdn.net/qq_21853607/article/details/72802080,经验证并非他们所述的问题。我debug的时候,发现mapper对象是有的,而不是第一个所述的没有创建代理,最后debug的时候发现好像是druiddatasouce bean创建的时候出错了,但是没有堆栈信息。完整的配置如下:

application-bean.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:property-placeholder location="classpath*:jrescloud.properties" ignore-unresolvable="true" order="1"/> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<!-- <property name="initialSize" value="1"/>
<property name="minIdle" value="100000"/>
<property name="maxActive" value="10"/> -->
<!-- 配置获取连接等待超时的时间 -->
<!-- <property name="maxWait" value="${jdbc.maxWait}"/>
打开PSCache,并且指定每个连接上PSCache的大小
<property name="poolPreparedStatements" value="${jdbc.pps}"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.mpps}"/>
配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
配置一个连接在池中最小生存的时间,单位是毫秒
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
<property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
<property name="logAbandoned" value="${jdbc.logAbandoned}"/>
配置监控统计拦截的filters
<property name="filters" value="${jdbc.filters}"/> -->
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="${mybatis.configLocation}"/>
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<list>
<value>${mybatis.mapperLocations}</value>
</list>
</property>
</bean>
</beans>
package com.XX.XXX.XXXX.config;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MybatisConfig { @Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.XX.XXX.XXXX.*.mapper");
return mapperScannerConfigurer;
}
}
package com.XX.XXX.XXXX.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
@ImportResource(locations={"classpath:application-bean.xml"})
public class AppConfig { @Bean
public DynamicDataSourceRegister dynamicDataSourceRegister() {
return new DynamicDataSourceRegister();
} @Bean
public DynamicDataSourceConfig getDynamicDataSourceConfig() {
return new DynamicDataSourceConfig();
}
}

最新文章

  1. Java Generics and Collections-2.3
  2. 使用Prerender.io为angular项目做SEO
  3. [转].NET下读取PDF文本
  4. iptables 工具
  5. ubuntu安装vim
  6. 替换运算符 shell
  7. fseek()
  8. memcache研究
  9. beautifulsoup 获取a(tag)的属性href
  10. PAM - 可插拔认证模块
  11. 《程序设计实践》【PDF】下载
  12. 网站搭建中,怎么区分ASP和PHP
  13. JSP常见的三个编译指令
  14. GDAL库调试(包括跨语言调试)
  15. 我手写的简易tomcat
  16. 异常 - 虚拟机初始化错误 - Error occurred during initialization of VM
  17. Linux 安装composer
  18. 格式化代码引发的css编译失败
  19. emcc,wasm,webassembly
  20. Python爬虫(一)——开封市58同城租房信息

热门文章

  1. windows安装 centos
  2. spring mvc配置datasource数据源的三种方式
  3. SQL Server数据库(时间戳timestamp)类型
  4. caffe运行训练脚本时报错:Unknown bottom blob &#39;data&#39; (layer &#39;conv1&#39;,bottom index 0)
  5. Python二分法查找
  6. 20155228 2016-2017-2 《Java程序设计》第3周学习总结
  7. JavaScript 基础,登录验证
  8. Spark学习之路 (十八)SparkSQL简单使用
  9. shell 编程每日100行
  10. 10 分钟速成 Python3