Screw介绍
在我们平时的开发中,有时候不可避免的需要查看数据库表字段信息,通过数据库查看特别繁琐,在码云上开源的screw可以一键式生成数据库文档,目前screw可以生成word、md和html格式类的文档,支持freemarker和velocity两种模板引擎。数据源支持HikariDataSource,DruidDataSource可能会发生类型转换报错。本节我们就来使用screw生成一下数据库文档。
1.创建springboot工程并引入依赖
引入数据库驱动和 screw-core的依赖,数据库驱动具体支持哪些请查看码云链接。
<?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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>screw</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>screw</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 在application.properties中配置数据库连接信息
spring.datasource.url=jdbc:mysql://XXX:3306/XXX?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#按照码云文档说明: MySQL数据库表和列字段有说明、生成文档没有说明
spring.datasource.xa.properties.userInformationSchema= true
3. 编写测试类并测试
@SpringBootTest
class ScrewApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@Test
void testGenerateDatabaseDocument() {
DataSource dataSource = applicationContext.getBean(DataSource.class);
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir("/resources")
// 打开目录
.openOutputDir(true)
// 文件类型,支持word、md和html
.fileType(EngineFileType.HTML)
//生成模板实现,支持freemarker和velocity
.produceType(EngineTemplateType.freemarker).build();
//生成配置文档
Configuration configuration = Configuration.builder()
.version("1.0.3")
.description("学生数据库文档描述信息")
.dataSource(dataSource)
.engineConfig(engineConfig)
.produceConfig(getProcessConfig())
.build();
//执行生成
new DocumentationExecute(configuration).execute();
}
/**
* 配置想要生成的表 + 配置想要忽略的表
* @return
*/
private ProcessConfig getProcessConfig() {
// 忽略表名
List<String> ignoreTable = Arrays.asList("aa","test_group");
//忽略表前缀,如忽略a开头的数据库表
List<String> ignorePrefix = Arrays.asList("sys");
//忽略表后缀
List<String> ignoreSuffix = Arrays.asList("month");
return ProcessConfig.builder()
// 根据名称生成指定表
.designatedTableName(new ArrayList<String>())
// 根据表前缀生成
.designatedTablePrefix(new ArrayList<String>())
// 根据表后缀生成
.designatedTableSuffix(new ArrayList<String>())
// 忽略表名
.ignoreTableName(ignoreTable)
// 忽略表前缀
.ignoreTablePrefix(ignorePrefix)
// 忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
}
}
4.生成结果如下
如上配置,在当前盘符下的resources文件夹下生成文件
-
html
image.png -
markdown
image.png -
word
image.png
5.screw帮助链接
文档生成工具screw