mybatis多数据源报错

2018-12-06 16:58:35,709 [ main ] - [ INFO  ] [ org.springframework.core.KotlinDetector : 57 ] -
				Kotlin reflection implementation not found at runtime, related features won't be available.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-12-06 16:58:36,423 [ main ] - [ INFO  ] [ cn.wanda.wedcredit.App : 50 ] -
				Starting App on SH4010301061179 with PID 7528 (D:\tools\eclipse-jee-oxygen-2-win32-x86_64\eclipse\workspace\wedcredit\target\classes started by xdb in D:\tools\eclipse-jee-oxygen-2-win32-x86_64\eclipse\workspace\wedcredit)
2018-12-06 16:58:36,425 [ main ] - [ INFO  ] [ cn.wanda.wedcredit.App : 675 ] -
				No active profile set, falling back to default profiles: default
2018-12-06 16:58:36,688 [ background-preinit ] - [ WARN  ] [ o.s.h.converter.json.Jackson2ObjectMapperBuilder : 122 ] -
				For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2018-12-06 16:58:39,260 [ main ] - [ INFO  ] [ o.s.boot.web.embedded.tomcat.TomcatWebServer : 90 ] -
				Tomcat initialized with port(s): 9901 (http)
2018-12-06 16:58:39,285 [ main ] - [ INFO  ] [ org.apache.coyote.http11.Http11NioProtocol : 173 ] -
				Initializing ProtocolHandler ["http-nio-9901"]
2018-12-06 16:58:39,314 [ main ] - [ INFO  ] [ org.apache.catalina.core.StandardService : 173 ] -
				Starting service [Tomcat]
2018-12-06 16:58:39,315 [ main ] - [ INFO  ] [ org.apache.catalina.core.StandardEngine : 173 ] -
				Starting Servlet Engine: Apache Tomcat/9.0.12
2018-12-06 16:58:39,334 [ main ] - [ INFO  ] [ org.apache.catalina.core.AprLifecycleListener : 173 ] -
				The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_162\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_191/bin/server;C:/Program Files/Java/jre1.8.0_191/bin;C:/Program Files/Java/jre1.8.0_191/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\Java\jdk1.8.0_162\bin;C:\ProgramData\Oracle\Java\javapath;d:\Oracle\ora90\bin;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Git\cmd;D:\tools\apache-maven-3.5.3-bin\apache-maven-3.5.3\bin;D:\tools\gradle-4.7-bin\gradle-4.7\bin;D:\tools\android-sdk_r24.4.1-windows\android-sdk-windows\tools;D:\tools\android-sdk_r24.4.1-windows\android-sdk-windows\platform-tools;C:\Program Files\nodejs\;C:\Users\xdb\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\xdb\AppData\Local\Programs\Python\Python37\;C:\Users\xdb\AppData\Local\Programs\Fiddler;D:\tools\eclipse-jee-oxygen-2-win32-x86_64\eclipse;;.]
2018-12-06 16:58:39,674 [ main ] - [ INFO  ] [ o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] : 173 ] -
				Initializing Spring embedded WebApplicationContext
2018-12-06 16:58:39,684 [ main ] - [ INFO  ] [ org.springframework.web.context.ContextLoader : 285 ] -
				Root WebApplicationContext: initialization completed in 3154 ms
2018-12-06 16:58:39,800 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.ServletRegistrationBean : 186 ] -
				Servlet statViewServlet mapped to [/druid/*]
2018-12-06 16:58:39,803 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.ServletRegistrationBean : 186 ] -
				Servlet dispatcherServlet mapped to [/]
2018-12-06 16:58:39,809 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.FilterRegistrationBean : 248 ] -
				Mapping filter: 'characterEncodingFilter' to: [/*]
2018-12-06 16:58:39,810 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.FilterRegistrationBean : 248 ] -
				Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-12-06 16:58:39,811 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.FilterRegistrationBean : 248 ] -
				Mapping filter: 'formContentFilter' to: [/*]
2018-12-06 16:58:39,866 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.FilterRegistrationBean : 248 ] -
				Mapping filter: 'requestContextFilter' to: [/*]
2018-12-06 16:58:39,867 [ main ] - [ INFO  ] [ o.s.boot.web.servlet.FilterRegistrationBean : 261 ] -
				Mapping filter: 'webStatFilter' to urls: [/*]
2018-12-06 16:58:40,744 [ main ] - [ INFO  ] [ o.s.scheduling.concurrent.ThreadPoolTaskExecutor : 171 ] -
				Initializing ExecutorService 'applicationTaskExecutor'
2018-12-06 16:58:40,967 [ main ] - [ WARN  ] [ o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext : 554 ] -
				Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: expected single matching bean but found 2: creditSqlSessionFactory,wiseSqlSessionFactory
2018-12-06 16:58:40,969 [ main ] - [ INFO  ] [ o.s.scheduling.concurrent.ThreadPoolTaskExecutor : 208 ] -
				Shutting down ExecutorService 'applicationTaskExecutor'
2018-12-06 16:58:40,977 [ main ] - [ INFO  ] [ org.apache.catalina.core.StandardService : 173 ] -
				Stopping service [Tomcat]
2018-12-06 16:58:41,102 [ main ] - [ INFO  ] [ o.s.b.a.l.ConditionEvaluationReportLoggingListener : 142 ] -
				

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-06 16:58:41,107 [ main ] - [ ERROR ] [ o.s.b.diagnostics.LoggingFailureAnalysisReporter : 42 ] -
				

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:
	- creditSqlSessionFactory: defined by method 'sqlSessionFactory' in class path resource [cn/config/CreditDataSourcesConfig.class]
	- wiseSqlSessionFactory: defined by method 'sessionFactory' in class path resource [cn/config/WiseDataSourcesConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
错误信息
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.x</groupId>
    <artifactId>xx</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>3.1.6</version>
            <type>jar</type>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/hessian-3.1.6.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4.0-atlassian-hosted</version>
            <type>jar</type>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc6-11.2.0.4.0-atlassian-hosted.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okio</groupId>
            <artifactId>okio</artifactId>
            <version>2.1.0</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml
server:
  port: 9901
aa:
  url: http://qq.com/a    
spring:
  datasource1:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://192.168.1.1:3306/aaa
    username: root
    password: root
  datasource2:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:oracle:thin:@//192.168.1.2:1530/bbb
    username: root
    password: root
    
application.yml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="60 seconds"
    debug="false">
    <property name="LOG_HOME" value="./logs" />
    <property name="appName" value="aaa" />
    <appender name="stdout"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [ %thread ] - [ %-5level ] [ %logger{50} : %line ] -
                %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="HTML" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_HOME}/${appName}.html</file>
        <append>true</append>
        <encoder
            class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%d%thread%-5level%logger{50}%line%msg%n</pattern>
            </layout>
            <charset class="java.nio.charset.Charset">GBK</charset>
        </encoder>
        <immediateFlush>true</immediateFlush>
    </appender>

    <appender name="appLogAppender"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${appName}.log</file>
        <rollingPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log
            </fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>30</maxHistory>
            <totalSizeCap>300MB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d [ %thread ] - [ %-5level ] [ %logger{50} : %line ] -
                %msg%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <appender name="appLogCurrent"
        class="ch.qos.logback.core.FileAppender">
        <file>${LOG_HOME}/${appName}_current.log</file>
        <append>false</append>
        <!-- set immediateFlush to false for much higher logging throughput -->
        <immediateFlush>true</immediateFlush>
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder 
            by default -->
        <encoder>
            <pattern>%d [ %thread ] - [ %-5level ] [ %logger{35} : %line ] -
                %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="stdout" />
        <appender-ref ref="appLogAppender" />
        <appender-ref ref="HTML" />
        <appender-ref ref="appLogCurrent" />
    </root>
</configuration>
logback.xml
package cn.wanda.wedcredit.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "admin");
        reg.addInitParameter("loginPassword", "admin");
        reg.addInitParameter("logSlowSql", "true");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
}
DruidConfig.java
package cn.wanda.wedcredit.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "cn.x.dao.credit", sqlSessionFactoryRef = "creditSqlSessionFactory")
public class CreditDataSourcesConfig {
    @ConfigurationProperties("spring.datasource1")
    @Bean(name = "creditDataSource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "creditTransactionManager")
    public DataSourceTransactionManager customTransactionManager() {
        return new DataSourceTransactionManager(druidDataSource());
    }

    @Bean(name = "creditSqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("creditDataSource") DruidDataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        // sessionFactoryBean.setConfigLocation(new
        // ClassPathResource("mybatis-config.xml"));
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean.getObject();
    }
    
    @Bean(name = "creditSqlSessionTemplate")
    public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("creditSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
DatasouceConfig1.java
package cn.wanda.wedcredit.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "cn.x.dao.wise", sqlSessionFactoryRef = "wiseSqlSessionFactory")
public class WiseDataSourcesConfig {
    @ConfigurationProperties("spring.datasource2")
    @Bean(name = "wiseDataSource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "wiseTransactionManager")
    public DataSourceTransactionManager customTransactionManager() {
        return new DataSourceTransactionManager(druidDataSource());
    }

    @Bean(name = "wiseSqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("wiseDataSource") DruidDataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        // sessionFactoryBean.setConfigLocation(new
        // ClassPathResource("mybatis-config.xml"));
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean.getObject();
    }
    
    @Bean(name = "wiseSqlSessionTemplate")
    public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("wiseSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
    
}
DataSourceConfig2.java

 

posted @ 2018-12-06 17:57  xiaodebing  阅读(838)  评论(0编辑  收藏  举报