今天发现了一个很好用的数据库生成文档的代码记录下
添加一下依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<groupId>com.my</groupId>
<artifactId>mysqldocument</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
代码实现
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 org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.util.ArrayList;
@SpringBootTest
class MysqldocumentApplicationTests {
@Test
void documentGeneration() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
hikariConfig.setJdbcUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx_xx?characterEncoding=UTF-8");
hikariConfig.setUsername("root");
hikariConfig.setPassword("xxx");
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
EngineConfig engineConfig = EngineConfig.builder()
.fileOutputDir("/cn/")
.openOutputDir(true)
.fileType(EngineFileType.HTML)
.produceType(EngineTemplateType.freemarker)
.fileName("测试生成数据库文档").build();
ArrayList<String> ignoreTableName = new ArrayList<>();
ignoreTableName.add("xxl_job_group");
ignoreTableName.add("xxl_job_info");
ProcessConfig processConfig = ProcessConfig.builder()
.designatedTableName(new ArrayList<>())
.designatedTablePrefix(new ArrayList<>())
.designatedTableSuffix(new ArrayList<>())
.ignoreTableName(ignoreTableName).build();
Configuration config = Configuration.builder()
.version("1.0.0")
.description("数据库设计文档生成")
.dataSource(dataSource)
.engineConfig(engineConfig)
.produceConfig(processConfig)
.build();
new DocumentationExecute(config).execute();
}
}