vlambda博客
学习文章列表

Java同时连接三个不同的数据库小结

    最近项目上涉及到做三个数库之间的数据迁移工作,中途连接三个数据库的配置很有意思,特此记录一下,有用到的同学可以参考一下。

    pom文件引包:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${baomidou.dynamic.datasource.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <!-- ArangoDB --> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-spring-boot-starter</artifactId> <version>${arango.starter.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

yml配置文件连接三个数据库:

#arangodbspring: data: arangodb:      hosts: ip:端口号 database: cw_dokb user: root load-balancing-strategy: round_robin protocol: http_json datasource: type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 test-on-borrow: true test-on-return: false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20  #postgresql dynamic: primary: postgresql strict: false datasource: postgresql: url: jdbc:postgresql://ip:端口号/cw_dokb?createDatabaseIfNotExist=true&useUnicode=true&autoReconnect=true&characterEncoding=utf8&connectionCollation=utf8_bin&useSSL=false username: 账号          password: 密码 driver-class-name: org.postgresql.Driver  #mysql mysql: url: jdbc:mysql://ip:端口号/cw_dokb?useUnicode=true&characterEncoding=utf-8          username: 账号 password: 密码 driver-class-name: com.mysql.cj.jdbc.Driver
# mybatismybatis: configuration:    map-underscore-to-camel-casetrue

项目结构:

单元测试: