背景:有时候急需要数据库设计文档,手写太麻烦,这里介绍一款开源组件,可以自动根据数据库连接生成数据库设计文档
废话不多说,直接上代码
导入maven包
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.30</version></dependency><dependency><groupId>cn.smallbun.screw</groupId><artifactId>screw-core</artifactId><version>1.0.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency>
相关代码
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 lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ScrewApplicationTests {@AutowiredApplicationContext applicationContext;@Testpublic void contextLoads() {//数据源HikariConfig hikariConfig = new HikariConfig();hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");hikariConfig.setJdbcUrl("jdbc:mysql://xxx.xxx.x.xx:3306/sei-file?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");hikariConfig.setUsername("root");hikariConfig.setPassword("xxx");//设置可以获取tables remarks信息hikariConfig.addDataSourceProperty("useInformationSchema", "true");hikariConfig.setMinimumIdle(2);hikariConfig.setMaximumPoolSize(5);DataSource dataSource = new HikariDataSource(hikariConfig);// 生成文件配置EngineConfig engineConfig = EngineConfig.builder()// 生成文件路径,自己mac本地的地址,这里需要自己更换下路径.fileOutputDir("D:/Screw")// 打开目录.openOutputDir(false)// 文件类型.fileType(EngineFileType.WORD)// 生成模板实现.produceType(EngineTemplateType.freemarker).build();// 生成文档配置(包含以下自定义版本号、描述等配置连接)Configuration config = Configuration.builder().version("1.0.0").description("xxx数据库表结构设计文档").dataSource(dataSource).engineConfig(engineConfig).produceConfig(getProcessConfig()).build();// 执行生成new DocumentationExecute(config).execute();}/*** 配置想要生成的表+ 配置想要忽略的表* @return 生成表配置*/public static ProcessConfig getProcessConfig(){// 忽略表名List<String> ignoreTableName = Arrays.asList("region","test_group");// 忽略表前缀,如忽略a开头的数据库表List<String> ignorePrefix = Arrays.asList("gen");// 忽略表后缀List<String> ignoreSuffix = Arrays.asList("copy","copy1","copy2","copy3","copy4","copy5","copy6","copy7","copy8","copy9");return ProcessConfig.builder()//根据名称指定表生成.designatedTableName(new ArrayList<>())//根据表前缀生成.designatedTablePrefix(new ArrayList<>())//根据表后缀生成.designatedTableSuffix(new ArrayList<>())//忽略表名.ignoreTableName(ignoreTableName)//忽略表前缀.ignoreTablePrefix(ignorePrefix)//忽略表后缀.ignoreTableSuffix(ignoreSuffix).build();}
最终生成效果:
相关参考:
screw: 简洁好用的数据库表结构文档工具,支持MySQL/MariaDB/SqlServer/Oracle/PostgreSQL/TIDB/CacheDB 数据库。