因为近期业务需求,mysql数据库不在一台服务器上,需要从不同的数据库获取数据,写了一个测试demo
项目路径
yml文件配置,自定义了bicap和train两个数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
bicap:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialize: true #指定初始化数据源,是否用data.sql来初始化,默认: true
name: bicap
url: jdbc:mysql://mysql.web.zz:3306/bicap?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
train:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialize: true
name: train
url: jdbc:mysql://10.253.50.250:3306/train?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
server:
port: 8088
tomcat:
max-threads: 1000
max-connections: 2000
accept-count: 1000
uri-encoding: utf-8
logging:
level.com.slh: info
file: data-source.log
主数据源配置
配置类需要对DataSource、DataSourceTransactionManager、SqlSessionFactory 、SqlSessionTemplate四个数据项进行配置;
package com.slh.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
impo