获取Mysql、Oracle、SqlServer 库结构、表结构、表内数据工具类

(Es数据库后续补充、分页根据自身情况实现)


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import diy.application.dto.query.DataSourceQuery;
import diy.application.entity.DataSourceInfo;
import diy.application.entity.DataSourceTableConstruct;
import diy.application.entity.DataTableColumnComments;
import diy.application.mapper.DataSourceColumnCommentsMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.xpack.sql.jdbc.EsDataSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.util.*;
import java.util.function.Function;

import static diy.application.utils.Constant.*;



@Component
@Slf4j
public class DataSourceUtil {

    private DataSourceUtil() {
    }

    @Resource
    DataSourceColumnCommentsMapper dataSourceColumnCommentsMapper;

    private static final String TABLE_NAME_STR = "TABLE_NAME";

    private static final String TABLE_CAT_STR = "TABLE_CAT";

    private static final String TOTAL_STR = "total";


    public List<String> getSchemas(DataSourceInfo dataSourceInfo) throws SQLException {
        String dataSourceType = dataSourceInfo.getType();

        // JDBC类型
        if (DATA_SOURCE_JDBC.equalsIgnoreCase(dataSourceType)) {
            return getRDBSConnection(dataSourceInfo, null, null).getSchemas();
        }
        // ES类型
        else if (DATA_SOURCE_ES.equalsIgnoreCase(dataSourceType)) {
            return getEsSchemas(getEsConnection(dataSourceInfo));
        }

        return Collections.emptyList();
    }

    public List<String> getTables(DataSourceInfo dataSourceInfo, String schemaName) throws SQLException {
        String dataSourceType = dataSourceInfo.getType();

        // JDBC类型
        if (DATA_SOURCE_JDBC.equalsIgnoreCase(dataSourceType)) {
            return getRDBSConnection(dataSourceInfo, schemaName, null).getTables();
        }
        // ES类型
        else if (DATA_SOURCE_ES.equalsIgnoreCase(dataSourceType)) {
            Connection esConnection = getEsConnection(dataSourceInfo);
            return getEsSchemas(esConnection);
        }

        return Collections.emptyList();
    }

    public List<DataSourceTableConstruct> getTableConstruct(DataSourceInfo dataSourceInfo, String schemaName, String tableName) throws SQLException {
        String dataSourceType = dataSourceInfo.getType();

        // JDBC类型
        if (DATA_SOURCE_JDBC.equalsIgnoreCase(dataSourceType)) {
            List<DataSourceTableConstruct> dataSourceTableConstructList = getRDBSConnection(dataSourceInfo, schemaName, tableName).getTableConstruct();
            return refreshColumnComments(dataSourceTableConstructList);
        }
        // ES类型
        else if (DATA_SOURCE_ES.equalsIgnoreCase(dataSourceType)) {
            Connection esConnection = getEsConnection(dataSourceInfo);
            // return getEsSchemas(esConnection);
        }

        return Collections.emptyList();
    }

    public JSONObject getTableData(DataSourceInfo dataSourceInfo, DataSourceQuery dataSourceQuery) throws SQLException {
        String dataSourceType = dataSourceInfo.getType();

        // JDBC类型
        if (DATA_SOURCE_JDBC.equalsIgnoreCase(dataSourceType)) {
            String schemaName = dataSourceQuery.getSchemaName();
            String tableName = dataSourceQuery.getTableName();
            // 获取表格数据
            List<String> columnNameList = new ArrayList<>();
            List<Map<String, String>> tableData = getRDBSConnection(dataSourceInfo, schemaName, tableName).getTableData(dataSourceQuery, columnNameList);
            // 获取数据总条目
            Integer total = getRDBSConnection(dataSourceInfo, schemaName, tableName).getTotal();
            return constructTableData(columnNameList, tableData, total);
        }
        // ES类型
        else if (DATA_SOURCE_ES.equalsIgnoreCase(dataSourceType)) {
            Connection esConnection = getEsConnection(dataSourceInfo);
            // return getEsSchemas(esConnection);
        }

        return new JSONObject();
    }

