开发环境:JDK1.8+SpringBoot2.1.4.RELEASE+Oracle
这里我们假设要使用两个数据源分别为:master和slave。
- pom.xml 依赖包
org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-data-jpacom.github.norauiojdbc712.1.0.2org.apache.commonscommons-lang3org.mybatis.spring.bootmybatis-spring-boot-starter1.1.1
- application.yml配置
server: port: 50000 ---spring: jpa: hibernate: ddlAuto: update openInView: true showSql: false databasePlatform: org.hibernate.dialect.Oracle10gDialect---# 第一个数据源 master: datasource: driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521/orcl username: t0 password: t0 type: com.zaxxer.hikari.HikariDataSource hikari: minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: MasterDatabookHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1 FROM DUAL# 第二个数据源 slave: datasource: driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521/orcl username: t1 password: t1 type: com.zaxxer.hikari.HikariDataSource hikari: minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: SlaveDatabookHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1 FROM DUAL ---# mybatis 配置,分表对应到不同的包中 master: mybatis: config-location: classpath:/MyBatis-conf.xml type-aliases-package: com.pack.domain #master数据源对应的包 mapper-locations: - classpath:/com/pack/mapper/oracle/*.xml #master数据源对应mapper文件slave: mybatis: config-location: classpath:/MyBatis-conf.xml type-aliases-package: com.pack.slave.domain #slave数据源对应的包 mapper-locations: - classpath:/com/pack/slave/mapper/oracle/*.xml #slave数据源对应mapper文件---# jpa相关的配置master: jpa: repos: com.pack.base.repository #master数据源对应的包配置 domain: com.pack.domain #master对应的实体包slave: jpa: repos: com.pack.slave.repository #salve数据源对应的包配置 domain: com.pack.slave.domain #slave对应的实体包
以上就是两个数据源对应相关的配置了,大家注意看里面的注释。接下来我们看mabatis和jpa对应的类相关的配置了,都是固定的配置。
- 数据源属性配置对应的java类配置
BaseProperties类
public class BaseDataSourceProperties implements BeanClassLoaderAware, InitializingBean {private ClassLoader classLoader;/** * Name of the datasource. Default to "testdb" when using an embedded database. */private String name;/** * Whether to generate a random datasource name. */private boolean generateUniqueName;/** * Fully qualified name of the connection pool implementation to use. By default, it * is auto-detected from the classpath. */private Class extends DataSource> type;/** * Fully qualified name of the JDBC driver. Auto-detected based on the URL by default. */private String driverClassName;/** * JDBC URL of the database. */private String url;/** * Login username of the database. */private String username;/** * Login password of the database. */private String password;/** * JNDI location of the datasource. Class, url, username & password are ignored when * set. */private String jndiName;/** * Initialize the datasource with available DDL and DML scripts. */private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;/** * Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or * data-${platform}.sql). */private String platform = "all";/** * Schema (DDL) script resource references. */private List schema;/** * Username of the database to execute DDL scripts (if different). */private String schemaUsername;/** * Password of the database to execute DDL scripts (if different). */private String schemaPassword;/** * Data (DML) script resource references. */private List data;/** * Username of the database to execute DML scripts (if different). */private String dataUsername;/** * Password of the database to execute DML scripts (if different). */private String dataPassword;/** * Whether to stop if an error occurs while initializing the database. */private boolean continueOnError = false;/** * Statement separator in SQL initialization scripts. */private String separator = ";";/** * SQL scripts encoding. */private Charset sqlScriptEncoding;private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;private Xa xa = new Xa();private String uniqueName;@Overridepublic void setBeanClassLoader(ClassLoader classLoader) {this.classLoader = classLoader;}@Overridepublic void afterPropertiesSet() throws Exception {this.embeddedDatabaseConnection = EmbeddedDatabaseConnection.get(this.classLoader);}/** * Initialize a {@link DataSourceBuilder} with the state of this instance. * @return a {@link DataSourceBuilder} initialized with the customizations defined on * this instance */public DataSourceBuilder> initializeDataSourceBuilder() {return DataSourceBuilder.create(getClassLoader()).type(getType()).driverClassName(determineDriverClassName()).url(determineUrl()).username(determineUsername()).pass