日常的开发工作中,经常会和数据库打交道,在某些场景可能会需要数据库表结构的文档,今天分享个简洁好用的数据库表结构文档生成工具screw。
screw目前支持MySQL、Oracle、SqlServer、MariaDB、PostgreSQL等数据库,生成文档目前支持html、word、markdown文档格式。
github: https://github.com/pingfangushi/screw
gitee: https://gitee.com/leshalv/screw
下面以MySQL数据库为例来简单介绍下。
新建测试库test及表employees、user。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| CREATE DATABASE test; CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名', `age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄', `position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位', `hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间', PRIMARY KEY (`id`), KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工记录表';
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名', `age` smallint(6) NOT NULL DEFAULT '0' COMMENT '年龄', `hobby` varchar(60) NOT NULL DEFAULT '' COMMENT '用户爱好', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `modify_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
|
引入screw的maven依赖:
1 2 3 4 5
| <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency>
|
除此之外,还需引入数据库相关的依赖:
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>4.0.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency>
|
文档生成代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import java.util.ArrayList; import javax.sql.DataSource; import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.engine.EngineConfig; import cn.smallbun.screw.core.engine.EngineFileType; import cn.smallbun.screw.core.engine.EngineTemplateType; import cn.smallbun.screw.core.execute.DocumentationExecute; import cn.smallbun.screw.core.process.ProcessConfig;
public class Main {
final String fileOutputDir="E:\\dbDoc\\";
public static void main(String[] args) { new Main().documentGeneration(); }
public void documentGeneration() { HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.jdbc.Driver"); hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test"); hikariConfig.setUsername("root"); hikariConfig.setPassword("123456"); hikariConfig.addDataSourceProperty("useInformationSchema", "true"); hikariConfig.setMinimumIdle(2); hikariConfig.setMaximumPoolSize(5); DataSource dataSource = new HikariDataSource(hikariConfig); EngineConfig engineConfig = EngineConfig.builder() .fileOutputDir(fileOutputDir) .openOutputDir(true) .fileType(EngineFileType.HTML) .produceType(EngineTemplateType.freemarker) .fileName("test").build();
ArrayList<String> ignoreTableName = new ArrayList<>(); ignoreTableName.add("test_user"); ignoreTableName.add("test_group"); ArrayList<String> ignorePrefix = new ArrayList<>(); ignorePrefix.add("test_"); ArrayList<String> ignoreSuffix = new ArrayList<>(); ignoreSuffix.add("_test"); ProcessConfig processConfig = ProcessConfig.builder() .designatedTableName(new ArrayList<>()) .designatedTablePrefix(new ArrayList<>()) .designatedTableSuffix(new ArrayList<>()) .ignoreTableName(ignoreTableName) .ignoreTablePrefix(ignorePrefix) .ignoreTableSuffix(ignoreSuffix).build(); Configuration config = Configuration.builder() .version("1.0.0") .description("数据库设计文档生成") .dataSource(dataSource) .engineConfig(engineConfig) .produceConfig(processConfig) .build(); new DocumentationExecute(config).execute(); }
}
|
运行代码,生成html文档,浏览器打开,效果如下: