基于springboot+mybatisplus,一个数据库读取另外一个数据库五分钟同步一次数据表里字段都一样,代码给我详细写出来
时间: 2024-05-09 14:21:24 浏览: 129
以下是基于springboot mybatisplus实现数据库同步的代码:
1. 首先在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3.1</version>
</dependency>
```
2. 在application.properties中配置两个数据源:
```
spring.datasource.source1.url=jdbc:mysql://localhost:3306/source1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
spring.datasource.source1.username=root
spring.datasource.source1.password=root
spring.datasource.source2.url=jdbc:mysql://localhost:3306/source2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
spring.datasource.source2.username=root
spring.datasource.source2.password=root
```
3. 创建两个数据源的配置类:
```
@Configuration
@MapperScan(basePackages = "com.example.mapper.source1", sqlSessionTemplateRef = "source1SqlSessionTemplate")
public class Source1DataSourceConfig {
@Bean(name = "source1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.source1")
public DataSource source1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "source1SqlSessionFactory")
public SqlSessionFactory source1SqlSessionFactory(@Qualifier("source1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "source1SqlSessionTemplate")
public SqlSessionTemplate source1SqlSessionTemplate(@Qualifier("source1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.source2", sqlSessionTemplateRef = "source2SqlSessionTemplate")
public class Source2DataSourceConfig {
@Bean(name = "source2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.source2")
public DataSource source2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "source2SqlSessionFactory")
public SqlSessionFactory source2SqlSessionFactory(@Qualifier("source2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "source2SqlSessionTemplate")
public SqlSessionTemplate source2SqlSessionTemplate(@Qualifier("source2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
4. 创建两个Mapper接口,分别操作两个数据源的表:
```
@Mapper
public interface Source1Mapper {
List<Source1> selectAll();
}
@Mapper
public interface Source2Mapper {
void truncateTable();
void insertBatch(List<Source2> list);
}
```
5. 创建定时任务,定时同步两个数据库的数据:
```
@Component
public class DataSyncTask {
@Autowired
private Source1Mapper source1Mapper;
@Autowired
private Source2Mapper source2Mapper;
@Scheduled(fixedDelay = 300000) // 每五分钟同步一次
public void syncData() {
List<Source1> source1List = source1Mapper.selectAll();
if (source1List != null && source1List.size() > 0) {
source2Mapper.truncateTable(); // 清空目标表
List<Source2> source2List = source1List.stream().map(source1 -> {
Source2 source2 = new Source2();
BeanUtils.copyProperties(source1, source2);
return source2;
}).collect(Collectors.toList());
source2Mapper.insertBatch(source2List); // 批量插入数据
}
}
}
```
以上就是基于springboot mybatisplus实现数据库同步的详细代码。
阅读全文