|
|
@@ -1,5 +1,7 @@
|
|
|
package com.xunmei.system.util;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.xunmei.common.core.constant.Constants;
|
|
|
import com.xunmei.common.security.utils.SecurityUtils;
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
@@ -8,6 +10,7 @@ import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
public class ExportDatabaseUtil {
|
|
|
@@ -118,6 +121,61 @@ public class ExportDatabaseUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据表名和过滤字段导出指定间范围内的数据 sql语句
|
|
|
+ * @param url
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param filterColumnName 过滤字段名
|
|
|
+ * @param startDate 开始日期
|
|
|
+ * @param endDate 结束日期
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public static void exportTableSqlWithDateFilter(String url,String username,String password,String tableName,String filterColumnName,String startDate,String endDate, File file){
|
|
|
+ Connection conn = null;
|
|
|
+ Statement stmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 建立与数据库的连接
|
|
|
+ conn = DriverManager.getConnection(url, username, password);
|
|
|
+
|
|
|
+ // 创建Statement对象
|
|
|
+ stmt = conn.createStatement();
|
|
|
+
|
|
|
+ FileWriter fw = new FileWriter(file, false);
|
|
|
+ BufferedWriter bw = new BufferedWriter(fw);
|
|
|
+
|
|
|
+ //写入table
|
|
|
+ writeTableWithDateFilter(stmt,rs,bw,tableName,filterColumnName,startDate,endDate);
|
|
|
+ bw.flush();
|
|
|
+ bw.close();
|
|
|
+ // 关闭资源
|
|
|
+ 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
|
|
|
@@ -224,6 +282,44 @@ public class ExportDatabaseUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 导出指定表名sql
|
|
|
+ * @param stmt
|
|
|
+ * @param rs
|
|
|
+ * @param bw
|
|
|
+ * @param tableName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static void writeTableWithDateFilter(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName,String filterColumnName,String startDate,String endDate) throws Exception {
|
|
|
+
|
|
|
+ //写入文件头部
|
|
|
+ wirteHeader(bw);
|
|
|
+
|
|
|
+ //写入表格导出头部
|
|
|
+ writeTableHeader(bw,tableName);
|
|
|
+
|
|
|
+ //写入按时间范围删除数据语句
|
|
|
+ writeDeleteTableDataWithFilterSql(bw,tableName,filterColumnName,startDate,endDate);
|
|
|
+
|
|
|
+ Date start = DateUtil.parse(startDate);
|
|
|
+ Date end = DateUtil.parse(endDate);
|
|
|
+
|
|
|
+ // 数据量较大时一次查询全部数据很慢且内存也会爆炸,改为按天查
|
|
|
+ for (Date current = start; !current.after(end); current = DateUtil.offsetDay(current, 1)) {
|
|
|
+ Date currentEnd=DateUtil.offsetDay(current, 1);
|
|
|
+ if(currentEnd.after(end))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ writeTableDateFilterInsertSql(stmt, rs, bw, tableName, filterColumnName,
|
|
|
+ DateUtil.format(current, Constants.DAILY_FORMAT),
|
|
|
+ DateUtil.format(currentEnd, Constants.DAILY_FORMAT));
|
|
|
+ }
|
|
|
+
|
|
|
+ //写入插入语句
|
|
|
+// writeTableDateFilterInsertSql(stmt,rs,bw,tableName,filterColumnName,startDate,endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 写入sql语句
|
|
|
* @param tables 数据库所有表名
|
|
|
* @param stmt Statement对象
|
|
|
@@ -314,6 +410,23 @@ public class ExportDatabaseUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 写入按时间范围删除表数据SQL
|
|
|
+ * @param bw
|
|
|
+ * @param tableName
|
|
|
+ * @param filterColumnName
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static void writeDeleteTableDataWithFilterSql(BufferedWriter bw,String tableName,String filterColumnName,String startDate,String endDate) throws SQLException, IOException {
|
|
|
+ //写入删除表数据语句
|
|
|
+ String dropTableDataSql =String.format("DELETE FROM %s WHERE `%s` >= '%s' AND `%s`< '%s' ;",tableName,filterColumnName,startDate,filterColumnName,endDate);
|
|
|
+ bw.write(dropTableDataSql);
|
|
|
+ bw.newLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 写入insert语句
|
|
|
* @param stmt Statement对象
|
|
|
* @param rs ResultSet结果对象
|
|
|
@@ -358,6 +471,57 @@ public class ExportDatabaseUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 按过滤条件写入insert语句
|
|
|
+ * @param stmt
|
|
|
+ * @param rs
|
|
|
+ * @param bw
|
|
|
+ * @param tableName
|
|
|
+ * @param filterColumnName
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @throws SQLException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static void writeTableDateFilterInsertSql(Statement stmt,ResultSet rs,BufferedWriter bw,String tableName,String filterColumnName,String startDate,String endDate) throws SQLException, IOException {
|
|
|
+ String columnStr = getColumnStr(stmt, tableName);
|
|
|
+
|
|
|
+// String selectSql = "SELECT "+ columnStr +" FROM `" + tableName + "`";
|
|
|
+ String selectSql =String.format("SELECT %s FROM `%s` WHERE `%s` >= '%s' AND `%s`< '%s' ;",columnStr,tableName,filterColumnName,startDate,filterColumnName,endDate);
|
|
|
+ // 生成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 查询结果对象
|