This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021″

Preface:

Instead of writing database design documents by hand, Java database documents can be generated in one click by introducing screw core package. Without further ado, go straight to the code demo.

List of supported databases:

  • MySQL
  • MariaDB
  • TIDB
  • Oracle
  • SqlServer
  • PostgreSQL
  • All of these major databases support it.

Implementation steps:

Introduces pom.xml core configuration SCREW package

       <dependency>
			<groupId>cn.smallbun.screw</groupId>
			<artifactId>screw-core</artifactId>
			<version>1.0.5</version>
		</dependency>
Copy the code

The main method class contains the following code:

package com.railway.modules.sys.controller;

/** * Created by LiYangYong on 2021/11/11 */
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 javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collections;

/** * Created by LiYangYong on 2021/11/11. */
public class TestScrewMain {
    private static final String DB_URL = "jdbc:mysql://localhost:3307";
    private static final String DB_NAME = "renren_fast? useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "crit@2019";
    private static final String FILE_OUTPUT_DIR = "C:\\Users\\Administrator\\Desktop\\java\\";

    // You can set Word or Markdown format
    private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD;
    private static final String DOC_FILE_NAME = "Database table Design Document";
    private static final String DOC_VERSION = "V1.0.0";
    private static final String DOC_DESCRIPTION = "Database table Design Description";

    public static void main(String[] args) {
        // Create screw configuration
        Configuration config = Configuration.builder()
                / / version
                .version(DOC_VERSION)
                / / description
                .description(DOC_DESCRIPTION)
                / / the data source
                .dataSource(buildDataSource())
                // Engine configuration
                .engineConfig(buildEngineConfig())
                // Process the configuration
                .produceConfig(buildProcessConfig())
                .build();

        // Execute SCREW to generate database document
        new DocumentationExecute(config).execute();
    }

    /** * Create data source */
    private static DataSource buildDataSource(a) {
        // Create the HikariConfig configuration class
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        hikariConfig.setJdbcUrl(DB_URL + "/" + DB_NAME);
        hikariConfig.setUsername(DB_USERNAME);
        hikariConfig.setPassword(DB_PASSWORD);
        // Set to get tables Remarks
        hikariConfig.addDataSourceProperty("useInformationSchema"."true");
        // Create a data source
        return new HikariDataSource(hikariConfig);
    }

    /** * Create screw's engine configuration */
    private static EngineConfig buildEngineConfig(a) {
        return EngineConfig.builder()
                // Generate file path
                .fileOutputDir(FILE_OUTPUT_DIR)
                // Open the directory
                .openOutputDir(false)
                // File type
                .fileType(FILE_OUTPUT_TYPE)
                // File type
                .produceType(EngineTemplateType.freemarker)
                // Customize the file name
                .fileName(DOC_FILE_NAME)
                .build();
    }

    /** * create screw processing configuration, generally can ignore * specified generation logic, when there is a specified table, a specified table prefix, a specified table suffix, will generate a specified table, the rest of the table is not generated, and skip ignore table configuration */
    private static ProcessConfig buildProcessConfig(a) {
        return ProcessConfig.builder()
                // Specify table generation by name
                .designatedTableName(Collections.<String>emptyList())
                // Based on the table prefix
                .designatedTablePrefix(Collections.<String>emptyList())
                // Based on the table suffix
                .designatedTableSuffix(Collections.<String>emptyList())
                // Ignore the table name
                .ignoreTableName(Arrays.asList("test"."mytable"."role"."t_role"."t_user"))
                // Ignore the table prefix
                //.ignoreTablePrefix(Collections.singletonList("t_"))
                // Ignore the table suffix
                //.ignoreTableSuffix(Collections.singletonList("_test")).build(); }}Copy the code

Generated database design document demo

 

Table qrtz_blob_triggers

Serial number The name of the The data type The length of the Decimal places Allows null values A primary key The default value instructions
1 SCHED_NAME varchar 120 0 N Y
2 TRIGGER_NAME varchar 200 0 N Y
3 TRIGGER_GROUP varchar 200 0 N Y
4 BLOB_DATA blob 65535 0 Y N

Table qrtz_calendars

Serial number The name of the The data type The length of the Decimal places Allows null values A primary key The default value instructions
1 SCHED_NAME varchar 120 0 N Y
2 CALENDAR_NAME varchar 200 0 N Y
3 CALENDAR blob 65535 0 N N

Table qrtz_cron_triggers

Serial number The name of the The data type The length of the Decimal places Allows null values A primary key The default value instructions
1 SCHED_NAME varchar 120 0 N Y
2 TRIGGER_NAME varchar 200 0 N Y
3 TRIGGER_GROUP varchar 200 0 N Y
4 CRON_EXPRESSION varchar 120 0 N N
5 TIME_ZONE_ID varchar 80 0 Y N

This frees the hands to pull