码云 https://gitee.com/leshalv/screw?_from=gitee_search
原理:使用freemarker,velocity模板工具,支持生成html,markdown,doc文档
1. yml配置
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: 123456
hikari:
maximum-pool-size: 10
connection-timeout: 60000
minimum-idle: 5
idle-timeout: 500000
max-lifetime: 540000
connection-test-query: SELECT 1
2. pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring-boot-starter</artifactId>
<groupId>org.mybatis.spring.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
3. 测试
package com.yl.test;
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.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@SpringBootTest
public class TestScrewDocumentGenerator {
@Autowired
DataSource dataSourceMysql;
@Test
public void documentGenerator() throws Exception {
String database = dataSourceMysql.getConnection().getCatalog();
//生成index.html目录
generatorDocument( "D:/api/db/" + database, dataSourceMysql);
}
private void generatorDocument(String fileOutputDir, DataSource dataSource) {
// 配置生成方式
EngineConfig engineConfig = EngineConfig.builder()
// 生成文件路径
.fileOutputDir(fileOutputDir)
// 打开目录
.openOutputDir(false)
// 文件类型
.fileType(EngineFileType.HTML)
// 生成使用的模板
.produceType(EngineTemplateType.freemarker)
// 自定义文件名称
.fileName("index")
.build();
// 配置忽略的表
List<String> ignoreTableName = new ArrayList<>();
// ignoreTableName.add("test_user");
// ignoreTableName.add("test_group");
// 配置忽略指定前缀的表
List<String> ignoreTablePrefix = new ArrayList<>();
// ignoreTablePrefix.add("test_");
// 配置忽略指定后缀的表
List<String> ignoreTableSuffix = new ArrayList<>();
// ignoreTableSuffix.add("_test");
// 配置数据库表结构文档生成过程
ProcessConfig processConfig = ProcessConfig.builder()
/*
* 指定生成逻辑:
* - 如果存在指定的表,指定的表前缀,指定的表后缀时.会生成指定的表,其余表不会生成并且跳过忽略表配置
* - 如果未指定,会生成忽略表配置的其余所有的表
*/
// 生成指定名称的表
// .designatedTableName(new ArrayList<>())
// // 生成指定前缀的表
// .designatedTablePrefix(new ArrayList<>())
// 生成指定后缀的表
// .designatedTableSuffix(new ArrayList<>())
// 忽略指定名称的表
.ignoreTableName(ignoreTableName)
// 忽略指定前缀的表
.ignoreTablePrefix(ignoreTablePrefix)
// 忽略指定后缀的表
.ignoreTableSuffix(ignoreTableSuffix)
.build();
// 文档生成信息配置
Configuration config = Configuration.builder()
// 文档版本
.version("1.0.0")
// 文档描述
.description("数据库表结构文档")
// 数据源
.dataSource(dataSource)
// 生成配置
.engineConfig(engineConfig)
// 执行配置
.produceConfig(processConfig)
.build();
// 执行
new DocumentationExecute(config).execute();
}
}