package org.jeecg.modules.arj.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import java.sql.*;
|
import java.util.*;
|
|
|
public class MetaMysql {
|
|
private final static String DATABASE_NAME = "generatesql";
|
|
private DatabaseMetaData dbMetaData = null;
|
private Connection con = null;
|
|
public MetaMysql() {
|
this.getDatabaseMetaData();
|
}
|
|
private void getDatabaseMetaData() {
|
try {
|
if (dbMetaData == null) {
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
String url = "jdbc:mysql://127.0.0.1:3306/jeecg-boot?charset=utf8mb4&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai" ;
|
String user = "root";
|
String password = "123456";
|
con = DriverManager.getConnection(url, user, password);
|
dbMetaData = con.getMetaData();
|
}
|
} catch (ClassNotFoundException e) {
|
e.printStackTrace();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 获得数据库的一些相关信息
|
*/
|
public void getDataBaseInformations() {
|
try {
|
System.out.println("数据库已知的用户: " + dbMetaData.getUserName());
|
System.out.println("数据库的系统函数的逗号分隔列表: " + dbMetaData.getSystemFunctions());
|
System.out.println("数据库的时间和日期函数的逗号分隔列表: " + dbMetaData.getTimeDateFunctions());
|
System.out.println("数据库的字符串函数的逗号分隔列表: " + dbMetaData.getStringFunctions());
|
System.out.println("数据库供应商用于 'schema' 的首选术语: " + dbMetaData.getSchemaTerm());
|
System.out.println("数据库URL: " + dbMetaData.getURL());
|
System.out.println("是否允许只读:" + dbMetaData.isReadOnly());
|
System.out.println("数据库的产品名称:" + dbMetaData.getDatabaseProductName());
|
System.out.println("数据库的版本:" + dbMetaData.getDatabaseProductVersion());
|
System.out.println("驱动程序的名称:" + dbMetaData.getDriverName());
|
System.out.println("驱动程序的版本:" + dbMetaData.getDriverVersion());
|
|
System.out.println();
|
System.out.println("数据库中使用的表类型");
|
ResultSet rs = dbMetaData.getTableTypes();
|
while (rs.next()) {
|
System.out.println(rs.getString(1));
|
}
|
rs.close();
|
System.out.println();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 获得该用户下面的所有表
|
*/
|
public void getAllTableList(String schemaName) {
|
try {
|
// table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE",
|
// "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
|
String[] types = {"TABLE"};
|
ResultSet rs = dbMetaData.getTables(null, schemaName, "%" , types);
|
while (rs.next()) {
|
String tableName = rs.getString("TABLE_NAME"); // 表名
|
String tableType = rs.getString("TABLE_TYPE"); // 表类型
|
String remarks = rs.getString("REMARKS"); // 表备注
|
System.out.println(tableName + "-" + tableType + "-" + remarks);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
/**
|
* 获得数据库中所有方案名称
|
*/
|
public void getAllSchemas() {
|
try {
|
ResultSet rs = dbMetaData.getSchemas();
|
while (rs.next()) {
|
String tableSchem = rs.getString("TABLE_SCHEM");
|
System.out.println("schems===="+tableSchem);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 根据指定用户名与表名获取其中列字段名
|
*
|
* @param schemaName
|
* @param tableName
|
* @return
|
*/
|
public Map<String, Field> getColumnNameBySchemaNameAndTableName(String schemaName, String tableName) {
|
Map<String, Field> columns = null;
|
try {
|
columns = new HashMap<>();
|
ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
|
while (rs.next()) {
|
String columnName = rs.getString("COLUMN_NAME");// 列名
|
String dataTypeName = rs.getString("TYPE_NAME");// java.sql.Types类型名称
|
int columnSize = rs.getInt("COLUMN_SIZE");// 列大小
|
Field field = new Field(columnName, dataTypeName, columnSize);
|
columns.put(columnName, field);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
return columns;
|
}
|
|
|
|
|
/**
|
* 获得表或视图中的所有列信息
|
*/
|
public List<LinkedHashMap<String,String>> getTableColumns(String schemaName, String tableName) {
|
List<LinkedHashMap<String,String>> list = new ArrayList<>();
|
try {
|
|
ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
|
|
while (rs.next()) {
|
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
String tableCat = rs.getString("TABLE_CAT");// 表目录(可能为空)
|
String tableSchemaName = rs.getString("TABLE_SCHEM");// 表的架构(可能为空)
|
String tableName_ = rs.getString("TABLE_NAME");// 表名
|
String columnName = rs.getString("COLUMN_NAME");// 列名
|
int dataType = rs.getInt("DATA_TYPE"); // 对应的java.sql.Types类型
|
String dataTypeName = rs.getString("TYPE_NAME");// java.sql.Types类型
|
// 名称
|
int columnSize = rs.getInt("COLUMN_SIZE");// 列大小
|
int decimalDigits = rs.getInt("DECIMAL_DIGITS");// 小数位数
|
int numPrecRadix = rs.getInt("NUM_PREC_RADIX");// 基数(通常是10或2)
|
int nullAble = rs.getInt("NULLABLE");// 是否允许为null
|
String remarks = rs.getString("REMARKS");// 列描述
|
String columnDef = rs.getString("COLUMN_DEF");// 默认值
|
int sqlDataType = rs.getInt("SQL_DATA_TYPE");// sql数据类型
|
int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB"); // SQL日期时间分?
|
int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); // char类型的列中的最大字节数
|
int ordinalPosition = rs.getInt("ORDINAL_POSITION"); // 表中列的索引(从1开始)
|
|
/**
|
* ISO规则用来确定某一列的为空性。 是---如果该参数可以包括空值; 无---如果参数不能包含空值
|
* 空字符串---如果参数为空性是未知的
|
*/
|
String isNullAble = rs.getString("IS_NULLABLE");
|
|
/**
|
* 指示此列是否是自动递增 是---如果该列是自动递增 无---如果不是自动递增列 空字串---如果不能确定它是否
|
* 列是自动递增的参数是未知
|
*/
|
String isAutoincrement = rs.getString("IS_AUTOINCREMENT");
|
if( columnName.equalsIgnoreCase("id")|| columnName.equalsIgnoreCase("head_id")) continue;
|
map.put("prop" , columnName);
|
map.put("label" , remarks);
|
map.put("editable" , "false");
|
map.put("type" , "text");
|
list.add(map);
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
|
return list ;
|
}
|
|
|
|
public void colseCon() {
|
try {
|
if (con != null) {
|
con.close();
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void main(String[] args) {
|
MetaMysql metaData = new MetaMysql();
|
metaData.getDataBaseInformations();
|
String table_name="c13_ctsy,c_sp,c17_fzjt,c2_check,c3_week,c4_clean,c_base,c_tieqk,c_wg,g_gyj,jg10_xk,jg11_qj," +
|
"jg9_cpzl,jg9_scjl,sh14_cc_fp,sh14_gjj_fp,sh14_jg_fp,sh14_yt_fp,sh14_zj,sh14_zj_fp,sm12_sm,zj5_base," +
|
"zj5_mfj,zj5_sp,zj5_wg,zj6_check,zj7_week,zj8_clean";
|
table_name = "pplan,pplan2" ;
|
String[] tableList = table_name.split(",");
|
for(String n:tableList) {
|
List<LinkedHashMap<String, String>> list = metaData.getTableColumns("test" , n);
|
String headHeader = "const "+n+"Header = reactive([" + JSONObject.toJSONString(list) + "]);";
|
System.out.println(headHeader);
|
}
|
|
}
|
|
}
|