SchemaSpy:一款免费开源的数据库文档工具

B站影视 欧美电影 2025-10-05 00:03 1

摘要:数据库文档对于系统开发至关重要,它可以帮助 DBA、开发和测试人员、数据分析师和数据科学家、技术顾问和维护人员以及业务人员等理解、开发、使用和维护数据库。

数据库文档对于系统开发至关重要,它可以帮助 DBA、开发和测试人员、数据分析师和数据科学家、技术顾问和维护人员以及业务人员等理解、开发、使用和维护数据库。

今天给大家介绍一个用于生成数据库文档的免费开源软件:SchemaSpy。

如果觉得文章有用,欢迎评论、点赞、推荐

SchemaSpy 简介

SchemaSpy 是一个基于 Java(java 8 以上版本)的数据库文档工具,用于分析数据库模式中的表结构等元数据并生成基于浏览器的可视化文档。用户可以通过 HTML 链接和实体关系图(ERD)查看数据库中的子表和父表之间的层级关系。

SchemaSpy 是一个免费的软件,基于 LGPL V3 协议发布,源代码位于 GitHub:

SchemaSpy 的主要功能包括:

支持绝大多数 JDBC 兼容的数据库 ;生成 ER 图,支持保存为图片;显示字段之间的外键关联和级联操作;显示存储过程和函数源代码;显示视图及定义;在注释中支持 Markdown 渲染;支持导出为 Excel、CSV 以及 PDF 文件;支持额外的元数据,具体参考 SchemaMeta;显示数据库中的异常情况,例如没有任何索引的表。

SchemaSpy 提供了一个示例数据库文档,可以让我们直观地感受一下最终的文档结果。例如表的结构:

整体的 ERD:

SchemaSpy 安装

SchemaSpy 基于 Java 开发,我们需要先安装一个 Java 运行环境。安装完成后查看一下版本:

PS C:\Users\tony> java -versionjava version "1.8.0_281"Java(TM) SE Runtime Environment (build 1.8.0_281-b09)Java HotSpot(TM) Client VM (build 25.281-b09, mixed mode)PS C:\Users\tony>

SchemaSpy 本身的安装非常简单,它就是一个可执行的 jar 文件,我们可以直接点击下载最新的文件 schemaspy-vX.Y.Z.jar。

schemaSpy 使用

SchemaSpy 是一个命令行工具,输入以下命令查看帮助信息:

C:\Users\tony>java -jar schemaspy-6.1.0.jar -help|____/ \___|_| |_|\___|_| |_| |_|\__,_|____/| .__/ \__, |6.1.0SchemaSpy generates an HTML representation of a database schema's relationships.SchemaSpy comes with ABSOLUTELY NO WARRANTY.SchemaSpy is free software and can be redistributed under the conditions of LGPL version 3 or later.http://www.gnu.org/licenses/INFO - Starting Main v6.1.0 on LAPTOP-DGRB6HD9 with PID 3228 (C:\Users\tony\schemaspy-6.1.0.jar started by tony in C:\Users\tony)INFO - The following proFiles are active: defaultINFO - Started Main in 1.802 seconds (JVM running for 2.282)INFO - Usage: java -jar /C:/Users/tony/schemaspy-6.1.0.jar [options]Options:?, -?, /?, -h, help, -help, --helpshow general usage and available options-dbHelp, -dbhelp, --dbHelp, --dbhelpShow built-in database types and their required connection parameters-l, --licensePrint license, it will first print GPL and then LGPL (LGPL is addition to GPL)-cat, --catalog, catalog, schemaspy.cat, schemaspy.catalogcatalog-db, -database-name, schemaspy.db, schemaspy.database-nameName of database to connect to-t, --database-type, database-type, schemaspy.t, schemaspy.database-typetype of databaseDefault: ora-debug, --debug, debug, schemaspy.debugEnable debug loggingDefault: false-degree, schemaspy.degreeLimit the degree of separation, allowed values 1 or 2, defaults to 2Default: 2-dp, --driverPath, driverPath, schemaspy.dp, schemaspy.driverPathpath to look for JDBC drivers overrides driverPath in [databaseType].properties, supports directory, will adddirectory and recurse to add all content. Supports multiple paths using os dependent pathSeparator.-nohtml, --no-html, nohtml, schemaspy.nohtmlSkip html generationDefault: false-o, --outputDirectory, outputDirectory, schemaspy.o, schemaspy.outputDirectorydirectory to place the generated output in-port, --port, port, schemaspy.port-s, --schema, schema, schemaspy.s, schemaspy.schemaname of the schema to analyze (defaults to the specified user)-sso, --single-sign-on, schemaspy.sso, schemaspy.single-sign-onRemove requirement for userDefault: false-u, --user, user, schemaspy.u, schemaspy.userconnect to the database with this user id-vizjs, schemaspy.vizjsUse viz.js instead of graphviz graphvizDot executableDefault: falseGo to http://schemaspy.org for a complete list/description of additional parameters.Sample usage using the default database type (implied -t ora):java -jar schemaSpy.jar -db mydb -s myschema -u devuser -p password -o output

运行 SchemaSpy 时可以通过命令行指定参数,也可以使用预定义的配置文件。SchemaSpy 默认使用 ./schemaspy.properties 作为配置文件,我们也可以使用命令行参数指定配置文件:

java -jar schemaspy.jar -configFile path/to/config.file

以下是一个配置文件的示例:

# 指定数据库类型. 使用 -dbhelp 参数查看详细信息.schemaspy.t=mssql# 可选参数,指定替代的 jdbc 驱动.schemaspy.dp=path/to/drivers# 数据库连接属性:主机、端口、数据库名、用户、密码schemaspy.host=serverschemaspy.port=port_numschemaspy.db=db_nameschemaspy.u=database_userschemaspy.p=database_password# 输出结果的存储目录schemaspy.o=path/to/output# 需要创建 ERD 的数据库模式 Schemaschemaspy.s=dbo

使用命令行参数运行的 SchemaSpy 命令如下:

java -jar schemaspy-vX.Y.Z.jar -t dbType -dp path/to/driver -db dbName -host server -port port_num [-s schema] -u user [-p password] -o outputDir -vizjs

命令行的参数和上面的配置文件一致,但是优先级更高。例如,以下是一个连接 PostgreSQL 数据库创建文档的命令:

C:\Users\tony>java -jar schemaspy-6.1.0.jar -t pgsql11 -dp D:/Applications/DBeaver/drivers/postgresql-42.2.5.jar -host 127.0.0.1 -port 5432 -db hrdb -u postgres -p Pg@2020 -o ./pg -vizjs|____/ \___|_| |_|\___|_| |_| |_|\__,_|____/| .__/ \__, |6.1.0SchemaSpy generates an HTML representation of a database schema's relationships.SchemaSpy comes with ABSOLUTELY NO WARRANTY.SchemaSpy is free software and can be redistributed under the conditions of LGPL version 3 or later.http://www.gnu.org/licenses/INFO - Starting Main v6.1.0 on LAPTOP-DGRB6HD9 with PID 13932 (C:\Users\tony\schemaspy-6.1.0.jar started by tony in C:\Users\tony)INFO - The following profiles are active: defaultINFO - Started Main in 1.35 seconds (JVM running for 1.944)INFO - Starting schema analysisINFO - Connected to PostgreSQL - 13.1INFO - Gathering schema detailsGathering schema details0sec)Connecting relationships1sec)Writing/graphing summary.INFO - Gathered schema details in 1 secondsINFO - Writing/graphing summary........(38sec)Writing/diagramming detailsINFO - Completed summary in 38 secondsINFO - Writing/diagramming details.................Java HotSpot(TM) Client VM warning: CodeCache is full. Compiler has been disabled.Java HotSpot(TM) Client VM warning: Try increasing the code cache size using -XX:ReservedCodeCacheSize=CodeCache: size=32768Kb used=31969Kb max_used=31969Kb free=798Kbbounds [0x02230000, 0x04230000, 0x04230000]total_blobs=6108 nmethods=5798 adapters=237compilation: disabled (not enough contiguous free space left9sec)Wrote relationship details of 35 tables/views to directory '.\pg' in 52 seconds.View the results by opening .\pg\index.htmlINFO - Wrote table details in 9 secondsINFO - Wrote relationship details of 35 tables/views to directory '.\pg' in 52 seconds.INFO - View the results by opening .\pg\index.html

运行成功后,打开输出目录中的 index.html 文件查看数据库文档。

关于命令行参数的详细介绍,可以参考官方文档。

总结

本文介绍了开源数据库文档 SchemaSpy 的安装和使用。SchemaSpy 的高级功能包括设置连接属性、自定义数据库类型、添加额外的注释、单点登录、自定义查询语句等,大家可以自行探讨。

来源:总有无能为力的不愉

相关推荐