在企业级开发中、我们经常会有编写数据库表结构文档的时间付出,关于数据库表结构文档状态:要么没有、要么有、但都是手写、后期运维开发,需要手动进行维护到文档中,很是繁琐,这里推荐一个开源项目:
screw
gitee地址:https://gitee.com/leshalv/screw
使用流程:
1.pom依赖
<dependencies><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><!-- HikariCP连接池 --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>3.4.5</version></dependency><!-- screw核心 --><dependency><groupId>cn.smallbun.screw</groupId><artifactId>screw-core</artifactId><version>1.0.5</version></dependency>
</dependencies>
2.具体代码
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;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;import javax.sql.DataSource;
import java.util.ArrayList;public class ScrewGenerator {public static void main(String[] args) {//数据源HikariConfig hikariConfig = new HikariConfig();hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");hikariConfig.setJdbcUrl("jdbc:mysql://127.0.01:3306/test?useSSL=false&serverTimezone=UTC");hikariConfig.setUsername("root");hikariConfig.setPassword("123456");//设置可以获取tables remarks信息hikariConfig.addDataSourceProperty("useInformationSchema", "true");hikariConfig.setMinimumIdle(2);hikariConfig.setMaximumPoolSize(5);DataSource dataSource = new HikariDataSource(hikariConfig);//生成配置EngineConfig engineConfig = EngineConfig.builder()//生成文件路径.fileOutputDir("C:\\Users\\Lenovo\\Desktop\\something")// 打开目录 设置为true执行完代码后会自动打开对应路径文件夹.openOutputDir(true)//生成类型//HTML(".html", "documentation_html", "HTML文件"),//WORD(".doc", "documentation_word", "WORD文件"),//MD(".md", "documentation_md", "Markdown文件");.fileType(EngineFileType.WORD)//自定义文件名称.produceType(EngineTemplateType.freemarker)//自定义文件名称.fileName("自定义文件名称").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()//忽略表名.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();System.out.println("生成成功");}
}
3.效果
根据这个配置修改生成的文件类型
//HTML(“.html”, “documentation_html”, “HTML文件”),
//WORD(“.doc”,“documentation_word”, “WORD文件”),
//MD(“.md”, “documentation_md”,“Markdown文件”);