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_dokbuser: rootload-balancing-strategy: round_robinprotocol: http_jsondatasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 5max-active: 20max-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 30000test-on-borrow: truetest-on-return: falsepool-prepared-statements: falsemax-pool-prepared-statement-per-connection-size: 20#postgresqldynamic:primary: postgresqlstrict: falsedatasource:postgresql:url: jdbc:postgresql://ip:端口号/cw_dokb?createDatabaseIfNotExist=true&useUnicode=true&autoReconnect=true&characterEncoding=utf8&connectionCollation=utf8_bin&useSSL=falseusername: 账号password: 密码driver-class-name: org.postgresql.Driver#mysqlmysql:url: jdbc:mysql://ip:端口号/cw_dokb?useUnicode=true&characterEncoding=utf-8username: 账号password: 密码driver-class-name: com.mysql.cj.jdbc.Driver# mybatismybatis:configuration:map-underscore-to-camel-case: true
项目结构:
单元测试:
