环境: intellij idea 2017.1.4 + spring boot 2.0
代码文档结构图:
注:此代码中的db1,db2为mysql数据源相关,db3为sqlserver数据源
2 数据库配置相关类
先在application.properties 文件中添加数据库的配置
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#spring.datasource.username=root
#spring.datasource.password=
#mybatis.mapper-locations: classpath:mapper/*.xml
## db1 database
spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.db1.username=root
spring.datasource.db1.password=
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
## db2 database
spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8
spring.datasource.db2.username=root
spring.datasource.db2.password=
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
## db3 database
spring.datasource.db3.jdbc-url=jdbc:sqlserver://ip:port;DatabaseName=dbname
spring.datasource.db3.username=dbaccountname
spring.datasource.db3.password=dbaccountpassowrd
spring.datasource.db3.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
## Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=3000
在datasouce目录下增加DataSourceConfig3文件,DataSourceConfig1,DataSourceConfig2略(资源代码里有),如下:
package com.neo.datasource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.