1、pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.seven.sync</groupId>
  7. <artifactId>db-sync</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <!--统一编译环境-->
  11. <java.version>1.8</java.version>
  12. <maven.compiler.source>1.8</maven.compiler.source>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16. <dependencies>
  17. <!--spring-boot-starter-web-->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. <version>2.3.9.RELEASE</version>
  22. </dependency>
  23. <!--spring-boot-starter-jdbc-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-jdbc</artifactId>
  27. <version>2.3.9.RELEASE</version>
  28. </dependency>
  29. <!--mysql-connector-java-->
  30. <dependency>
  31. <groupId>mysql</groupId>
  32. <artifactId>mysql-connector-java</artifactId>
  33. <version>8.0.26</version>
  34. </dependency>
  35. <!--mybatis-plus-boot-starter-->
  36. <dependency>
  37. <groupId>com.baomidou</groupId>
  38. <artifactId>mybatis-plus-boot-starter</artifactId>
  39. <version>3.4.1</version>
  40. </dependency>
  41. <!--lombok-->
  42. <dependency>
  43. <groupId>org.projectlombok</groupId>
  44. <artifactId>lombok</artifactId>
  45. <version>1.18.12</version>
  46. <scope>compile</scope>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <finalName>${project.artifactId}</finalName>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. <configuration>
  56. <includeSystemScope>true</includeSystemScope>
  57. </configuration>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

2、yml配置

  1. server:
  2. port: 8008
  3. spring:
  4. datasource:
  5. yms: #数据源1,亚马逊
  6. type: com.zaxxer.hikari.HikariDataSource
  7. driver-class-name: com.mysql.cj.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库
  8. jdbc-url: jdbc:mysql://cn4lujzijsav.ap-east-1.rds.amazonaws.com:33068/pool_big?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useAffectedRows=true&allowMultiQueries=true
  9. username: root # 用户名
  10. password: mima # 密码
  11. hikari:
  12. minimum-idle: 10
  13. maximum-pool-size: 1000
  14. auto-commit: true
  15. idle-timeout: 560000
  16. pool-name: DatebookHikariCP-ds1
  17. max-lifetime: 600000
  18. connection-timeout: 60000
  19. connection-test-query: SELECT 1
  20. ali: #数据源2,阿里云
  21. type: com.zaxxer.hikari.HikariDataSource
  22. driver-class-name: com.mysql.cj.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库
  23. jdbc-url: jdbc:mysql://mysql.zhangbei.rds.aliyuncs.com:3306/pool_big_backup?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useAffectedRows=true&allowMultiQueries=true
  24. username: root # 用户名
  25. password: mima@ # 密码
  26. hikari:
  27. minimum-idle: 10
  28. maximum-pool-size: 1000
  29. auto-commit: true
  30. idle-timeout: 560000
  31. pool-name: DatebookHikariCP-ds2
  32. max-lifetime: 600000
  33. connection-timeout: 60000
  34. connection-test-query: SELECT 1
  35. mybatis-plus:
  36. configuration:
  37. map-underscore-to-camel-case: true #开启驼峰映射
  38. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志

3、config配置

ali数据库源

  1. import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
  2. import com.zaxxer.hikari.HikariDataSource;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  11. import javax.sql.DataSource;
  12. /**
  13. * @author deng
  14. * @Version V1.0.0
  15. * @ClassName AliConfiguration
  16. * @date 2022/2/16 9:49
  17. * @Description AliConfiguration
  18. */
  19. @Configuration
  20. @MapperScan(basePackages = "com.seven.sync.mapper.ali", sqlSessionTemplateRef = "aliSessionTemplate")
  21. public class AliConfiguration {
  22. @Bean
  23. @ConfigurationProperties("spring.datasource.ali")
  24. public HikariDataSource aliDataSource(){
  25. return new HikariDataSource();
  26. }
  27. @Bean
  28. public SqlSessionFactory aliSessionFactory(@Qualifier("aliDataSource") DataSource dataSource) throws Exception {
  29. MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
  30. bean.setDataSource(dataSource);
  31. return bean.getObject();
  32. }
  33. @Bean
  34. public DataSourceTransactionManager aliTransactionManager(@Qualifier("aliDataSource") DataSource dataSource){
  35. return new DataSourceTransactionManager(dataSource);
  36. }
  37. @Bean
  38. public SqlSessionTemplate aliSessionTemplate(@Qualifier("aliSessionFactory") SqlSessionFactory sqlSessionFactory){
  39. return new SqlSessionTemplate(sqlSessionFactory);
  40. }
  41. }

yms数据库源

  1. import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
  2. import com.zaxxer.hikari.HikariDataSource;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  11. import javax.sql.DataSource;
  12. /**
  13. * @author deng
  14. * @Version V1.0.0
  15. * @ClassName YmsConfiguration
  16. * @date 2022/2/16 9:47
  17. * @Description yms数据库配置
  18. */
  19. @Configuration
  20. @MapperScan(basePackages = "com.seven.sync.mapper.yms", sqlSessionTemplateRef = "ymsSessionTemplate")
  21. public class YmsConfiguration {
  22. @Bean
  23. @ConfigurationProperties("spring.datasource.yms")
  24. public HikariDataSource ymsDataSource(){
  25. return new HikariDataSource();
  26. }
  27. @Bean
  28. public SqlSessionFactory ymsSessionFactory(@Qualifier("ymsDataSource") DataSource dataSource) throws Exception {
  29. MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
  30. bean.setDataSource(dataSource);
  31. return bean.getObject();
  32. }
  33. @Bean
  34. public DataSourceTransactionManager ymsTransactionManager(@Qualifier("ymsDataSource") DataSource dataSource){
  35. return new DataSourceTransactionManager(dataSource);
  36. }
  37. @Bean
  38. public SqlSessionTemplate ymsSessionTemplate(@Qualifier("ymsSessionFactory") SqlSessionFactory sqlSessionFactory){
  39. return new SqlSessionTemplate(sqlSessionFactory);
  40. }
  41. }