This is the 23rd day of my participation in the August More Text Challenge

As the amount of code increases day by day, the requirements of the business are gradually understood, but look at the whole project, various modules and functions, many services and many tables. Therefore, in order to be familiar with the data structure of the whole project in a short time, we must have a certain understanding of the database table in the macro. And SCREW tool is very good to meet our needs, it can output the table of the entire database as the database table structure document, whether it is in the analysis or familiar with the table structure can improve the efficiency.

The SCREW project’s Github address is: SCREW

Screw

1. Database support

Screw is a simple and easy to use database table structure document generation tool, it supports a variety of databases and support custom template output documents, documents also have a variety of file types to choose. Currently supported databases are:

  1. MySQL
  2. Oracle
  3. SqlServer
  4. PostgreSQL
  5. Cache DB
  6. MariaDB
  7. TIDB

The project is still under development and more databases will be added.

2. Screw use procedure

2.1 Java code mode

2.1.1 Screw dependency is introduced in POM file

Create a SpringBoot project and introduce web and mysql initiators, introduce SCREW’s core dependencies and HikariCP database connection pool dependencies in the PROJECT’s POM file, screw uses the HikariCP database connection pool when creating database documents.

<! -- Screw core -->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.5</version>
</dependency>
<! -- HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.4.5</version>
</dependency>
Copy the code

2.1.2 Setting Data Source Information

In the SpringBoot application.properties configuration file, configure the data source information for creating database documents. To generate remarks for tables, use useInformationSchema=true.

spring.datasource.url=jdbc:mysql://localhost:3306/fire? useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true 
Copy the code

2.1.3 Generate database documents using code

We need to create a Java class and add the following official code methods to the class. Some of the configuration contents can be modified as needed, such as the configuration document generation path and the generated file format, etc. Finally, the execution method will generate the document in the defined directory.

Screw generates database documents in HTML, Word and Markdowm formats.

/** * Document generation method */
void documentGeneration(a) {
   // Get the data source configuration
   HikariConfig hikariConfig = new HikariConfig();
   hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
   hikariConfig.setJdbcUrl("JDBC: mysql: / / 127.0.0.1:3306 / database");
   hikariConfig.setUsername("root");
   hikariConfig.setPassword("password");
   // Set to get tables Remarks
   hikariConfig.addDataSourceProperty("useInformationSchema"."true");
   hikariConfig.setMinimumIdle(2);
   hikariConfig.setMaximumPoolSize(5);
   DataSource dataSource = new HikariDataSource(hikariConfig);
   // Build the configuration
   EngineConfig engineConfig = EngineConfig.builder()
         // Generate file path
         .fileOutputDir(fileOutputDir)
         // Open the directory
         .openOutputDir(true)
         // File type
         .fileType(EngineFileType.HTML)
         // Generate template implementation
         .produceType(EngineTemplateType.freemarker)
         // Customize the file name
         .fileName("Custom file name").build();

   / / ignore list
   ArrayList<String> ignoreTableName = new ArrayList<>();
   ignoreTableName.add("test_user");
   ignoreTableName.add("test_group");
   // Ignore the table prefix
   ArrayList<String> ignorePrefix = new ArrayList<>();
   ignorePrefix.add("test_");
   // Ignore the table suffix
   ArrayList<String> ignoreSuffix = new ArrayList<>();
   ignoreSuffix.add("_test");
   ProcessConfig processConfig = ProcessConfig.builder()
         If there is a specified table, a specified table prefix, or a specified table suffix, the specified table will be generated. Other tables will not be generated, and the configuration of the table will be skipped
		 // Specify table generation by name
		 .designatedTableName(new ArrayList<>())
		 // Based on the table prefix
		 .designatedTablePrefix(new ArrayList<>())
		 // Based on the table suffix
		 .designatedTableSuffix(new ArrayList<>())
         // Ignore the table name
         .ignoreTableName(ignoreTableName)
         // Ignore the table prefix
         .ignoreTablePrefix(ignorePrefix)
         // Ignore the table suffix
         .ignoreTableSuffix(ignoreSuffix).build();
   / / configuration
   Configuration config = Configuration.builder()
         / / version
         .version("1.0.0")
         / / description
         .description("Database Design Document Generation")
         / / the data source
         .dataSource(dataSource)
         // Build the configuration
         .engineConfig(engineConfig)
         // Build the configuration
         .produceConfig(processConfig)
         .build();
   // Perform the build
   new DocumentationExecute(config).execute();
}
Copy the code

2.2. Generate database documents directly using maven plug-ins

In addition to using Java code, we can add screw configuration directly in the POM file as a Maven plug-in, and use screw->run in the Maven module to execute and generate database documentation after the dependency is loaded.

All dependency information and output document path and file format information are defined in the Maven plug-in.

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.3</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>123456</password>
                <! --driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <! --jdbc url-->
                <jdbcUrl>jdbc:mysql://localhost:3306/fire</jdbcUrl>
                <! -- Generate file type -->
                <fileType>HTML</fileType>
                <! -- Open file output directory -->
                <openOutputDir>false</openOutputDir>
                <! Create a template -->
                <produceType>freemarker</produceType>
                <! -- Empty document name: [database name - description - version number] will be used as document name -->
                <! </docName>-->
                <! - description -- -- >
                <description>Database document generation</description>
                <! -- version -- -- >
                <version>${project.version}</version>
                <! - the title -- -- >
                <title>Fire Database Documentation</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code

3. Maven cannot download dependencies

Today in the introduction of SCREW dependence, maven in IDEA suddenly flutters, SCREW dependence download down, after a toss, the final solution to the problem, also in this record.

3.1 Manually Updating dependency Information

If the dependency is not downloaded successfully, it may be due to the deviation of the automatic import dependency of IDEA. We can try the following methods:

  1. Import/download the dependency information again in the Maven bar of IDEA
  2. Remove and re-add dependencies in the POM file and let Maven re-import
  3. We can go to maven’s local repository to check whether the corresponding coordinates already have dependencies. If there are incomplete dependencies, we need to delete them and import them again

3.2 the maven source

When a dependency introduced in a project in IDEA doesn’t download properly, the first thing to think about is whether maven is configured correctly in IDEA. In IDEA, open file -> Settings and search for Maven to view the maven configuration information currently in use.

  1. If it comes with IDEABundled(maven3)So we have to find a correspondencesettingsCheck whether the official Maven tool configuration is insufficient or the image information is not added, resulting in slow or failure to download remote dependency files.
  2. If you are downloading Maven from yourself, check out the official versionsettingsandrepositoryWhether the path is overridden by local Maven, and whether the Settings profile in local Maven uses a domestic mirror source.
  • Maven 3.6.1 May cause compatibility problems for IDEA in 2019. If the local Maven version is incompatible with IDEA, you can download a later version or temporarily use Maven with IDEA.

4. The last

Well, the above is my project in some business needs to use SCREW to create database documents, and the introduction of dependency in IDEA is the record of small problems, if you also have the same needs or problems, I hope to help you oh!