    public JSONObject getTableDataByStr(DataSourceInfo dataSourceInfo, DataSourceQuery dataSourceQuery) throws SQLException {
        String dataSourceType = dataSourceInfo.getType();
        String executeSql = dataSourceQuery.getExecuteStr().replace("\r", " ").replace("\n", " ");
        // JDBC类型
        if (DATA_SOURCE_JDBC.equalsIgnoreCase(dataSourceType)) {
            // 获取表格数据
            List<String> columnNameList = new ArrayList<>();
            List<Map<String, String>> tableData = getRDBSConnection(dataSourceInfo, null, null)
                    .getTableDataBySql(dataSourceQuery, executeSql, columnNameList);
            // 获取数据总条目
            return constructTableData(columnNameList, tableData, tableData.size());
        }
        // ES类型
        else if (DATA_SOURCE_ES.equalsIgnoreCase(dataSourceType)) {
            Connection esConnection = getEsConnection(dataSourceInfo);
            // return getEsSchemas(esConnection);
        }

        return new JSONObject();
    }


    /**
     * 数据组装
     *
     * @return "tableConfig":{"columns":[{"prop":"schema_name","label":"column_name"}],"data":[{"schema_name":"DW"}]}
     */
    private JSONObject constructTableData(List<String> columnNameList, List<Map<String, String>> tableData, Integer total) {

        JSONObject response = new JSONObject();
        JSONObject tableConfig = new JSONObject();
        // 整理columns
        ArrayList<Map<String, String>> columns = new ArrayList<>();
        HashMap<String, String> map;
        for (String columnName : columnNameList) {
            map = new HashMap<>();
            map.put("prop", columnName);
            map.put("label", columnName);
            columns.add(map);
        }
        tableConfig.putOpt("columns", columns);

        // 整理data
        tableConfig.putOpt("data", tableData);
        // 总数
        tableConfig.putOpt(TOTAL_STR, total);

        response.putOpt("tableConfig", tableConfig);
        return response;
    }


    /**
     * 刷新注释
     *
     * @param list list
     */
    private List<DataSourceTableConstruct> refreshColumnComments(List<DataSourceTableConstruct> list) {
        if (CollUtil.isEmpty(list)) {
            return list;
        }

        // 查询有自定义注释的列
        List<DataTableColumnComments> dataTableColumnComments = dataSourceColumnCommentsMapper.selectColumnComments(list);
        for (DataSourceTableConstruct a : list) {
            for (DataTableColumnComments b : dataTableColumnComments) {
                // 匹配赋值
                if (ObjectUtil.equals(a.getDataSourceId(), b.getDataSourceId()) && ObjectUtil.equals(a.getSchemaName(), b.getSchemaName())
                        && ObjectUtil.equals(a.getTableName(), b.getTableName()) && ObjectUtil.equals(a.getColumnName(), b.getColumnName())) {
                    a.setCommentId(b.getId());
                    a.setComments(b.getComments());
                }
            }
        }
        return list;
    }


    /**
     * 获取jdbc连接
     *
     * @param dataSourceInfo dataSource
     */
    @SneakyThrows
    public RDBSInterface getRDBSConnection(DataSourceInfo dataSourceInfo, String schemaName, String tableName) throws SQLException {
        String url = dataSourceInfo.getUrl();
        String userName = dataSourceInfo.getUserName();
        String password = dataSourceInfo.getPassword();
        // 获取Connection、获取数据库信息
        Connection connection = DriverManager.getConnection(url, userName, password);
        return Objects.requireNonNull(getRDBSConnection(dataSourceInfo, (String x) -> {
            try {
                Class.forName(x);
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
                throw new RuntimeException();
            }
            return connection;
        }, schemaName, tableName));
    }

    private RDBSInterface getRDBSConnection(DataSourceInfo dataSourceInfo, Function<String, Connection> function,
                                            String schemaName, String tableName) {
        Connection connection;
        Integer dataSourceInfoId = dataSourceInfo.getId();
        String url = dataSourceInfo.getUrl();
        if (url.contains(DATA_SOURCE_MYSQL)) {
            connection = function.apply(MYSQL_DRIVER);
            return new RDBSInterface.MysqlInfo(dataSourceInfoId, connection, schemaName, tableName);
        } else if (url.contains(DATA_SOURCE_ORACLE)) {
            connection = function.apply(ORACLE_DRIVER);
            return new RDBSInterface.OracleInfo(dataSourceInfoId, connection, schemaName, tableName);
        } else if (url.contains(DATA_SOURCE_SQLSERVER)) {
            connection = function.apply(SQL_SERVER_DRIVER);
            return new RDBSInterface.SqlServerInfo(dataSourceInfoId, connection, schemaName, tableName);
        }
        return null;
    }


    /**
     * 关系型数据库
     * Mysql Oracle SqlServer
     */
    private interface RDBSInterface {

        List<String> getSchemas() throws SQLException;

        List<String> getTables() throws SQLException;

        List<DataSourceTableConstruct> getTableConstruct() throws SQLException;

        List<Map<String, String>> getTableData(DataSourceQuery dataSourceQuery, List<String> columnNameList) throws SQLException;

        List<Map<String, String>> getTableDataBySql(DataSourceQuery dataSourceQuery, String executeSql, List<String> columnNameList) throws SQLException;

        Integer getTotal() throws SQLException;

        // 执行sql获取指定columnName的值
        default List<String> getAssignColumnValue(Connection conn, String sql, String columnName) throws SQLException {
            try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
                ResultSet res = preparedStatement.executeQuery();
                List<String> schemaNameList = new ArrayList<>();
                while (res.next()) {
                    String schemaName = res.getString(columnName);
                    schemaNameList.add(schemaName);
                }
                return schemaNameList;
            } finally {
                conn.close();
            }
        }

        // 执行sql获取指定columnNameList的值
        default List<Map<String, String>> getTableData(Connection conn, String sql, List<String> columnNameList) throws SQLException {
            HashMap<String, String> hashMap;
            List<Map<String, String>> hashMapList = new ArrayList<>();
            String columnValue = "";
            try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
                ResultSet res = preparedStatement.executeQuery();
                ResultSetMetaData metaData = res.getMetaData();
                // 获取列名称
                int columnCount = metaData.getColumnCount();
                for (int i = 0; i < columnCount; i++) {
                    columnNameList.add(metaData.getColumnName(i + 1));
                }
                while (res.next()) {
                    hashMap = new HashMap<>();
                    for (String columnNameStr : columnNameList) {
                        columnValue = res.getString(columnNameStr);
                        hashMap.put(columnNameStr, ObjectUtil.isEmpty(columnValue) ? "" : columnValue);
                    }
                    hashMapList.add(hashMap);
                }
                return hashMapList;
            } finally {
                conn.close();
            }
        }

        // 获取统一格式数据表结构
        default List<DataSourceTableConstruct> getTableConstruct(Connection conn, String sql) throws SQLException {
            try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
                ResultSet res = preparedStatement.executeQuery();
                List<DataSourceTableConstruct> dataSourceTableInfoArrayList = new ArrayList<>();
                DataSourceTableConstruct dataSourceTableInfo;
                while (res.next()) {
                    dataSourceTableInfo = new DataSourceTableConstruct();
                    dataSourceTableInfo.setDataSourceId(Integer.valueOf(res.getString("data_source_id")));
                    dataSourceTableInfo.setSchemaName(res.getString("schema_name"));
                    dataSourceTableInfo.setTableName(res.getString("table_name"));
                    dataSourceTableInfo.setColumnName(res.getString("column_name"));
                    dataSourceTableInfo.setDataType(res.getString("data_type"));
                    dataSourceTableInfo.setCommentsOri(res.getString("comments_ori"));
                    dataSourceTableInfoArrayList.add(dataSourceTableInfo);
                }
                return dataSourceTableInfoArrayList;
            } finally {
                conn.close();
            }
        }

        // 数据源总数
        default Integer getTotal(Connection conn, String schemaName, String tableName) throws SQLException {
            String sql = "select count(*) from " + schemaName + "." + tableName;

            try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
                ResultSet res = preparedStatement.executeQuery();
                if (res.next()) {
                    return res.getInt(1);
                }
            } finally {
                conn.close();
            }
            return ZERO;
        }

        class MysqlInfo implements RDBSInterface {

            Integer dataSourceId;
            Connection conn;
            String schemaName;
            String tableName;

            public MysqlInfo(Integer dataSourceId, Connection conn, String schemaName, String tableName) {
                this.dataSourceId = dataSourceId;
                this.conn = conn;
                this.schemaName = schemaName;
                this.tableName = tableName;
            }

            @Override
            public List<String> getSchemas() throws SQLException {
                return getAssignColumnValue(conn, "show databases", "Database");
            }

            @Override
            public List<String> getTables() throws SQLException {
                return getAssignColumnValue(conn, "select table_name from information_schema.tables where table_schema = '" + schemaName + "'", TABLE_NAME_STR);
            }

            @Override
            public List<DataSourceTableConstruct> getTableConstruct() throws SQLException {
                String sql = "select '" + dataSourceId + "' data_source_id, '" + schemaName + "' schema_name, '" + tableName + "' table_name, " +
                        "column_name, data_type, column_comment comments_ori from information_schema.columns where table_schema='" +
                        schemaName + "' and table_name='" + tableName + "'";
                return getTableConstruct(conn, sql);
            }

            @Override
            public List<Map<String, String>> getTableData(DataSourceQuery dataSourceQuery, List<String> columnNameList) throws SQLException {
                List<Integer> pageParam = getPageParam(dataSourceQuery);
                String sql = "select * from " + schemaName + "." + tableName + " limit " + pageParam.get(0) + ", " + pageParam.get(1);
                return getTableData(conn, sql, columnNameList);
            }

            @Override
            public List<Map<String, String>> getTableDataBySql(DataSourceQuery dataSourceQuery, String executeSql, List<String> columnNameList) throws SQLException {
                if (ObjectUtil.isNotEmpty(dataSourceQuery.getPageNo())) {
                    List<Integer> pageParam = getPageParam(dataSourceQuery);
                    executeSql = "select hole.* from (" + executeSql + ") hole" + " limit " + pageParam.get(0) + ", " + pageParam.get(1);
                }
                return getTableData(conn, executeSql, columnNameList);
            }

            private List<Integer> getPageParam(DataSourceQuery dataSourceQuery) {
                Integer pageNo = dataSourceQuery.getPageNo();
                Integer pageSize = dataSourceQuery.getPageSize();
                int firstIndex = (pageNo - 1) * pageSize + 1;
                int lastIndex = pageNo * pageSize;
                return Arrays.asList(firstIndex, lastIndex);
            }

            @Override
            public Integer getTotal() throws SQLException {
                return getTotal(conn, schemaName, tableName);
            }

        }

        class OracleInfo implements RDBSInterface {

            Integer dataSourceId;
            Connection conn;
            String schemaName;
            String tableName;

            public OracleInfo(Integer dataSourceId, Connection conn, String schemaName, String tableName) {
                this.dataSourceId = dataSourceId;
                this.conn = conn;
                this.schemaName = schemaName;
                this.tableName = tableName;
            }

            @Override
            public List<String> getSchemas() throws SQLException {
                return getAssignColumnValue(conn, "SELECT USERNAME FROM ALL_USERS", "USERNAME");
            }

            @Override
            public List<String> getTables() throws SQLException {
                return getAssignColumnValue(conn, "SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = '" + schemaName + "' ORDER BY TABLE_NAME", TABLE_NAME_STR);
            }

            @Override
            public List<DataSourceTableConstruct> getTableConstruct() throws SQLException {
                String sql = "SELECT '" + dataSourceId + "' DATA_SOURCE_ID, '" + schemaName + "' SCHEMA_NAME, '" + tableName + "' TABLE_NAME, " +
                        "A.COLUMN_NAME,A.DATA_TYPE,B.COMMENTS COMMENTS_ORI FROM ALL_TAB_COLS A JOIN (SELECT * FROM DBA_COL_COMMENTS WHERE TABLE_NAME = '" + tableName + "') B " +
                        "ON A.COLUMN_NAME = B.COLUMN_NAME WHERE A.TABLE_NAME = '" + tableName + "'";
                return getTableConstruct(conn, sql);
            }

            @Override
            public List<Map<String, String>> getTableData(DataSourceQuery dataSourceQuery, List<String> columnNameList) throws SQLException {
                String executeSql = "select * from " + schemaName + "." + tableName;
                return getTableDataByPage(dataSourceQuery, executeSql, columnNameList);
            }

            @Override
            public List<Map<String, String>> getTableDataBySql(DataSourceQuery dataSourceQuery, String executeSql, List<String> columnNameList) throws SQLException {
                if (dataSourceQuery.isByPage()) {
                    return getTableDataByPage(dataSourceQuery, executeSql, columnNameList);
                } else {
                    return getTableData(conn, executeSql, columnNameList);
                }
            }

            private List<Map<String, String>> getTableDataByPage(DataSourceQuery dataSourceQuery, String executeSql, List<String> columnNameList) throws SQLException {
                List<Integer> pageParam = getPageParam(dataSourceQuery);
                List<Map<String, String>> hashMapList = new ArrayList<>();

                String columnValue = "";
                try (PreparedStatement preparedStatement = conn.prepareStatement(executeSql)) {
                    // 如果要分页,则使用 Oracle 的 FETCH 分页
                    preparedStatement.setFetchSize(dataSourceQuery.getPageSize());
                    preparedStatement.setMaxRows(pageParam.get(1));

                    ResultSet res = preparedStatement.executeQuery();
                    ResultSetMetaData metaData = res.getMetaData();
                    // 获取列名称
                    int columnCount = metaData.getColumnCount();
                    for (int i = 0; i < columnCount; i++) {
                        columnNameList.add(metaData.getColumnName(i + 1));
                    }

                    int rowNum = 1;
                    Map<String, String> hashMap;
                    while (res.next() && rowNum > pageParam.get(0)) {
                        hashMap = new HashMap<>();
                        for (String columnNameStr : columnNameList) {
                            columnValue = res.getString(columnNameStr);
                            hashMap.put(columnNameStr, ObjectUtil.isEmpty(columnValue) ? "" : columnValue);
                        }
                        hashMapList.add(hashMap);
                        rowNum++;
                    }
                } finally {
                    conn.close();
                }
                return hashMapList;
            }


            private List<Integer> getPageParam(DataSourceQuery dataSourceQuery) {
                Integer pageNo = dataSourceQuery.getPageNo();
                Integer pageSize = dataSourceQuery.getPageSize();
                int from = (pageNo - 1) * pageSize;
                int to = pageNo * pageSize;
                return Arrays.asList(from, to);
            }

            @Override
            public Integer getTotal() throws SQLException {
                return getTotal(conn, schemaName, tableName);
            }

        }

        class SqlServerInfo implements RDBSInterface {

            Integer dataSourceId;
            Connection conn;
            String schemaName;
            String tableName;

            public SqlServerInfo(Integer dataSourceId, Connection conn, String schemaName, String tableName) {
                this.dataSourceId = dataSourceId;
                this.conn = conn;
                this.schemaName = schemaName;
                this.tableName = tableName;
            }

            @Override
            public List<String> getSchemas() throws SQLException {
                return getAssignColumnValue(conn, "select name from sys.schemas where schema_id > 4", "name");
            }

            @Override
            public List<String> getTables() throws SQLException {
                return getAssignColumnValue(conn, "select name from sys.tables where schema_id = SCHEMA_ID('" + schemaName + "') order by name", "name");
            }

            @Override
            public List<DataSourceTableConstruct> getTableConstruct() throws SQLException {
                String sql = "SELECT '" + dataSourceId + "' DATA_SOURCE_ID, '" + schemaName + "' SCHEMA_NAME, '" + tableName + "' TABLE_NAME, c.name AS column_name, " +
                        "t.Name AS data_type, " +
                        "CAST(ep.value AS varchar(max)) AS COMMENTS_ORI " +
                        "FROM sys.columns c " +
                        "JOIN sys.types t ON c.user_type_id = t.user_type_id " +
                        "LEFT JOIN sys.extended_properties ep ON ep.major_id = c.object_id " +
                        "AND ep.minor_id = c.column_id " +
                        "WHERE c.object_id = OBJECT_ID('" + schemaName + "." + tableName + "')";
                return getTableConstruct(conn, sql);
            }

            @Override
            public List<Map<String, String>> getTableData(DataSourceQuery dataSourceQuery, List<String> columnNameList) throws SQLException {
                List<Integer> pageParam = getPageParam(dataSourceQuery);
                String sql = "SELECT * from (SELECT row_number() over (order by id asc) as rownumber, * FROM " +
                        schemaName + "." + tableName + ") as t Where t.rownumber > " + pageParam.get(0) + " And t.rownumber <= " + pageParam.get(1);
                return getTableData(conn, sql, columnNameList);
            }

            @Override
            public List<Map<String, String>> getTableDataBySql(DataSourceQuery dataSourceQuery, String executeSql, List<String> columnNameList) throws SQLException {
                if (ObjectUtil.isNotEmpty(dataSourceQuery.getPageNo())) {
                    List<Integer> pageParam = getPageParam(dataSourceQuery);
                    executeSql = "SELECT * from (SELECT row_number() over (order by id asc) as rownumber,* ) as t " +
                            "Where t.rownumber > " + pageParam.get(0) + " And t.rownumber <= " + pageParam.get(1);
                }
                return getTableData(conn, executeSql, columnNameList);
            }

            private List<Integer> getPageParam(DataSourceQuery dataSourceQuery) {
                Integer pageNo = dataSourceQuery.getPageNo();
                Integer pageSize = dataSourceQuery.getPageSize();
                int firstIndex = (pageNo - 1) * pageSize;
                int lastIndex = pageNo * pageSize;
                return Arrays.asList(firstIndex, lastIndex);
            }

            @Override
            public Integer getTotal() throws SQLException {
                return getTotal(conn, schemaName, tableName);
            }

        }

    }


    /**
     * 获取ES connection
     *
     * @param dataSourceInfo dataSourceInfo
     * @return Connection
     */
    private Connection getEsConnection(DataSourceInfo dataSourceInfo) throws SQLException {
        String url = dataSourceInfo.getUrl();
        EsDataSource esDataSource = new EsDataSource();
        esDataSource.setUrl(url);
        return esDataSource.getConnection();
    }

    /**
     * 查看ES所有数据库.
     *
     * @param connection connection
     */
    private List<String> getEsSchemas(Connection connection) throws SQLException {
        ResultSet schemas = connection.getMetaData().getCatalogs();
        ArrayList<String> list = new ArrayList<>();
        while (schemas.next()) {
            list.add(String.valueOf(schemas.getObject(TABLE_CAT_STR)));
        }
        return list;
    }

    /**
     * 查看ES数据库所有表.
     *
     * @param connection connection
     */
    public static void getEsTables(Connection connection) throws Exception {
        ResultSet tables = connection.getMetaData().getTables(null, null, null, null);
        while (tables.next()) {
//            System.out.println(tables.getString(TABLE_NAME_STR));
            String TABLE_CAT = tables.getString(TABLE_CAT_STR);
            String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
            String TABLE_NAME = tables.getString(TABLE_NAME_STR);
            String TABLE_TYPE = tables.getString("TABLE_TYPE");
            String REMARKS = tables.getString("REMARKS");
            log.info("表类别:{}、表模式:{}、表名称:{}、表类型:{}、表描述:{}", TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TABLE_TYPE, REMARKS);
        }
    }

    /**
     * 查看ES表所有字段.
     *
     * @param connection connection
     */
    public static void getEsColumns(Connection connection) throws Exception {
        ResultSet tables = connection.getMetaData().getColumns(null, null, "test_index", null);
        while (tables.next()) {
            String TABLE_CAT = tables.getString("TABLE_CAT");
            String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
            String TABLE_NAME = tables.getString(TABLE_NAME_STR);
            String columnName = tables.getString("COLUMN_NAME");
            String typeName = tables.getString("TYPE_NAME");
            String columnSize = tables.getString("COLUMN_SIZE");
            String REMARKS = tables.getString("REMARKS");
            log.info("表类别:{}、表模式:{}、表名称:{}、字段名称:{}、字段类型:{}、字段大小:{}、字段描述:{}",
                    TABLE_CAT, TABLE_SCHEM, TABLE_NAME, columnName, typeName, columnSize, REMARKS);
        }
    }
}

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "DataSource对象", description = "数据源")
public class DataSourceQuery {

