commit 56309d44e01181628bd6779cd49081abe6707b97
Author: 55007 <55007@maojian>
Date: Tue Jan 7 18:15:34 2025 +0800
mysql应用
diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..f7e4a1d
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..58da1f7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target/
+/logs/
diff --git a/.project b/.project
new file mode 100644
index 0000000..92491e5
--- /dev/null
+++ b/.project
@@ -0,0 +1,23 @@
+
+
+ mysql_app
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+ org.eclipse.m2e.core.maven2Nature
+
+
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..abdea9a
--- /dev/null
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,4 @@
+eclipse.preferences.version=1
+encoding//src/main/java=UTF-8
+encoding//src/main/resources=UTF-8
+encoding/=UTF-8
diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..71df522
--- /dev/null
+++ b/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,9 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
+org.eclipse.jdt.core.compiler.release=disabled
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c7de53a
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+mysql应用 java版本
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..64d2c41
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,173 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.4.RELEASE
+
+
+ com.bfd
+ data_process
+ 1.0-SNAPSHOT
+
+
+ 1.8
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ mysql
+ mysql-connector-java
+ runtime
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework
+ spring-jdbc
+
+
+ com.zaxxer
+ HikariCP
+
+
+ com.alibaba.fastjson2
+ fastjson2
+ 2.0.12
+
+
+ cn.hutool
+ hutool-all
+ 5.8.27
+
+
+ org.apache.kafka
+ kafka-clients
+ 2.7.1
+
+
+ de.codecentric
+ spring-boot-admin-client
+ 2.2.4
+
+
+ org.apache.curator
+ curator-framework
+ 5.2.0
+
+
+ org.apache.curator
+ curator-recipes
+ 5.2.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+
+ *.properties
+ *.yml
+ *.yaml
+
+
+
+
+ com.bfd.dataprocess.DataProcessApplication
+
+ true
+
+ lib/
+
+ false
+
+
+
+ config/
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-dependency-plugin
+
+
+ copy
+ package
+
+ copy-dependencies
+
+
+ ${project.build.directory}/lib/
+
+
+
+
+
+
+ maven-resources-plugin
+
+
+ copy-resources
+ package
+
+ copy-resources
+
+
+
+
+
+ src/main/resources/
+
+ *.properties
+ *.yml
+ *.yaml
+
+
+
+ ${project.build.directory}/config
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/bfd/dataprocess/DataProcessApplication.java b/src/main/java/com/bfd/dataprocess/DataProcessApplication.java
new file mode 100644
index 0000000..ee57b94
--- /dev/null
+++ b/src/main/java/com/bfd/dataprocess/DataProcessApplication.java
@@ -0,0 +1,23 @@
+package com.bfd.dataprocess;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+
+/**
+ * @author guowei
+ */
+
+@SpringBootApplication
+@Slf4j
+public class DataProcessApplication {
+ public static void main(String[] args) {
+ ConfigurableApplicationContext applicationContext = SpringApplication.run(DataProcessApplication.class, args);
+ applicationContext.getBean(DataProcessApplication.class).start();
+ }
+
+ public void start(){
+
+ }
+}
diff --git a/src/main/java/com/bfd/dataprocess/config/Constant.java b/src/main/java/com/bfd/dataprocess/config/Constant.java
new file mode 100644
index 0000000..056fd89
--- /dev/null
+++ b/src/main/java/com/bfd/dataprocess/config/Constant.java
@@ -0,0 +1,32 @@
+package com.bfd.dataprocess.config;
+
+/**
+ * @author:jinming
+ * @className:Constant
+ * @version:1.0
+ * @description:
+ * @Date:2023/8/16 15:26
+ */
+public class Constant {
+ /**
+ * 空字符串常量
+ */
+ public static final String EMPTY = "";
+
+ /**
+ * 不需要DataUtil解析的Key
+ */
+ public static final String NOT_KEY = ":$";
+
+ public final static String STOP = "stop";
+
+ public final static String OR = "or";
+
+ public final static String SELECT = "select";
+
+ public final static String OPERATION = "operation";
+
+ public final static String CHAR = "`";
+
+ public final static String TRACE= "trace";
+}
\ No newline at end of file
diff --git a/src/main/java/com/bfd/dataprocess/controller/ApiController.java b/src/main/java/com/bfd/dataprocess/controller/ApiController.java
new file mode 100644
index 0000000..0682239
--- /dev/null
+++ b/src/main/java/com/bfd/dataprocess/controller/ApiController.java
@@ -0,0 +1,64 @@
+package com.bfd.dataprocess.controller;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.bfd.dataprocess.service.ProcessService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+
+/**
+ * @author guowei
+ */
+@RestController
+@Slf4j
+@RequestMapping(value = "/api")
+@CrossOrigin(origins = "*", maxAge = 3600)
+public class ApiController {
+
+ @Resource
+ ProcessService processService;
+
+ /**
+ * insert Api
+ * @param jsonObject
+ * @return
+ */
+ @RequestMapping(value = "/insertData", method = RequestMethod.POST, produces = "application/json")
+ @ResponseBody
+ public String insertData(@RequestBody JSONObject jsonObject) {
+ processService.insertData(jsonObject);
+ return "success";
+ }
+
+ /**
+ * query Api
+ * @param jsonObject
+ * @return
+ */
+ @RequestMapping(value = "/queryData", method = RequestMethod.POST, produces = "application/json")
+ @ResponseBody
+ public String queryData(@RequestBody JSONObject jsonObject) {
+ processService.queryData(jsonObject);
+ return "success";
+ }
+
+ /**
+ * query field Api
+ * @param jsonObject
+ * @return
+ */
+ @RequestMapping(value = "/queryField", method = RequestMethod.POST, produces = "application/json")
+ @ResponseBody
+ public String queryField(@RequestBody JSONObject jsonObject) {
+ processService.queryField(jsonObject);
+ return "success";
+ }
+
+ @RequestMapping(value = "/sqlExecution", method = RequestMethod.POST, produces = "application/json")
+ @ResponseBody
+ public String sqlImpl(@RequestBody JSONObject jsonObject) {
+ processService.sqlImpl(jsonObject);
+ return "success";
+ }
+}
diff --git a/src/main/java/com/bfd/dataprocess/service/ProcessService.java b/src/main/java/com/bfd/dataprocess/service/ProcessService.java
new file mode 100644
index 0000000..c8349da
--- /dev/null
+++ b/src/main/java/com/bfd/dataprocess/service/ProcessService.java
@@ -0,0 +1,19 @@
+package com.bfd.dataprocess.service;
+
+import com.alibaba.fastjson2.JSONObject;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author guowei
+ */
+@Service
+public interface ProcessService {
+
+ void insertData(JSONObject jsonObject);
+
+ void queryData(JSONObject jsonObject);
+
+ void queryField(JSONObject jsonObject);
+
+ void sqlImpl(JSONObject jsonObject);
+}
diff --git a/src/main/java/com/bfd/dataprocess/service/impl/ProcessServiceImpl.java b/src/main/java/com/bfd/dataprocess/service/impl/ProcessServiceImpl.java
new file mode 100644
index 0000000..fb65179
--- /dev/null
+++ b/src/main/java/com/bfd/dataprocess/service/impl/ProcessServiceImpl.java
@@ -0,0 +1,543 @@
+package com.bfd.dataprocess.service.impl;
+
+import cn.hutool.core.util.IdUtil;
+import com.alibaba.fastjson2.*;
+import com.bfd.dataprocess.config.Constant;
+import com.bfd.dataprocess.service.ProcessService;
+import com.bfd.dataprocess.util.DataUtil;
+import com.bfd.dataprocess.util.DynamicDataSource;
+import com.bfd.dataprocess.util.KfkUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.sql.*;
+import java.util.*;
+
+/**
+ * @author guowei
+ */
+@Service
+@Slf4j
+public class ProcessServiceImpl implements ProcessService {
+ @Autowired
+ DynamicDataSource dynamicDataSource;
+
+ @Override
+ public void insertData(JSONObject jsonObject) {
+ Map staMap = new HashMap<>(16);
+ JSONObject data = jsonObject.getJSONObject("data");
+ //输入
+ JSONObject input = jsonObject.getJSONObject("input");
+ log.info("insertData ---> input:" + JSON.toJSONString(input));
+ log.info("insertData ---> data:" + JSON.toJSONString(data));
+ //数据库信息
+ JSONObject conn_info = input.getJSONObject("conn_info");
+ //场景id
+ Integer scenes_id = jsonObject.getInteger("scenes_id");
+ //字段
+ JSONArray form = input.getJSONArray("form");
+ Map resultMap = new HashMap<>(16);
+ resultMap.put("id", IdUtil.randomUUID());
+ try {
+ // 构建 INSERT 语句的 SQL 字符串
+ StringBuilder sqlBuilder = new StringBuilder();
+ sqlBuilder.append("INSERT INTO ").append(conn_info.getString("table")).append(" (");
+ StringBuilder valuesBuilder = new StringBuilder(") VALUES (");
+ List valueList = new ArrayList<>();
+ form.forEach(value -> {
+ JSONObject item = JSONObject.parseObject(JSON.toJSONString(value));
+ String fieldName = item.getString("field");
+ String fieldValue = item.get("value").toString();
+ if (fieldValue.isEmpty()) {
+ log.info("字段:" + fieldName + ",值为空,fieldValue:" + fieldValue);
+ return;
+ }
+ sqlBuilder.append(Constant.CHAR).append(fieldName).append(Constant.CHAR).append(", ");
+ String[] fieldValue_split = fieldValue.split(":");
+ if (fieldValue_split.length > 1) {
+ JSONObject fieldData = data.getJSONObject(fieldValue_split[0]);
+ //.getString(fieldValue_split[1]);
+ fieldValue = JSONPath.eval(fieldData, fieldValue_split[1]).toString();
+ }
+ valuesBuilder.append(" ? ").append(", ");
+ valueList.add(fieldValue);
+ });
+ // 删除最后一个逗号和空格
+ sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length());
+ valuesBuilder.delete(valuesBuilder.length() - 2, valuesBuilder.length());
+
+ sqlBuilder.append(valuesBuilder).append(")");
+ String sql = sqlBuilder.toString();
+// System.out.println("Generated SQL: " + sql);
+ log.info("Generated SQL: " + sql);
+ // 创建 PreparedStatement 对象
+ try (Connection connection = dynamicDataSource.getConnection(conn_info, scenes_id);
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+ for (int i = 0; i < valueList.size(); i++) {
+ // 根据字段的顺序设置参数值
+ preparedStatement.setObject(i + 1, valueList.get(i).toString());
+ }
+
+// System.out.println("Finall Generated INSERT SQL: " + preparedStatement.toString());
+ log.info("Finall Generated INSERT SQL: " + preparedStatement.toString());
+ // 执行插入操作
+ int rowsInserted = preparedStatement.executeUpdate();
+// System.out.println("Rows inserted: " + rowsInserted);
+ log.info("Rows inserted: " + rowsInserted);
+
+ if (rowsInserted > 0) {
+ resultMap.put("content", "写入成功");
+ staMap.put("status",1);
+ staMap.put("message","成功");
+ } else {
+ resultMap.put("content", "写入失败");
+ staMap.put("status",2);
+ staMap.put("message","失败");
+ }
+ } catch (SQLException sqlException) {
+ sqlException.printStackTrace();
+ log.error("执行插入失败", sqlException);
+ resultMap.put("content", "失败");
+ staMap.put("status",2);
+ staMap.put("message","失败,sql异常");
+ }
+
+
+ } catch (Throwable e) {
+ log.error("insert error", e);
+ e.printStackTrace();
+ resultMap.put("content", "写入失败");
+ staMap.put("status",2);
+ staMap.put("message","失败");
+ }
+ Map