mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 09:35:29 +08:00
数据迁移
This commit is contained in:
parent
c448f182f4
commit
c9f291a898
@ -1,13 +1,18 @@
|
||||
package com.luban.api;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.luban.api.entity.H5Project;
|
||||
import com.luban.api.service.H5ProjectService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootTest
|
||||
public class ImportH5Project {
|
||||
@ -36,4 +41,82 @@ public class ImportH5Project {
|
||||
|
||||
System.out.println("批量导入结果: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void migrateTaobaoZhihuGaodingData() {
|
||||
// 配置源数据源
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
dataSource.setUrl("jdbc:mysql://localhost:3307/dian_shang?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true");
|
||||
dataSource.setUsername("root");
|
||||
dataSource.setPassword("123456"); // 根据实际情况修改密码
|
||||
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
|
||||
// 查询源数据
|
||||
String sql1 = """
|
||||
SELECT id, 链接, page, 作者姓名, 点赞数, 答案, html, create_time
|
||||
FROM taobao_zhihu_gaoding
|
||||
where 链接 = 'https://www.gaoding.com/api/v3/cp/recommend-contents/general'
|
||||
order by page
|
||||
""";
|
||||
List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql1);
|
||||
|
||||
System.out.println("从源数据表查询到 " + dataList.size() + " 条记录");
|
||||
|
||||
// 转换并保存数据到目标表
|
||||
List<H5Project> projects = new ArrayList<>();
|
||||
for (Map<String, Object> data : dataList) {
|
||||
String htmlContent = (String) data.get("html");
|
||||
if("[]".equals(htmlContent)){
|
||||
continue;
|
||||
}
|
||||
List<JSONObject> list1 = JSONUtil.toList(htmlContent, JSONObject.class);
|
||||
|
||||
for (JSONObject obj1 : list1) {
|
||||
obj1.get("id");
|
||||
obj1.get("payment_tag");
|
||||
obj1.get("content_updated_at");//1747668808
|
||||
obj1.get("type");//h5_bargraph
|
||||
obj1.get("title");//H5长页简约可爱点击打开盲盒拆盲盒产品促销
|
||||
obj1.get("group_ids");
|
||||
|
||||
// 查询源数据
|
||||
String sql12 = """
|
||||
SELECT id, 链接, page, 作者姓名, 点赞数, 答案, html, create_time
|
||||
FROM taobao_zhihu_gaoding
|
||||
where 链接 = 'https://www.gaoding.com/api/v3/cp/contents/{id}/distribution-infos?with_content=true'
|
||||
order by id
|
||||
""".replace("{id}", obj1.getStr("id"));
|
||||
Map<String, Object> db2 = jdbcTemplate.queryForMap(sql12);
|
||||
String html2 = (String) db2.get("html");
|
||||
JSONObject obj2 = JSONUtil.parseObj(html2);
|
||||
|
||||
|
||||
//正式开始保存
|
||||
H5Project project = new H5Project();
|
||||
project.setId(obj2.getLong("id"));
|
||||
project.setTitle(obj2.getStr("title"));
|
||||
project.setType(obj1.getStr("type")); // 设置类型为知乎相关
|
||||
|
||||
|
||||
//TODO 写到这里了,明天继续
|
||||
project.setDescription("");
|
||||
|
||||
project.setContent("");
|
||||
project.setPreviewUrl(""); // 可以根据需要设置预览图URL
|
||||
project.setPreviewUrlWatermark(""); // 可以根据需要设置水印预览图URL
|
||||
project.setPreviewWidth(375); // 默认预览图宽度
|
||||
project.setPreviewHeight(667); // 默认预览图高度
|
||||
project.setStatus(1); // 设置为启用状态
|
||||
projects.add(project);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("开始保存 " + projects.size() + " 条记录到目标表");
|
||||
|
||||
// 批量保存项目
|
||||
// boolean result = h5ProjectService.saveBatchH5ProjectsWithContent(projects);
|
||||
// System.out.println("数据迁移结果: " + result + ",共迁移 " + projects.size() + " 条数据");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user