温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用java怎么自动生成数据库文档

发布时间:2021-05-13 16:41:46 来源:亿速云 阅读:330 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用java怎么自动生成数据库文档,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一、引入pom.xml依赖

<dependencies>
  <!-- screw 库,简洁好用的数据库表结构文档生成器 -->
  <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>
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.22</version>
  </dependency>
</dependencies>

二、创建Java类

public class TestScrewMain {

    private static final String DB_URL = "jdbc:mysql://192.168.31.158:3306";
    private static final String DB_NAME = "testdt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "123456";

    private static final String FILE_OUTPUT_DIR = "C:\\Users\\DT开发者\\Desktop\\";
    // 可以设置 Word 或者 Markdown 格式
    private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD;
    private static final String DOC_FILE_NAME = "数据库字典文档";
    private static final String DOC_VERSION = "V1.0.0";
    private static final String DOC_DESCRIPTION = "文档描述";

    public static void main(String[] args) {
        // 创建 screw 的配置
        Configuration config = Configuration.builder()
                // 版本
                .version(DOC_VERSION)
                // 描述
                .description(DOC_DESCRIPTION)
                // 数据源
                .dataSource(buildDataSource())
                // 引擎配置
                .engineConfig(buildEngineConfig())
                // 处理配置
                .produceConfig(buildProcessConfig())
                .build();

        // 执行 screw,生成数据库文档
        new DocumentationExecute(config).execute();
    }

    /**
     * 创建数据源
     */
    private static DataSource buildDataSource() {
        // 创建 HikariConfig 配置类
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl(DB_URL + "/" + DB_NAME);
        hikariConfig.setUsername(DB_USERNAME);
        hikariConfig.setPassword(DB_PASSWORD);
        // 设置可以获取 tables remarks 信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        // 创建数据源
        return new HikariDataSource(hikariConfig);
    }

    /**
     * 创建 screw 的引擎配置
     */
    private static EngineConfig buildEngineConfig() {
        return EngineConfig.builder()
                // 生成文件路径
                .fileOutputDir(FILE_OUTPUT_DIR)
                // 打开目录
                .openOutputDir(false)
                // 文件类型
                .fileType(FILE_OUTPUT_TYPE)
                // 文件类型
                .produceType(EngineTemplateType.freemarker)
                // 自定义文件名称
                .fileName(DOC_FILE_NAME)
                .build();
    }

    /**
     * 创建 screw 的处理配置,一般可忽略
     * 指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
     */
    private static ProcessConfig buildProcessConfig() {
        return ProcessConfig.builder()
                // 根据名称指定表生成
                .designatedTableName(Collections.<String>emptyList())
                // 根据表前缀生成
                .designatedTablePrefix(Collections.<String>emptyList())
                // 根据表后缀生成
                .designatedTableSuffix(Collections.<String>emptyList())
                // 忽略表名
                .ignoreTableName(Arrays.asList("test", "mytable","role","t_role","t_user"))
                // 忽略表前缀
                //.ignoreTablePrefix(Collections.singletonList("t_"))
                // 忽略表后缀
                //.ignoreTableSuffix(Collections.singletonList("_test"))
                .build();
    }

}

使用java怎么自动生成数据库文档
使用java怎么自动生成数据库文档

三、使用 Maven 插件的方式

<plugin>
 <groupId>cn.smallbun.screw</groupId>
 <artifactId>screw-maven-plugin</artifactId>
 <version>1.0.5</version>
 <dependencies>
     <!-- 数据库连接 -->
     <dependency>
         <groupId>com.zaxxer</groupId>
         <artifactId>HikariCP</artifactId>
         <version>3.4.5</version>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.17</version>
     </dependency>
 </dependencies>
 <configuration>
     <!-- 数据库相关配置 -->
     <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
     <jdbcUrl>jdbc:mysql://192.168.31.158:3306/testdt?serverTimezone=Asia/Shanghai</jdbcUrl>
     <username>root</username>
     <password>123456</password>
     <!-- screw 配置 -->
     <fileType>WORD</fileType>
     <title>数据库文档</title> <!--标题-->
     <fileName>测试文档名称</fileName> <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
     <description>数据库文档生成</description> <!--描述-->
     <version>${project.version}</version> <!--版本-->
     <openOutputDir>false</openOutputDir> <!--打开文件输出目录-->
     <produceType>freemarker</produceType> <!--生成模板-->
 </configuration>
 <executions>
     <execution>
         <phase>compile</phase>
         <goals>
             <goal>run</goal>
         </goals>
     </execution>
 </executions>
</plugin>

执行 screw-maven-plugin 插件,会在 doc 目录下生成文档。如下图所示:

使用java怎么自动生成数据库文档

Java有哪些集合类

Java中的集合主要分为四类:1、List列表:有序的,可重复的;2、Queue队列:有序,可重复的;3、Set集合:不可重复;4、Map映射:无序,键唯一,值不唯一。

看完上述内容,你们对使用java怎么自动生成数据库文档有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI