美文网首页
对比多个数据源的数据库结构差异

对比多个数据源的数据库结构差异

作者: RobertCrazying | 来源:发表于2017-12-05 09:43 被阅读83次

工作中经常会出现不同的环境之间数据库的差异,然而这些差异并不容易通过肉眼察觉,我们可以通过程序来帮助我们对比。如下举例MySQL数据库下测试环境和集成环境的差异

主要通过INFORMATION_SCHEMA(DSL)来查询数据库的结构

关键代码

  1. 获取数据库的所有表
  private static List<String> getTables(JdbcTemplate jdbcTemplate){
      String getTablesSql = "SELECT table_name as tableName FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = DATABASE()";
      List<Map<String, Object>> data = jdbcTemplate.queryForList(getTablesSql);
      List<String> tables = new ArrayList<>();
      for (Map<String, Object> map : data){
          tables.add((String) map.get("tableName"));
      }
      return tables;
  }
  1. 根据表名获取字段信息
    private static List<String> getColumns(JdbcTemplate jdbcTemplate, String tableName){
        String getColumnSql = "select column_name columnName from information_schema.columns where table_schema =DATABASE() and table_name = '"+tableName+"'";
        List<Map<String, Object>> data = jdbcTemplate.queryForList(getColumnSql);
        List<String> columns = new ArrayList<>();
        for (Map<String, Object> map : data){
            columns.add((String) map.get("columnName"));
        }
        return columns;
    }
  1. 根据需要传入jdbcTemplate数据源即可完成需要的对比

相关文章

网友评论

      本文标题:对比多个数据源的数据库结构差异

      本文链接:https://www.haomeiwen.com/subject/yrjqixtx.html