hikari数据源配置类_SpringBoot多数据源配置详解

本文介绍了在SpringBoot环境下,如何配置Hikari数据源实现多数据源,包括pom.xml的依赖设置,application.yml的详细配置,以及相关数据源属性配置类、Mapper配置和测试步骤。通过配置master和slave两个数据源,展示了SpringBoot应用中多数据源的完整配置过程。
摘要由CSDN通过智能技术生成

开发环境: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值