记一次使用screw 生成MySQL数据库文档

tech2022-08-23  112

记一次使用screw 生成数据库文档

介绍

🚀 screw (螺丝钉) 英:[skruː] ~ 简洁好用的数据库表结构文档生成工具

特点

支持多种数据库

MySQLOracleTIDBSqlServerMariaDBPostgreSQLCache DB

支持多种生成文档类型

MD(Markdown)HTMLWord

普通方式

引入依赖 <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>${lastVersion}</version> </dependency> 编写代码 /** * 文档生成 */ @Test public void shouldAnswerWithTrue() { //数据源 HikariConfig hikariConfig = new HikariConfig(); // 驱动 根据版本的原因 驱动名可能不一样 hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/tobacco?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"); //用户名 hikariConfig.setUsername("root"); //密码 hikariConfig.setPassword("a123"); //设置可以获取tables remarks信息 hikariConfig.addDataSourceProperty("useInformationSchema", "true"); hikariConfig.setMinimumIdle(2); hikariConfig.setMaximumPoolSize(5); DataSource dataSource = new HikariDataSource(hikariConfig); //生成配置 EngineConfig engineConfig = EngineConfig.builder() //生成文件路径 若不存在会自动创建 .fileOutputDir("/Users/lengleng") //打开目录 生成完是否自动打开文件夹 .openOutputDir(true) //文件类型 可以有HTML, Word ,MD(MarkDown) .fileType(EngineFileType.MD) //生成模板实现 .produceType(EngineTemplateType.freemarker).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"); //指定表前缀 ArrayList<String> ignatedTable = new ArrayList<>(); ignatedTable.add("Party_"); ignatedTable.add("sys_"); ProcessConfig processConfig = ProcessConfig.builder() //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置 //根据名称指定表生成 .designatedTableName(new ArrayList<>()) //根据表前缀生成 .designatedTablePrefix(ignatedTable) //根据表后缀生成 .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(); }

Maven插件方式 - POM.XML(只在POM.xml配置完即可 不用其他任何配置)

<build> <plugins> <plugin> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-maven-plugin</artifactId> <version>${lastVersion}</version> <dependencies> <!-- HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> </dependencies> <configuration> <!--username--> <username>root</username> <!--password--> <password>password</password> <!--driver--> <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName> <!--jdbc url--> <jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl> <!--生成文件类型--> <fileType>HTML</fileType> <!--打开文件输出目录--> <openOutputDir>false</openOutputDir> <!--生成模板--> <produceType>freemarker</produceType> <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称--> <fileName>测试文档名称</fileName> <!--描述--> <description>数据库文档生成</description> <!--版本--> <version>${project.version}</version> <!--标题--> <title>数据库文档</title> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

运行

双击运行

结果

生成报错问题

导入完成 运行

java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader

原因是 缺少依赖

<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.

出现这种问题还是版本新,与其他版本造成

显示新版本的数据库连接程序需要指定UTC时区,改正方法将配置文件中的“url”后面加上指定的时区,将其值改为“url=jdbc:mysql://localhost:3306/db&serverTimezone=GMT”

例子:

jdbc:mysql://127.0.0.1:3306/tobacco?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT

生成后文档乱码?

MySQL:URL加入?characterEncoding=UTF-8。

Caused by: java.lang.NoSuchFieldError: VERSION_2_3_30?

检查项目freemarker依赖,这是由于版本过低造成的,升级版本为2.3.30即可。

MySQL数据库表和列字段有说明、生成文档没有说明?

URL链接加入useInformationSchema=true即可。

更多问题去Github观看开发者的Readme,上面有写。遇到其他问题也可以问开发者

本次生成测试的数据库是MySQL。

最新回复(0)