1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.seven.sync</groupId>
<artifactId>db-sync</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--统一编译环境-->
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.9.RELEASE</version>
</dependency>
<!--spring-boot-starter-jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.3.9.RELEASE</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!--mybatis-plus-boot-starter-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、yml配置
server:
port: 8008
spring:
datasource:
yms: #数据源1,亚马逊
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库
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
username: root # 用户名
password: mima # 密码
hikari:
minimum-idle: 10
maximum-pool-size: 1000
auto-commit: true
idle-timeout: 560000
pool-name: DatebookHikariCP-ds1
max-lifetime: 600000
connection-timeout: 60000
connection-test-query: SELECT 1
ali: #数据源2,阿里云
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver # mysql的驱动你可以配置别的关系型数据库
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
username: root # 用户名
password: mima@ # 密码
hikari:
minimum-idle: 10
maximum-pool-size: 1000
auto-commit: true
idle-timeout: 560000
pool-name: DatebookHikariCP-ds2
max-lifetime: 600000
connection-timeout: 60000
connection-test-query: SELECT 1
mybatis-plus:
configuration:
map-underscore-to-camel-case: true #开启驼峰映射
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志
3、config配置
ali数据库源
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author deng
* @Version V1.0.0
* @ClassName AliConfiguration
* @date 2022/2/16 9:49
* @Description AliConfiguration
*/
@Configuration
@MapperScan(basePackages = "com.seven.sync.mapper.ali", sqlSessionTemplateRef = "aliSessionTemplate")
public class AliConfiguration {
@Bean
@ConfigurationProperties("spring.datasource.ali")
public HikariDataSource aliDataSource(){
return new HikariDataSource();
}
@Bean
public SqlSessionFactory aliSessionFactory(@Qualifier("aliDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean
public DataSourceTransactionManager aliTransactionManager(@Qualifier("aliDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate aliSessionTemplate(@Qualifier("aliSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
yms数据库源
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author deng
* @Version V1.0.0
* @ClassName YmsConfiguration
* @date 2022/2/16 9:47
* @Description yms数据库配置
*/
@Configuration
@MapperScan(basePackages = "com.seven.sync.mapper.yms", sqlSessionTemplateRef = "ymsSessionTemplate")
public class YmsConfiguration {
@Bean
@ConfigurationProperties("spring.datasource.yms")
public HikariDataSource ymsDataSource(){
return new HikariDataSource();
}
@Bean
public SqlSessionFactory ymsSessionFactory(@Qualifier("ymsDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean
public DataSourceTransactionManager ymsTransactionManager(@Qualifier("ymsDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate ymsSessionTemplate(@Qualifier("ymsSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}