基于Java编写一个数据库比较工具类
更新时间:2023年07月21日 09:39:47 作者:zyqok
这篇文章主要为大家详细介绍了如何基于Java编写一个数据库比较工具类,其中比较结果会以现数据库的视角说明,感兴趣的小伙伴可以了解一下
数据库比较工具类
温馨提示
(1)如果想知道现数据库和原数据库有哪些区别,则原数据库配置 old,现数据库配置 now;
(2)比较结果以现数据库的视角说明,如:提示新增 xxx 表,则表示现数据库有,原数据库没有,反之;
(3)整个比较过程中,并不会对两个数据库造成任何影响,可放心使用;
(4)配置好两个数据库后,直接运行本类中的 main() 方法即可,最终比较结果将以 json 的格式直接输出在控制台。
完整代码
package sql; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.Data; import java.sql.*; import java.util.*; /** * 数据库比较工具类 * 温馨提示: * (1)如果想知道现数据库和原数据库有哪些区别,则原数据库配置 old,现数据库配置 now; * (2)比较结果以现数据库的视角说明,如:提示新增 xxx 表,则表示现数据库有,原数据库没有,反之; * (3)整个比较过程中,并不会对两个数据库造成任何影响,可放心使用; * (4)配置好两个数据库后,直接运行本类中的 main() 方法即可,最终比较结果将以 json 的格式直接输出在控制台。 * * @author Yuanqiang.Zhang * @since 2023/7/3 */ @Data public class CompareDataBaseUtils { public static Map<String, String> old; public static Map<String, String> now; static { // 原数据库 old = new HashMap<>(); old.put(Constant.ip, "127.0.0.1"); old.put(Constant.port, "3306"); old.put(Constant.dataBaseName, "zyq_test"); old.put(Constant.userName, "root"); old.put(Constant.password, "root123"); old.put(Constant.description, "原数据库"); // 现数据库 now = new HashMap<>(); now.put(Constant.ip, "127.0.0.1"); now.put(Constant.port, "3306"); now.put(Constant.dataBaseName, "zyq_test2"); now.put(Constant.userName, "root"); now.put(Constant.password, "root123"); now.put(Constant.description, "现数据库"); } /** * 【主方法】配置好数据源后直接执行即可 */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); print("比较中,请等待..."); JSONObject compareResult = compareDataBase(); if (Objects.nonNull(compareResult)) { print(String.format("比较完成,累计耗时%s毫秒", (System.currentTimeMillis() - startTime))); print("最终比较结果如下:"); print(compareResult.toString()); } } public static JSONObject compareDataBase() { Connection oldConn = getConnection(old); Connection nowConn = getConnection(now); if (Objects.isNull(oldConn) || Objects.isNull(nowConn)) { return null; } JSONObject oldTables = getCompareTable(old, oldConn); JSONObject nowTables = getCompareTable(now, nowConn); Set<String> allTableNames = new HashSet<>(); allTableNames.addAll(oldTables.keySet()); allTableNames.addAll(nowTables.keySet()); List<String> nowMoreTableNames = new ArrayList<>(); List<String> nowLessTableNames = new ArrayList<>(); List<String> commonTableNames = new ArrayList<>(); for (String tableName : allTableNames) { boolean existOld = oldTables.containsKey(tableName); boolean existNow = nowTables.containsKey(tableName); if (existNow && existOld) { commonTableNames.add(tableName); } else { if (existNow) { nowMoreTableNames.add(tableName); } else { nowLessTableNames.add(tableName); } } } // 表相关修改 JSONObject compare = new JSONObject(new LinkedHashMap<>()); compare.put(Constant.more_tables, nowMoreTableNames); compare.put(Constant.less_tables, nowLessTableNames); // 获取相同两张表的比较结果 JSONObject changeTables = new JSONObject(new LinkedHashMap<>()); for (String commonTableName : commonTableNames) { JSONObject oldInfo = oldTables.getJSONObject(commonTableName); JSONObject nowInfo = nowTables.getJSONObject(commonTableName); JSONObject compareInfo = getCompareInfo(oldInfo, nowInfo); Boolean changed = compareInfo.getBoolean(Constant.changed); if (changed) { compareInfo.remove(Constant.changed); changeTables.put(commonTableName, compareInfo); } } compare.put(Constant.update_tables, changeTables); return compare; } /** * 获取老表和新表之间的比较结果 */ public static JSONObject getCompareInfo(JSONObject old, JSONObject now) { // 表信息比较 JSONObject oldBasic = old.getJSONObject(Constant.basics); JSONObject nowBasic = now.getJSONObject(Constant.basics); JSONArray basicCompareResult = compareSameFieldsObject(oldBasic, nowBasic); // 表字段比较 JSONObject oldFields = old.getJSONObject(Constant.fields); JSONObject nowFields = now.getJSONObject(Constant.fields); Set<String> allFields = new HashSet<>(); allFields.addAll(oldFields.keySet()); allFields.addAll(nowFields.keySet()); JSONArray nowMoreFields = new JSONArray(); JSONArray nowLessFields = new JSONArray(); JSONArray commonFields = new JSONArray(); for (String field : allFields) { boolean existOld = oldFields.containsKey(field); boolean existNow = nowFields.containsKey(field); if (existOld && existNow) { commonFields.add(field); } else { if (existNow) { nowMoreFields.add(field); } else { nowLessFields.add(field); } } } // 相同字段更新比较 JSONObject fieldsCompareResult = new JSONObject(new LinkedHashMap<>()); for (Object field : commonFields) { String fileName = field.toString(); JSONObject oldField = oldFields.getJSONObject(fileName); JSONObject nowField = nowFields.getJSONObject(fileName); JSONArray commonResultArray = compareSameFieldsObject(oldField, nowField); if (!commonResultArray.isEmpty()) { fieldsCompareResult.put(fileName, commonResultArray); } } // 字段变更信息 boolean fieldChanged = !nowMoreFields.isEmpty() || !nowLessFields.isEmpty() || !fieldsCompareResult.isEmpty(); JSONObject fieldsResult = new JSONObject(new LinkedHashMap<>()); if (!nowMoreFields.isEmpty()) { fieldsResult.put(Constant.more_fields, nowMoreFields); } if (!nowLessFields.isEmpty()) { fieldsResult.put(Constant.less_fields, nowLessFields); } if (!fieldsCompareResult.isEmpty()) { fieldsResult.put(Constant.update_fields, fieldsCompareResult); } // 基础变更信息 boolean basicChanged = !basicCompareResult.isEmpty(); // 总变更信息 boolean changed = fieldChanged || basicChanged; // 字段比较结果 JSONObject compareResult = new JSONObject(new LinkedHashMap<>()); if (basicChanged) { compareResult.put(Constant.table_change_info, basicCompareResult); } if (fieldChanged) { compareResult.put(Constant.field_change_info, fieldsResult); } compareResult.put(Constant.changed, changed); return compareResult; } /** * 比较新老两个相同属性的对象 * * @param oldObj 老对象 * @param nowObj 新对象 * @return 比较结果 */ private static JSONArray compareSameFieldsObject(JSONObject oldObj, JSONObject nowObj) { Set<String> keys = oldObj.keySet(); JSONArray array = new JSONArray(); String oldDesc = old.get(Constant.description); String nowDesc = now.get(Constant.description); for (String key : keys) { String oldValue = oldObj.getString(key); String nowValue = nowObj.getString(key); boolean same; if (Objects.isNull(oldValue)) { same = Objects.isNull(nowValue); } else { same = oldValue.equals(nowValue); } if (!same) { JSONObject diff = new JSONObject(new LinkedHashMap<>()); diff.put(Constant.change_dimension, key); diff.put(oldDesc, oldValue); diff.put(nowDesc, nowValue); array.add(diff); } } return array; } /** * 获取所有表字段属性 * * @param dataSource 数据源 * @return 表信息 */ public static JSONObject getCompareTable(Map<String, String> dataSource, Connection conn) { // 获取数据库所有表基础信息 JSONObject basics = getAllTableList(conn); String dbName = dataSource.get(Constant.dataBaseName); String desc = dataSource.get(Constant.description); int tableCount = 0; int tableTotal = basics.size(); // 获取每张表的每个字段详情 JSONObject tables = new JSONObject(new LinkedHashMap<>()); for (String tableName : basics.keySet()) { tableCount++; JSONObject table = new JSONObject(new LinkedHashMap<>()); table.put(Constant.basics, basics.getJSONObject(tableName)); JSONObject fields = getTableFields(conn, dbName, tableName); table.put(Constant.fields, fields); String info = String.format("[%s][%s/%s][%s]解析完成,共%s个字段。", desc, tableCount, tableTotal, tableName, fields.size()); print(info); tables.put(tableName, table); } return tables; } /** * 获取数据源属性 * * @param conn 数据库连接 * @param dataBaseName 数据库名称 * @param tableName 表名称 * @return 该表的所有字段信息 */ private static JSONObject getTableFields(Connection conn, String dataBaseName, String tableName) { String sql = "SELECT * FROM information_schema.columns WHERE table_schema = '" + dataBaseName + "' AND table_name = '" + tableName + "'"; JSONObject columns = new JSONObject(new LinkedHashMap<>()); try { Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { JSONObject column = new JSONObject(new LinkedHashMap<>()); String name = rs.getString("COLUMN_NAME"); column.put(Constant.column_name, name); column.put(Constant.column_comment, rs.getString("COLUMN_COMMENT")); column.put(Constant.column_is_nullable, rs.getString("IS_NULLABLE")); column.put(Constant.column_type, rs.getString("COLUMN_TYPE")); column.put(Constant.column_collation, rs.getString("COLLATION_NAME")); column.put(Constant.column_key, rs.getString("COLUMN_KEY")); column.put(Constant.column_extra, rs.getString("EXTRA")); columns.put(name, column); } } catch (SQLException e) { e.printStackTrace(); } return columns; } /** * 获取数据库链接 * * @param map 数据源 * @return Connection */ private static Connection getConnection(Map<String, String> map) { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = String.format("jdbc:mysql://%s:%s/%s", map.get(Constant.ip), map.get(Constant.port), map.get(Constant.dataBaseName)); return DriverManager.getConnection(url, map.get(Constant.userName), map.get(Constant.password)); } catch (Exception e) { System.err.printf("[%s]连接失败,请检查配置是否正确!%n", map.get(Constant.description)); return null; } } /** * 获取所有数据表名 * * @param conn 数据库链接信息 * @return Map<表名, 备注> */ private static JSONObject getAllTableList(Connection conn) { String sql = "SHOW TABLE status"; JSONObject basics = new JSONObject(new LinkedHashMap<>()); try { Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { JSONObject basic = new JSONObject(new LinkedHashMap<>()); String name = rs.getString("Name"); basic.put(Constant.table_name, name); basic.put(Constant.table_engine, rs.getString("Engine")); basic.put(Constant.table_comment, rs.getString("Comment")); basic.put(Constant.table_collation, rs.getString("Collation")); basics.put(name, basic); } } catch (SQLException e) { e.printStackTrace(); } return basics; } private static JSONObject getTableIndexes(Connection conn, String tableName) { String sql = "SHOW INDEX FROM " + tableName; JSONObject columns = new JSONObject(new LinkedHashMap<>()); try { Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { JSONObject column = new JSONObject(new LinkedHashMap<>()); String name = rs.getString("COLUMN_NAME"); column.put(Constant.column_name, name); column.put(Constant.column_comment, rs.getString("COLUMN_COMMENT")); column.put(Constant.column_is_nullable, rs.getString("IS_NULLABLE")); column.put(Constant.column_type, rs.getString("COLUMN_TYPE")); column.put(Constant.column_collation, rs.getString("COLLATION_NAME")); column.put(Constant.column_key, rs.getString("COLUMN_KEY")); column.put(Constant.column_extra, rs.getString("EXTRA")); columns.put(name, column); } } catch (SQLException e) { e.printStackTrace(); } return columns; } /** * 输出打印 * * @param msg 打印信息 */ private static void print(String msg) { System.out.println(msg); } /** * 工具类中含的常量(为方便阅读,本工具类中与输入无相关的常量字段均采用下划线小写) */ private static class Constant { // 数据源配置常量键 public static final String ip = "ip"; public static final String port = "port"; public static final String dataBaseName = "dataBaseName"; public static final String userName = "userName"; public static final String password = "password"; public static final String description = "description"; // 代码逻辑常量 public static final String basics = "basics"; public static final String fields = "fields"; public static final String changed = "changed"; // 比较结果键名(其他) public static final String table_change_info = "表信息变更"; public static final String field_change_info = "表字段变更"; public static final String more_tables = "新增的表"; public static final String less_tables = "减少的表"; public static final String update_tables = "变更的表"; public static final String more_fields = "新增的字段"; public static final String less_fields = "减少的字段"; public static final String update_fields = "变更的字段"; public static final String change_dimension = "变更维度"; // 比较结果键名(表信息) public static final String table_name = "名称"; public static final String table_engine = "引擎"; public static final String table_comment = "备注"; public static final String table_collation = "字符集"; // 比较结果键名(字段信息) public static final String column_name = "名称"; public static final String column_comment = "备注"; public static final String column_is_nullable = "是否可空"; public static final String column_type = "数据类型"; public static final String column_collation = "字符集"; public static final String column_key = "键类型"; public static final String column_extra = "额外信息"; } }
以上就是基于Java编写一个数据库比较工具类的详细内容,更多关于Java数据库比较工具类的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot使用MockMvc进行单元测试的实例代码
在Spring Boot应用程序中,使用MockMvc进行单元测试是一种有效的方式,可以验证控制器的行为和API的正确性,在这篇博客中,我们将介绍如何使用MockMvc对用户控制器进行测试,感兴趣的朋友可以参考下2024-01-01IDEA配置tomcat的方法、IDEA配置tomcat运行web项目详解
这篇文章主要介绍了IDEA配置tomcat的方法、IDEA配置tomcat运行web项目详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07SpringBoot+WebSocket+Netty实现消息推送的示例代码
这篇文章主要介绍了SpringBoot+WebSocket+Netty实现消息推送的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04
最新评论