问题背景
项目验收阶段,需要提供数据库说明文档。
项目服务使用的spring boot,版本2.1.6.RELEASE
解决方案
1.pom引入freemarker和screw-core依赖
代码:
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.smallbun.screw/screw-core -->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
2.检查数据库连接配置
配置示例:
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true
3.使用测试方法生成文档
代码示例:
package com.xxx;
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 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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void contextLoads() {
DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
// 生成文件配置
EngineConfig engineConfig = EngineConfig.builder()
// 生成文件路径,这里需要自己更换下路径
//.fileOutputDir("/Users/mac/Desktop")Mac路径示例
//win系统路径示例
.fileOutputDir("D:\\")
// 打开目录
.openOutputDir(false)
// 文件类型,这里是生成word文档,可以按需改为其他文件类型。
.fileType(EngineFileType.WORD)
// 生成模板实现
.produceType(EngineTemplateType.freemarker).build();
// 生成文档配置(包含以下自定义版本号、描述等配置连接)
Configuration config = Configuration.builder()
//版本号按需修改
.version("1.0.0")
.description("生成文档信息描述")
.dataSource(dataSourceMysql)
.engineConfig(engineConfig)
.produceConfig(getProcessConfig())
.build();
// 执行生成
new DocumentationExecute(config).execute();
}
/**
* 配置想要生成的表+ 配置想要忽略的表
* @return 生成表配置
*/
public static ProcessConfig getProcessConfig(){
// 忽略表名,按实际需要填写
List<String> ignoreTableName = Arrays.asList("demo1","demo2");
// 忽略表前缀,如忽略a开头的数据库表
//List<String> ignorePrefix = Arrays.asList("a","t");
// 忽略表后缀,按实际需要填写
List<String> ignoreSuffix = Arrays.asList("_copy","_test");
return ProcessConfig.builder()
//根据名称指定表生成
.designatedTableName(new ArrayList<>())
//根据表前缀生成
.designatedTablePrefix(new ArrayList<>())
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀。如不需要此过滤规则可以注释掉。
//.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
}
}
4.说明
亲测可用。原帖见于开源中国,2020年7月发表,本帖使用freemarker和screw-core组件为当前最新版本。
原帖地址:https://my.oschina.net/mdxlcj/blog/4341399