    @ApiModelProperty(value = "数据源名称")
    private String dataSourceName;

    @ApiModelProperty(value = "连接状态")
    private Integer dataSourceStatus;

    @ApiModelProperty(value = "数据源id")
    private Integer id;

    @ApiModelProperty(value = "schemaName")
    private String schemaName;

    @ApiModelProperty(value = "tableName")
    private String tableName;

}


import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("data_source")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DataSourceInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 数据源类型
     */
    private String type;

    /**
     * 数据源名称
     */
    private String dataSourceName;

    /**
     * url
     */
    private String url;

    /**
     * 数据库用户名
     */
    private String userName;

    /**
     * 数据库密码
     */
    private String password;

    /**
     * 数据源状态
     */
    private Integer dataSourceStatus;

    /**
     * 数据源描述
     */
    private String description;

    /**
     * 创建人
     */
    @TableField(value = "create_by", fill = FieldFill.INSERT)
    private String createBy;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新人
     */
    @TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
    private String updateBy;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 是否删除(0-未删, 1-已删)
     */
    private Integer isDeleted;

}

import lombok.Data;

@Data
public class DataSourceTableConstruct {

    private Integer dataSourceId;

    private String schemaName;

    private String tableName;

    private String columnName;

    private String dataType;

    private Integer commentId;

