数据库字典screw配置
数据库字典screw

screw是简洁好用的数据库表结构文档生成工具。由screw自动生成数据字典,如上图所示。
参考源码及文档:https://gitee.com/leshalv/screw
screw可以通过maven插件和普通代码的方式配置。下面就介绍通过这两种方式配置MySQL和Oracle两种数据库。
Maven插件的方式配置
1.引入依赖
2.修改驱动账户名密码
3.使用maven插件screw:run
1.引入依赖
<dependencies>
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>1.0.2</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
<!--username-->
<username>root</username>
<!--password-->
<password>admin</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/shop_goods?characterEncoding=UTF-8&serverTimezone=UTC</jdbcUrl>
<!--生成文件类型-->
<fileType>HTML</fileType>
<!--打开文件输出目录-->
<openOutputDir>false</openOutputDir>
<!--生成模板-->
<produceType>freemarker</produceType>
<!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
<fileName>测试文档名称</fileName>
<!--描述-->
<description>数据库文档生成</description>
<!--版本-->
<version>${project.version}</version>
<!--标题-->
<title>数据库文档</title>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
2.修改驱动和账户名密码
在刚刚引入的依赖里面,找到
<!--username-->
<username>root</username>
<!--password-->
<password>admin</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/shop_goods</jdbcUrl>
修改账户名密码,驱动和URL。
3.使用maven插件screw:run输出数据字典

点击screw:run就能自动生成数据字典
普通代码方式配置
编写springboottest引入screw
1.依赖引入
2.编写测试类
3.配置jdbc属性
4.启动测试类
1.依赖引入
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2.编写测试类
package com.example.demo;
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.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;
@SpringBootTest
@ConditionalOnClass(DataSource.class)
public class ScrewSpringbootTest {
@Autowired
ApplicationContext applicationContext;
@Test
public void contextLoads() {
DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
// 生成文件配置
EngineConfig engineConfig = EngineConfig.builder()
// 生成文件路径,自己mac本地的地址,这里需要自己更换下路径
.fileOutputDir("D:\\")
// 打开目录
.openOutputDir(false)
// 文件类型
.fileType(EngineFileType.HTML)
// 生成模板实现
.produceType(EngineTemplateType.freemarker).build();
// 生成文档配置(包含以下自定义版本号、描述等配置连接)
Configuration config = Configuration.builder()
.version("1.0.3")
.description("生成文档信息描述")
.dataSource(dataSourceMysql)
.engineConfig(engineConfig)
.produceConfig(getProcessConfig())
.build();
// 执行生成
new DocumentationExecute(config).execute();
}
/**
* 配置想要生成的表+ 配置想要忽略的表
* @return 生成表配置
*/
public ProcessConfig getProcessConfig(){
// 忽略表名
List<String> ignoreTableName = Arrays.asList("aa","test_group");
// 忽略表前缀,如忽略a开头的数据库表
List<String> ignorePrefix = Arrays.asList("a","t");
// 忽略表后缀
List<String> ignoreSuffix = Arrays.asList("_test","czb_");
return ProcessConfig.builder()
//根据名称指定表生成
.designatedTableName(new ArrayList<>())
//根据表前缀生成
.designatedTablePrefix(new ArrayList<>())
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀
.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
}
}
3.配置jdbc属性
注意根据自己数据库账户名密码URL进行修改
#mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop_goods?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true
#oracle
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@//ip:1521/服务名
spring.datasource.username=root
spring.datasource.password=admin
4.启动测试类即可在对应的路径找到输出文件
Oracle与MySQL配置区别
主要还是数据库连接驱动的区别
引入依赖的时候可以将MySQL驱动修改成如下
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
总结
1.使用插件的方式配置更加简单快速,但是使用普通代码的时候可以修改过滤规则,插件默认是不显示系统表
2.screw1.0.3比screw1.0.2的config多了多虑参数,目前最新版为1.0.5,实际选择1.0.3以上版本即可

浙公网安备 33010602011771号