preface

In daily development, when after the project launch, party a will allow you to provide a bunch of acceptance documents, including the database dictionary, remember before I was a few tables, a CV, side to write again, this kind of thing give me how to do the development, all kinds of fun, a few hours after dozens of pages of Word document is released, The page is fancy, but not useful, and the party doesn’t even look at it.

Recently xiaobian is also looking for such plug-ins, is not to write documents, a waste of time and mood ah, as expected I found a more easy to use, simple operation is not complex.

Screw is a simple and easy to use database table structure document generation tool, support MySQL, Oracle, PostgreSQL and other mainstream relational databases.

This is github: github.com/pingfangush…


Import pom.xml dependencies

<dependencies> <! - screw library, <dependency> <groupId>cn. Smallbun.screw </groupId> </artifactId> screen-core </artifactId> <version>1.0. 5</version> </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.022.</version>
  </dependency>
</dependencies>
Copy the code

Create a Java class

/ * * *@description: Use SCREW to generate documentation *@author: DT
 * @date: 2021/5/9 12:19
 * @version: v1.0 * /

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 developer \\Desktop\\";
    // You can set the Word or Markdown format
    private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD;
    private static final String DOC_FILE_NAME = "Database Dictionary document";
    private static final String DOC_VERSION = "V1.0.0";
    private static final String DOC_DESCRIPTION = "Documentation";

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

        // Execute screw to generate the 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.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl(DB_URL + "/" + DB_NAME);
        hikariConfig.setUsername(DB_USERNAME);
        hikariConfig.setPassword(DB_PASSWORD);
        // Set to get the tables remarks information
        hikariConfig.addDataSourceProperty("useInformationSchema"."true");
        // Create the data source
        return new HikariDataSource(hikariConfig);
    }

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

How to use Maven plugins

<plugin>
 <groupId>cn.smallbun.screw</groupId>
 <artifactId>screw-maven-plugin</artifactId>
 <version>1.0. 5</version> <dependencies> <! <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4. 5</version>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.017.</version> </dependency> </dependencies> <configuration> <! </driverClassName> <jdbcUrl> JDBC :mysql:/ / 192.168.31.158:3306 / testdt? serverTimezone=Asia/Shanghai
     <username>root</username>
     <password>123456</password> <! </title> </title> <! <fileName> test document name </fileName> <! -- If the document name is empty: [database name - description - version number] will be used as the document name --> <description> Database document generation </description> <! ${project. Version}</version> <! -- version --> <openOutputDir>false</openOutputDir> <! Freemarker </produceType> <! Freemarker </produceType> <! Executions > <execution> <phase>compile</phase> <goal>run</goal> </goals> </execution> </executions> </plugin>Copy the code

Executing the SCREW -maven-plugin will generate documentation in the doc directory. As shown in the figure below:

conclusion

Screw is a simple and easy to use database table structure document generation tool, support MySQL, Oracle, PostgreSQL and other mainstream relational databases. It is still very convenient to use, do not have to manually write a data dictionary, very applicable, from now on farewell CV handwritten database documents, it is really painful ah! The plugin can also generate Java entity classes, but there are other things that Lombok doesn’t support, such as Swagger annotations, underlying entity field table mappings, etc. The next article will recommend some good scripts or utility classes for generating entity classes.