应急sql更新代码

master
dongdingding 1 year ago
parent d2a6bf2a43
commit 822cc48f42

@ -66,6 +66,11 @@
<version>2.3.1</version> <version>2.3.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.6</version>
</dependency>
</dependencies> </dependencies>

@ -2,14 +2,19 @@ package com.example.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import java.net.InetAddress;
import java.net.UnknownHostException;
@EnableScheduling @EnableScheduling
@SpringBootApplication @SpringBootApplication
public class Demo4Application { public class Demo4Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Demo4Application.class, args); SpringApplication.run(Demo4Application.class, args);
} }
} }

@ -9,7 +9,7 @@ import javax.sql.DataSource;
@Configuration @Configuration
public class DataSourceConfig { public class DataSourceConfig {
//云坤
@Bean(name = "dataSource1") @Bean(name = "dataSource1")
public DataSource dataSource1() { public DataSource dataSource1() {
return DataSourceBuilder.create() return DataSourceBuilder.create()
@ -18,7 +18,7 @@ public class DataSourceConfig {
.password("lt_admin@O$c&,") .password("lt_admin@O$c&,")
.build(); .build();
} }
//公司
@Bean(name = "dataSource2") @Bean(name = "dataSource2")
public DataSource dataSource2() { public DataSource dataSource2() {
return DataSourceBuilder.create() return DataSourceBuilder.create()
@ -27,4 +27,7 @@ public class DataSourceConfig {
.password("Jichuang@2023") .password("Jichuang@2023")
.build(); .build();
} }
} }

@ -1,9 +1,14 @@
package com.example.demo.task; package com.example.demo.task;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -17,35 +22,30 @@ import java.util.Map;
public class DataUpdateTask { public class DataUpdateTask {
@Autowired @Autowired
private JdbcTemplate masterJdbcTemplate; private JdbcTemplate masterJdbcTemplate;
@Autowired @Autowired
private JdbcTemplate slaveJdbcTemplate; private JdbcTemplate slaveJdbcTemplate;
// 每天早上7点执行一次任务 // 每天早上5点执行一次任务 更新检查记录表
@Scheduled(cron = "0 0 7 * * ?") @Scheduled(cron = "0 0 5 * * ?")
public void updateData() { public void updateData() {
System.out.println("hello");
// 获取远程数据库所有表名 // 获取远程数据库所有表名
List<String> tableNames = masterJdbcTemplate.queryForList( List<String> tableNames = masterJdbcTemplate.queryForList(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'tc_zz'", "SELECT table_name FROM information_schema.tables WHERE table_schema = 'ying_ji'",
String.class); String.class);
// 更新每个表的数据到另一个数据库 // 更新每个表的数据到另一个数据库
for (String tableName : tableNames) { for (String tableName : tableNames) {
if (!tableName.equals("szs_enterprise_information") && tableName.startsWith("sz") ){ if (tableName.equals("sz_enfor_examine") ){
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查 slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
slaveJdbcTemplate.execute("delete FROM tc_zz." + tableName); slaveJdbcTemplate.execute("delete FROM ying_ji." + tableName);
// ... 以此类推,根据实际表的数量进行删除操作 // ... 以此类推,根据实际表的数量进行删除操作
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查 slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
String selectQuery = "SELECT * FROM tc_zz." + tableName; String selectQuery = "SELECT * FROM ying_ji." + tableName;
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery); List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
for (Map<String, Object> row : tableData) { for (Map<String, Object> row : tableData) {
StringBuilder insertQuery = new StringBuilder("INSERT INTO tc_zz.") StringBuilder insertQuery = new StringBuilder("INSERT INTO ying_ji.")
.append(tableName) .append(tableName)
.append(" ("); .append(" (");
StringBuilder valuesClause = new StringBuilder(" VALUES ("); StringBuilder valuesClause = new StringBuilder(" VALUES (");
@ -69,17 +69,12 @@ public class DataUpdateTask {
} }
// 每周一早上5点执行一次任务 更新企业原表
@Scheduled(cron = "0 0 5 ? * MON")
// 每周日晚上5点执行一次任务
@Scheduled(cron = "0 0 17 ? * SUN")
public void updateInformationData() { public void updateInformationData() {
System.out.println("hello");
// 获取远程数据库所有表名 // 获取远程数据库所有表名
List<String> tableNames = masterJdbcTemplate.queryForList( List<String> tableNames = masterJdbcTemplate.queryForList(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'tc_zz'", "SELECT table_name FROM information_schema.tables WHERE table_schema = 'ying_ji'",
String.class); String.class);
// 更新每个表的数据到另一个数据库 // 更新每个表的数据到另一个数据库
@ -87,14 +82,14 @@ public class DataUpdateTask {
if (tableName.equals("szs_enterprise_information") ) { if (tableName.equals("szs_enterprise_information") ) {
// 删除本地数据库所有表的数据 // 删除本地数据库所有表的数据
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查 slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
slaveJdbcTemplate.execute("delete FROM tc_zz." + tableName); slaveJdbcTemplate.execute("delete FROM ying_ji." + tableName);
// ... 以此类推,根据实际表的数量进行删除操作 // ... 以此类推,根据实际表的数量进行删除操作
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查 slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
String selectQuery = "SELECT * FROM tc_zz." + tableName; String selectQuery = "SELECT * FROM ying_ji." + tableName;
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery); List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
for (Map<String, Object> row : tableData) { for (Map<String, Object> row : tableData) {
StringBuilder insertQuery = new StringBuilder("INSERT INTO tc_zz.") StringBuilder insertQuery = new StringBuilder("INSERT INTO ying_ji.")
.append(tableName) .append(tableName)
.append(" ("); .append(" (");
StringBuilder valuesClause = new StringBuilder(" VALUES ("); StringBuilder valuesClause = new StringBuilder(" VALUES (");
@ -105,17 +100,14 @@ public class DataUpdateTask {
valuesClause.append("?, "); valuesClause.append("?, ");
values.add(entry.getValue()); values.add(entry.getValue());
} }
// 删除最后一个逗号和空格 // 删除最后一个逗号和空格
insertQuery.delete(insertQuery.length() - 2, insertQuery.length()); insertQuery.delete(insertQuery.length() - 2, insertQuery.length());
valuesClause.delete(valuesClause.length() - 2, valuesClause.length()); valuesClause.delete(valuesClause.length() - 2, valuesClause.length());
insertQuery.append(")").append(valuesClause).append(")"); insertQuery.append(")").append(valuesClause).append(")");
slaveJdbcTemplate.update(insertQuery.toString(), values.toArray()); slaveJdbcTemplate.update(insertQuery.toString(), values.toArray());
} }
} }
} }
} }
} }

Loading…
Cancel
Save