Java数据同步
项目基于Spring Boot,数据库同步涉及到多个数据源,配置多个数据源。
在application.properties中配置两个数据源,remote.wz和local.wz
spring.datasource.remote.wz.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.remote.wz.jdbc-url=jdbc:sqlserver://localhost:1433;databaseName=szf
spring.datasource.remote.wz.username=sa
spring.datasource.remote.wz.password=sa
spring.datasource.local.wz.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.local.wz.jdbc-url=jdbc:sqlserver://localhost:1433;databaseName=WZ
spring.datasource.local.wz.username=sa
spring.datasource.local.wz.password=sa
在java类中配置这两个数据源
remote.wz
@Configuration
@MapperScan(basePackages = {"com.demo.dataimport.dao.remotewz"}, sqlSessionFactoryRef = "remoteWzSqlSessionFactory", sqlSessionTemplateRef = "remoteWzSqlSessionTemplate")
public class RemoteWzConfig {
/**
* 远程工商
*/
@Bean("remoteWzDataSource")
@ConfigurationProperties(prefix = "spring.datasource.remote.wz")
public DataSource remoteGsDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("remoteWzSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory1(@Qualifier("remoteWzDataSource") DataSource ds1) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1);