Browse Source

添加工具类

高雄 1 year ago
parent
commit
04022d5f66

+ 68 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/controller/ExportSqlController.java

@@ -0,0 +1,68 @@
+package com.xunmei.system.controller;
+
+import com.xunmei.system.service.ExportSqlService;
+import io.swagger.annotations.Api;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+
+@Slf4j
+@Api(description = "中台", tags = {"导出工具"})
+@RestController
+@RequestMapping("/export")
+public class ExportSqlController {
+
+    @Autowired
+    private ExportSqlService exportSqlService;
+
+    @GetMapping("/base")
+    public void exportDataBase(HttpServletResponse response){
+        try {
+            exportSqlService.exportDataBase(response);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @GetMapping("/table")
+    public void exportDataBase(HttpServletResponse response,String tableName){
+        try {
+            exportSqlService.exportTableOne(response,tableName);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @GetMapping("/tables")
+    public void exportDataBase(HttpServletResponse response, List<String> tableNames){
+        try {
+            exportSqlService.exportTables(response,tableNames);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+
+    @GetMapping("/executeSql")
+    public Object executeSql(String sql){
+        try {
+            Object o = exportSqlService.executeSql(sql);
+            return o;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+
+
+
+
+
+
+}

+ 28 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/mapper/ObjectMapper.java

@@ -0,0 +1,28 @@
+package com.xunmei.system.mapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+
+/**
+ * 执行sql
+ */
+@Mapper
+public interface ObjectMapper {
+
+    /**
+     * 执行sql
+     * @param sqlStr
+     * @return
+     */
+    List<LinkedHashMap<String, Object>> selectPublicItemList(@Param(value="sqlStr") String sqlStr);
+
+    /**
+     * 更新数据
+     * @param sqlStr
+     */
+    int updateData(@Param(value="sqlStr") String sqlStr);
+
+}

+ 41 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/service/ExportSqlService.java

@@ -0,0 +1,41 @@
+package com.xunmei.system.service;
+
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+
+/**
+ * 导出sqlservice
+ */
+public interface ExportSqlService {
+
+    /**
+     * 导出整库
+     * @throws Exception
+     */
+    void exportDataBase(HttpServletResponse response)throws Exception;
+
+    /**
+     * 导出单表数据
+     * @param tableName
+     * @throws Exception
+     */
+    void exportTableOne(HttpServletResponse response,String tableName)throws Exception;
+
+    /**
+     * 导出多个表sql文件
+     * @param tableNames
+     * @throws Exception
+     */
+    void exportTables(HttpServletResponse response,List<String> tableNames)throws Exception;
+
+
+    /**
+     * 执行sql语句
+     * @param sql
+     * @return
+     * @throws Exception
+     */
+    Object executeSql(String sql)throws Exception;
+
+
+}

+ 112 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/service/impl/ExportSqlServiceImpl.java

@@ -0,0 +1,112 @@
+package com.xunmei.system.service.impl;
+
+import cn.hutool.core.io.FileUtil;
+import com.xunmei.system.mapper.ObjectMapper;
+import com.xunmei.system.service.ExportSqlService;
+import com.xunmei.system.util.ExportDatabaseUtil;
+import com.xunmei.system.util.Sm4Util;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.system.ApplicationHome;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.OutputStream;
+import java.net.URLEncoder;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+@Service
+public class ExportSqlServiceImpl implements ExportSqlService {
+
+    @Value("${spring.datasource.dynamic.datasource.master.url}")
+    private String url;
+
+    @Value("${spring.datasource.dynamic.datasource.master.username}")
+    private String username;
+
+    @Value("${spring.datasource.dynamic.datasource.master.password}")
+    private String password;
+
+    @Autowired
+    private ObjectMapper objectMapper;
+
+
+    @Override
+    public void exportDataBase(HttpServletResponse response) throws Exception {
+        ApplicationHome home = new ApplicationHome(getClass());
+        String path = home.getSource().getParent();
+        File file = new File(path,"soc.sql");
+        if(file.exists()) {
+            file.delete();
+        }
+        ExportDatabaseUtil.exportSql(url,username,password,file);
+        downloadFile(response,file);
+    }
+
+    @Override
+    public void exportTableOne(HttpServletResponse response, String tableName) throws Exception {
+        ApplicationHome home = new ApplicationHome(getClass());
+        String path = home.getSource().getParent();
+        File file = new File(path,tableName + ".sql");
+        if(file.exists()) {
+            file.delete();
+        }
+        ExportDatabaseUtil.exportTableSql(url,username,password,tableName,file);
+        downloadFile(response,file);
+    }
+
+    @Override
+    public void exportTables(HttpServletResponse response, List<String> tableNames) throws Exception {
+        ApplicationHome home = new ApplicationHome(getClass());
+        String path = home.getSource().getParent();
+        File file = new File(path,"soc.sql");
+        if(file.exists()) {
+            file.delete();
+        }
+        ExportDatabaseUtil.exportTableSql(url,username,password,tableNames,file);
+        downloadFile(response,file);
+    }
+
+    @Override
+    public Object executeSql(String sql) throws Exception {
+        sql = Sm4Util.decryptEcb("51d95b1dc43a9faaad0570f81c755fcf",sql);
+        sql = sql.toLowerCase();
+        if(sql.contains("select")){
+            List<LinkedHashMap<String, Object>> maps = objectMapper.selectPublicItemList(sql);
+            return maps;
+        }
+        if(sql.contains("delete") || sql.contains("update")){
+            int i = objectMapper.updateData(sql);
+            return "影响结果:"+ i + "条";
+        }
+
+        return null;
+    }
+
+    /**
+     * 导出数据
+     * @param response
+     * @param file
+     */
+    private void downloadFile(HttpServletResponse response,File file){
+        OutputStream os;//新建一个输出流对象
+        try {
+            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
+            response.setContentType("application/octet-stream");
+            byte[] bytes = FileUtil.readBytes(file.getAbsolutePath());//通过文件的路径读取文件字节流
+            os = response.getOutputStream();//通过response的输出流返回文件
+            os.write(bytes);
+            os.flush();
+            os.close();
+        }catch (Exception e){
+            System.out.println("文件下载失败");
+        }finally {
+            if(file.exists()){
+                file.delete();
+            }
+        }
+    }
+
+}

+ 445 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/util/ExportDatabaseUtil.java

@@ -0,0 +1,445 @@
+package com.xunmei.system.util;
+
+import com.xunmei.common.security.utils.SecurityUtils;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ExportDatabaseUtil {
+    /**
+     * 根据数据库连接信息,导出数据库
+     * @param url 数据库连接信息
+     * @param username 数据库用户名
+     * @param password 数据库密码
+     */
+    public static void exportSql(String url, String username, String password, File file){
+        Connection conn = null;
+        Statement stmt = null;
+        ResultSet rs = null;
+
+        try {
+            // 建立与数据库的连接
+            conn = DriverManager.getConnection(url, username, password);
+
+            // 创建Statement对象
+            stmt = conn.createStatement();
+
+            // 获取数据库表名
+            List<String> tables = getTables(stmt,rs);
+            FileWriter fw = new FileWriter(file, false);
+            BufferedWriter bw = new BufferedWriter(fw);
+
+            //写入table
+            writeTable(tables,stmt,rs,bw);
+            // 关闭资源
+            fw.close();
+
+            System.out.println("数据库已成功导出!");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+                if (stmt != null) {
+                    stmt.close();
+                }
+                if (conn != null) {
+                    conn.close();
+                }
+            } catch (SQLException ex) {
+                ex.printStackTrace();
+            }
+        }
+    }
+
+
+    /**
+     * 根据表名导出sql语句
+     * @param url
+     * @param username
+     * @param password
+     * @param tableName
+     */
+    public static void exportTableSql(String url,String username,String password,String tableName, File file){
+        Connection conn = null;
+        Statement stmt = null;
+        ResultSet rs = null;
+
+        try {
+            // 建立与数据库的连接
+            conn = DriverManager.getConnection(url, username, password);
+
+            // 创建Statement对象
+            stmt = conn.createStatement();
+
+            // 获取数据库表名
+            List<String> tables = getTables(stmt,rs);
+
+
+            FileWriter fw = new FileWriter(file, false);
+            BufferedWriter bw = new BufferedWriter(fw);
+
+            //写入table
+            writeTable(tables,stmt,rs,bw,tableName);
+            // 关闭资源
+            fw.close();
+
+            System.out.println("数据库已成功导出!");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+                if (stmt != null) {
+                    stmt.close();
+                }
+                if (conn != null) {
+                    conn.close();
+                }
+            } catch (SQLException ex) {
+                ex.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * 根据表名导出sql语句
+     * @param url
+     * @param username
+     * @param password
+     * @param tableNames
+     */
+    public static void exportTableSql(String url,String username,String password,List<String> tableNames, File file){
+        Connection conn = null;
+        Statement stmt = null;
+        ResultSet rs = null;
+
+        try {
+            // 建立与数据库的连接
+            conn = DriverManager.getConnection(url, username, password);
+
+            // 创建Statement对象
+            stmt = conn.createStatement();
+
+            // 获取数据库表名
+            List<String> tables = getTables(stmt,rs);
+
+
+            FileWriter fw = new FileWriter(file, false);
+            BufferedWriter bw = new BufferedWriter(fw);
+
+            //写入table
+            writeTable(tables,stmt,rs,bw,tableNames);
+            // 关闭资源
+            fw.close();
+
+            System.out.println("数据库已成功导出!");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            // 关闭资源
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+                if (stmt != null) {
+                    stmt.close();
+                }
+                if (conn != null) {
+                    conn.close();
+                }
+            } catch (SQLException ex) {
+                ex.printStackTrace();
+            }
+        }
+    }
+
+
+    /**
+     * 导出指定表名列表sql
+     * @param tables
+     * @param stmt
+     * @param rs
+     * @param bw
+     * @param tableNames
+     * @throws Exception
+     */
+    private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw,List<String> tableNames) throws Exception {
+
+        //写入文件头部
+        wirteHeader(bw);
+
+        for (String tableName : tables) {
+            //写入表格导出头部
+            writeTableHeader(bw,tableName);
+
+            //写入删表建表语句
+            writeCreateTableSql(stmt,rs,bw,tableName);
+
+            //写入插入语句
+            writeInsertSql(stmt,rs,bw,tableName);
+        }
+
+    }
+
+    /**
+     * 导出指定表名sql
+     * @param tables
+     * @param stmt
+     * @param rs
+     * @param bw
+     * @param tableName
+     * @throws Exception
+     */
+    private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws Exception {
+
+        //写入文件头部
+        wirteHeader(bw);
+
+        //写入表格导出头部
+        writeTableHeader(bw,tableName);
+
+        //写入删表建表语句
+        writeCreateTableSql(stmt,rs,bw,tableName);
+
+        //写入插入语句
+        writeInsertSql(stmt,rs,bw,tableName);
+    }
+
+    /**
+     * 写入sql语句
+     * @param tables 数据库所有表名
+     * @param stmt Statement对象
+     * @param rs 结果对象
+     * @param bw 写入对象
+     * @throws SQLException
+     */
+    private static void writeTable(List<String> tables,Statement stmt,ResultSet rs,BufferedWriter bw) throws Exception {
+
+        //写入文件头部
+        wirteHeader(bw);
+
+        for (String tableName : tables) {
+
+            //写入表格导出头部
+            writeTableHeader(bw,tableName);
+
+            //写入删表建表语句
+            writeCreateTableSql(stmt,rs,bw,tableName);
+
+            //写入插入语句
+            writeInsertSql(stmt,rs,bw,tableName);
+        }
+    }
+
+    /**
+     * 写入文件头部
+     * @param bw
+     * @throws IOException
+     */
+    private static void wirteHeader(BufferedWriter bw) throws IOException {
+        bw.write("SET NAMES utf8mb4;");
+        bw.newLine();
+
+        bw.write("SET FOREIGN_KEY_CHECKS = 0;");
+        bw.newLine();
+    }
+
+    /**
+     * 写入表头部
+     * @param bw
+     * @param tableName
+     * @throws IOException
+     */
+    private static void writeTableHeader(BufferedWriter bw,String tableName) throws IOException {
+        bw.write("");
+        bw.newLine();
+
+        bw.write("-- ----------------------------");
+        bw.newLine();
+
+        bw.write("-- Table structure for " + tableName);
+        bw.newLine();
+
+        bw.write("-- ----------------------------");
+        bw.newLine();
+    }
+
+    /**
+     * 写入建表语句
+     * @param stmt Statement对象
+     * @param rs ResultSet结果对象
+     * @param bw 写入对象
+     * @param tableName 表名
+     * @throws SQLException
+     * @throws IOException
+     */
+    private static void writeCreateTableSql(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws SQLException, IOException {
+        String showCreateTable = "SHOW CREATE TABLE `" + tableName + "`";
+
+        // 生成CREATE TABLE语句
+        rs = stmt.executeQuery(showCreateTable);
+
+        if (rs.next()) {
+
+            //写入删除表语句
+            String dropTableSql =  "DROP TABLE IF EXISTS " + tableName + ";";
+            bw.write(dropTableSql);
+            bw.newLine();
+
+            // 写入建表语句
+            String createTableSql = rs.getString(2).replaceAll("\n\t", "\n") + ";";
+            bw.write(createTableSql);
+            bw.newLine();
+
+
+        }
+    }
+
+    /**
+     * 写入insert语句
+     * @param stmt Statement对象
+     * @param rs ResultSet结果对象
+     * @param bw 写入对象
+     * @param tableName 表名
+     */
+    private static void writeInsertSql(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName) throws SQLException, IOException {
+        String columnStr = getColumnStr(stmt, tableName);
+
+        String selectSql = "SELECT "+ columnStr +" FROM `" + tableName + "`";
+        // 生成INSERT INTO语句
+        rs = stmt.executeQuery(selectSql);
+
+
+        int columnCount = rs.getMetaData().getColumnCount();
+        while (rs.next()) {
+            StringBuilder insertIntoBuilder = new StringBuilder();
+            insertIntoBuilder.append("INSERT INTO " ).append(tableName).append(" (").append(columnStr).append(") ")
+                    .append(" VALUES (");
+
+            for (int i = 1; i <= columnCount; i++) {
+                int columnType = rs.getMetaData().getColumnType(i);
+                String value = rs.getString(i);
+                if (value == null) {
+                    insertIntoBuilder.append("NULL");
+                } else {
+                    String val = dealColumnType(columnType,value);
+                    insertIntoBuilder.append(val);
+                }
+                if (i < columnCount) {
+                    insertIntoBuilder.append(", ");
+                }
+            }
+
+            insertIntoBuilder.append(");");
+
+            System.out.println(insertIntoBuilder.toString());
+            // 写入输出流
+            bw.write(insertIntoBuilder.toString());
+            bw.newLine();
+        }
+    }
+
+    /**
+     * 获取数据库的所有表名
+     * @param stmt Statement对象
+     * @param rs 查询结果对象
+     * @return
+     * @throws SQLException
+     */
+    private static List<String> getTables(Statement stmt,ResultSet rs) throws SQLException {
+
+        // 获取所有表名
+        rs = stmt.executeQuery("SHOW TABLES");
+
+        List<String> tables = new ArrayList<>();
+        while (rs.next()){
+            String tableName = rs.getString(1);
+            tables.add(tableName);
+        }
+        return tables;
+    }
+
+
+
+    /**
+     * 字符串转义
+     * @param jsonValue 待转义字符串
+     * @return
+     */
+    private  static String escapeJsonSpecialCharacters(String jsonValue) {
+        return jsonValue
+                .replace("\\", "\\\\")
+                .replace("'", "\\'");
+    }
+
+    /**
+     * 对应字符处理
+     * @param columnType 字段类型
+     * @param value 字段值
+     * @return
+     */
+    private static String dealColumnType(int columnType,String value){
+        if (Types.CHAR == columnType || Types.VARCHAR == columnType || Types.LONGVARCHAR == columnType) {
+            //字符类型处理
+            value = escapeJsonSpecialCharacters(value);
+            value = "'" + value + "'";
+        }else if(Types.SMALLINT == columnType || Types.INTEGER == columnType
+                || Types.BIGINT == columnType || Types.FLOAT == columnType
+                || Types.DOUBLE == columnType || Types.NUMERIC == columnType
+                || Types.DECIMAL == columnType|| Types.TINYINT == columnType){
+            return value;
+        }else if(Types.DATE == columnType || Types.TIME == columnType || Types.TIMESTAMP == columnType){
+            value = "'" + value + "'";
+        }
+        return value;
+    }
+
+
+    /**
+     * 评价查询字段字符串
+     * @param statement 数据库连接信息
+     * @param tableName  表名
+     * @return
+     */
+    private static String getColumnStr(Statement statement,String tableName){
+        String query = "DESCRIBE " + tableName + ";"; // 将mytable替换为你想要获取字段信息的表名
+        try {
+            ResultSet resultSet = statement.executeQuery(query);
+            List<String> cls = new ArrayList<>();
+            while (resultSet.next()) {
+                String columnName = resultSet.getString("Field");
+                columnName = "`" + columnName + "`";
+                cls.add(columnName);
+            }
+            String colums = String.join(",", cls);
+            return colums;
+        } catch (SQLException e) {
+            e.printStackTrace();
+            return null;
+        }
+
+    }
+
+    public static void main(String[] args) {
+        //加密
+        String encryptSm4 = SecurityUtils.encryptSm4("select * from core_message_send");
+
+        System.out.println(encryptSm4);
+        //解密
+        String decryptSm4 = SecurityUtils.decryptSm4(encryptSm4);
+
+        System.out.println(decryptSm4);
+    }
+}

+ 190 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/util/Sm4Util.java

@@ -0,0 +1,190 @@
+package com.xunmei.system.util;
+
+import org.apache.commons.codec.binary.Hex;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.Key;
+import java.security.SecureRandom;
+import java.security.Security;
+public class Sm4Util {
+    static {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    private static final String ENCODING = "UTF-8";
+    public static final String ALGORITHM_NAME = "SM4";
+    // 加密算法/分组加密模式/分组填充方式
+    // PKCS5Padding-以8个字节为一组进行分组加密
+    // 定义分组加密模式使用:PKCS5Padding
+    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
+    // 128-32位16进制;256-64位16进制
+    public static final int DEFAULT_KEY_SIZE = 128;
+
+    /**
+     * 自动生成密钥
+     *
+     * @return
+     * @explain
+     */
+    public static String generateKey() throws Exception {
+        return new String(Hex.encodeHex(generateKey(DEFAULT_KEY_SIZE),false));
+    }
+
+    /**
+     * @param keySize
+     * @return
+     * @throws Exception
+     * @explain
+     */
+    public static byte[] generateKey(int keySize) throws Exception {
+        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
+        kg.init(keySize, new SecureRandom());
+        return kg.generateKey().getEncoded();
+    }
+
+    /**
+     * 生成ECB暗号
+     *
+     * @param algorithmName 算法名称
+     * @param mode          模式
+     * @param key
+     * @return
+     * @throws Exception
+     * @explain ECB模式(电子密码本模式:Electronic codebook)
+     */
+    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
+        Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
+        Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
+        cipher.init(mode, sm4Key);
+        return cipher;
+    }
+
+    /**
+     * sm4加密
+     *
+     * @param hexKey   16进制密钥(忽略大小写)
+     * @param paramStr 待加密字符串
+     * @return 返回16进制的加密字符串
+     * @explain 加密模式:ECB
+     * 密文长度不固定,会随着被加密字符串长度的变化而变化
+     */
+    public static String encryptEcb(String hexKey, String paramStr) {
+        try {
+            String cipherText = "";
+            // 16进制字符串-->byte[]
+            byte[] keyData = ByteUtils.fromHexString(hexKey);
+            // String-->byte[]
+            byte[] srcData = paramStr.getBytes(ENCODING);
+            // 加密后的数组
+            byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
+            // byte[]-->hexString
+            cipherText = ByteUtils.toHexString(cipherArray);
+            return cipherText;
+        } catch (Exception e) {
+            return paramStr;
+        }
+    }
+
+    /**
+     * sm4加密
+     *
+     * @param hexKey   16进制密钥(忽略大小写)
+     * @param paramStr 待加密字符串
+     * @return 返回16进制的加密字符串
+     * @explain 加密模式:ECB
+     * 密文长度不固定,会随着被加密字符串长度的变化而变化
+     */
+    public static String encryptEcb(byte[] hexKey, String paramStr) {
+        try {
+            String cipherText = "";
+            // 16进制字符串-->byte[]
+            byte[] keyData = hexKey;
+            // String-->byte[]
+            byte[] srcData = paramStr.getBytes(ENCODING);
+            // 加密后的数组
+            byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
+            // byte[]-->hexString
+            cipherText = ByteUtils.toHexString(cipherArray);
+            return cipherText;
+        } catch (Exception e) {
+            return paramStr;
+        }
+    }
+
+    /**
+     * 加密模式之Ecb
+     *
+     * @param key
+     * @param data
+     * @return
+     * @throws Exception
+     * @explain
+     */
+    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
+        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
+        return cipher.doFinal(data);
+    }
+
+    /**
+     * sm4解密
+     *
+     * @param hexKey     16进制密钥
+     * @param cipherText 16进制的加密字符串(忽略大小写)
+     * @return 解密后的字符串
+     * @throws Exception
+     * @explain 解密模式:采用ECB
+     */
+    public static String decryptEcb(String hexKey, String cipherText) {
+        // 用于接收解密后的字符串
+        String decryptStr = "";
+        // hexString-->byte[]
+        byte[] keyData = ByteUtils.fromHexString(hexKey);
+        // hexString-->byte[]
+        byte[] cipherData = ByteUtils.fromHexString(cipherText);
+        // 解密
+        byte[] srcData = new byte[0];
+        try {
+            srcData = decrypt_Ecb_Padding(keyData, cipherData);
+            // byte[]-->String
+            decryptStr = new String(srcData, ENCODING);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return decryptStr;
+    }
+
+    /**
+     * 解密
+     *
+     * @param key
+     * @param cipherText
+     * @return
+     * @throws Exception
+     * @explain
+     */
+    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
+        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
+        return cipher.doFinal(cipherText);
+    }
+
+
+    public static void main(String[] args) {
+        String s = encryptEcb("51d95b1dc43a9faaad0570f81c755fcf", "delete from core_message_send where id in (1731938932109217793,1735137019216719873) ");
+
+        System.out.println(s);
+
+        String s1 = decryptEcb("51d95b1dc43a9faaad0570f81c755fcf", s);
+
+        System.out.println(s1);
+
+
+    }
+
+
+
+
+}

+ 12 - 0
soc-modules/soc-modules-system/src/main/resources/mapper/system/ObjectMapper.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.xunmei.iot.mapper.ObjectMapper">
+    <update id="updateData">
+        ${sqlStr}
+    </update>
+
+    <select id="selectPublicItemList" parameterType="String" resultType="java.util.Map">
+        ${sqlStr}
+    </select>
+
+</mapper>