    private String comments;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己写的数据装载工具,平时工作中用于装载大文本文件到数据,也能直接装载excel,不过excel只能使用第一个sheet,且不支持合并的单元格。 jdk要求版本1.6及以上。 使用方法: java -jar lynload.jar,即可看到中文命令行参数(unix下需设置gbk字符集环境才能看中文,没gbk环境不能看中文,但不影响导入数据)。 特色: 1.同时支持多个数据. 2.支持任意字符串作为列分隔符,什么竖线逗号或者十六进制字符及任意组合字符串都行 3.支持任意字符串作为每行分隔符,例如\r,\r\n,或者十六进制字符或任意指定的字符串(例如可实现把整个日志文件装入数据的单个clob字段). 4.支持任意大小的文本,装入几十GB都试过没问题。 5.明确到每一行的错误信息.(装载时会生成一个.err文件,里面有装载出错的每一行错误原因(少字段还是格式不符合要求等等)。 6.可以作为使用在jsp等后台直接调用,将整个jar作为,使用 OraLoad类的int ProcLoad(Connection in_conn, String args[])函数就可以实现类似功能了。例如 new OraLoad.ProcLoad( null, "-u test -p testpwd -i 127.0.0.1 -s ora10 -f test.txt -t tmp_test -c , -e gbk"); 具体参数如下: Usage: -db database type 1或者oracle, oracle数据,不区分大小写,默认1,需java1.5及以上版本 2或者sqlserver, mssqlserver数据,不区分大小写,需java1.6及以上版本 3或者mysql, mysql数据,不区分大小写,需java1.5及以上版本 4或者sybase, sybase数据,不区分大小写(暂未测试) -url 数据连接的url.默认空字符串 如果有url参数,则-i-p-s-dn参数全无效。 如果没有url参数,则必须提供-i-p-s参数,程序里面好拼凑url 例如: jdbc:sqlserver://localhost:1433; DatabaseName=sample -i ip address:port ip地址和端口,中间用冒号隔开 url不为空则此参数无效 如果不带端口,系统根据数据类型采用默认端口 oracle数据: 默认为1521端口 sqlserver数据: 默认为1433端口 mysql数据: 默认为3306端口 sybase数据: 默认为5000端口 例如: 127.0.0.1:1521 例如: 192.168.0.1 -s serviceid or database name oracle服务名或者其他数据数据名 url不为空则此参数无效 -u username 用户名,必输 -p Password 登录密码,必输 -t TableName 要插入的名,可以带用户,必输 例如: scott.emp 或者 emp都行 -f FileName 来源的文件名,必输 -ff FileName format 来源的文件名类型:txt,文本文件;xls:Excel2003或2007文件),默认是txt 对于Excel文件的导入,仅导入第一个sheet的内容,其他sheet的内容忽略 -e Encoding 文件的编码,默认gbk -c Cut split string 分隔字符串,可以是多个字符组合,默认竖线 -r commit rows 每插入多少行提交一次,-1示不提交,默认-1,如果设置了ac参数为1,则此参数无效。 注意:如果设置了此参数,每到此行数会执行一次commit,如果是外部调用此过程要注意所有数据会被提交 -ac all commit 0.插入成功的都提交,失败的登记文本,默认0; 1.全部插入成功后才提交(有失败则回滚并结束) -a column flag 第一行的类型 0.没有列名行,默认0。此值为0则it参数不生效。 1.第一行是逗号分隔的包括列类型的列信息,例如NAME VARCHAR2(30),注意,区分大小写 2.第一行是列分隔符分隔的列名(不包括列字段信息),注意,列名区分大小写 -it insert type 1.导入的列需要跟数据列名个数及名称完全相同,顺序无所谓,默认1 2.仅导入文件字段名与数据字段名完全相同的数据,顺序无所谓 3.第一行是列信息,按此列信息创建新导入数据(暂不用) 4.忽略第一行,无论第一行是什么数据全部忽略,从第二行按列字段顺序匹配装载 必须在-a 参数 > 0时才生效,否则无意义 -d date formate 日期格式,参考java的SimpleDateFormate类参数,默认自动识别 可识别格式:yyyyMMdd, yyyy-MM-dd, yyyy/MM/dd, yyyyMMdd:HHmmss(等于oracle的yyyymmdd:hh24miss) yyyy年,MM月,dd日,HH 24小时制时,hh 12小时制时,mm 分, ss秒 -l line end string 每条记录结尾的分隔符,默认是 \r\n回车换行符,支持前面-c参数说的转义符 oracle例子: 登陆scott用户,裝载a.txt的逗号分隔的gbk编码文本内容到scott.emp java -jar xxx.jar -i 127.0.0.1 -p 1521 -s ora10 -u system -p manager -t scott.emp -f a.txt -c , sqlserver例子: 登陆11.8.126.181数据的test用户,裝载f: est.xlsx的excel数据到tmp_i java -jar -db sqlserver -i 11.8.126.181 -s abcd -u test -w test -f f: est.xlsx -t tmp_i -ff xls mysql例子: 登陆本机数据的root用户,裝载a.txt的文本数据到tmp_i,不提交 java -jar xxx.jar -db mysql -i 127.0.0.1 -s gjjgj -u root -w root -f a.txt -t tmp_i

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值