Mybatis-Plus development accelerator mybatis-plus-generator-ui

foreword

       In the development mode based on Mybatis, many developers will also choose Mybatis-Plus to assist function development, so as to improve the efficiency of development. Although Mybatis also has code generation tools, Mybatis-Plus has made some adjustments based on Mybatis, so the code generated by conventional generation tools still does not meet expectations. And the support for multiple databases is not very good. Therefore, we need a basic program generation framework that supports high customization, has a graphical UI page, and can adapt to most databases. This article introduces this code self-service generator based on Mybatis-Plus, github address: mybatis-plus-generator-ui . The article explains mybatis-plus-generator-ui in detail through example integration. Interested friends can clone it by themselves, or they can expand and customize it by themselves.

 1. What is mybatis-plus-generator-ui?

       It encapsulates mybatis-plus-generator, and quickly generates various business codes compatible with Spring boot and mybatis-plus frameworks through the Web UI. Provides an interactive Web UI for generating relevant functional codes compatible with the mybatis-plus framework, including Entity, Mapper, Mapper.xml, Service, Controller, etc. You can customize templates and various output parameters, or directly through SQL query statements Generate code.

e6927af544754f648791caac3fb5ecba.png

7afd27972f0942efb4deaf6c07e10cc4.png

function list:

  1. Table query: query the list query of the configured relational database table.
  2. Output configuration: Configure the relevant code that needs to be generated, such as code template information such as Entity, Mapper, Servcie, and Controller, for calling during conversion.
  3. Project import: You can import information configured by other projects for use in this project.
  4. Download template: The template information that supports the configuration of this project is downloaded and shared.
  5. Policy configuration: directly define the generation policy of various files.
  6. Template upload: It supports downloading templates from other projects and uploading them for use in this project.
  7. SQL input upload: Supports directly uploading or copying query statements into the input box.
  8. SQL code generation: Generate corresponding codes based on SQL scripts.

2. How to use mybatis-plus-generator-ui?

       mybatis-plus-generator-ui provides services for external projects in the form of jar packages, reads the configuration information of the database through the configured database configuration, and provides it to developers through the Web UI. mybatis-plus-generator-ui supports POSTGRE_SQL, ORACLE, DB2, MySQL, SQLSERVER and other common relational databases.

1. Introduction of maven pom

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.yelang</groupId>
	<artifactId>mybatis-plus-generator-ui-case</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.github.davidfantasy</groupId>
			<artifactId>mybatis-plus-generator-ui</artifactId>
			<version>1.4.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>42.2.25</version>
		</dependency>
	</dependencies>
</project>

2. Create a new program entry and run it as the main function

After version 1.4.0 ,        mybatis-plus-generator-ui can support the independent deployment of GeberatorUIServer as a separate spring boot project, and provide source code generation services for multiple projects by specifying the root directory of the target project on the page. This method is suitable for the mode where multiple project libraries need to be developed independently. The key code of the example is as follows:

package com.yelang;

import com.github.davidfantasy.mybatisplus.generatorui.GeneratorConfig;
import com.github.davidfantasy.mybatisplus.generatorui.MybatisPlusToolsApplication;
import com.github.davidfantasy.mybatisplus.generatorui.mbp.NameConverter;

public class GeneratorMain {

	public static void main(String[] args) {
		GeneratorConfig config = GeneratorConfig.builder().jdbcUrl("jdbc:postgresql://127.0.0.1:5432/ghyapp")
				.userName("ghy01").password("ghy01").driverClassName("org.postgresql.Driver")
				// 数据库schema,POSTGRE_SQL,ORACLE,DB2类型的数据库需要指定
				// .schemaName("myBusiness")
				// 如果需要修改各类生成文件的默认命名规则,可自定义一个NameConverter实例,覆盖相应的名称转换方法:
				.nameConverter(new NameConverter() {
					/**
					 * 自定义Service类文件的名称规则
					 */
					public String serviceNameConvert(String tableName) {
						return this.entityNameConvert(tableName) + "Service";
					}

					/**
					 * 自定义Controller类文件的名称规则
					 */
					public String controllerNameConvert(String tableName) {
						return this.entityNameConvert(tableName) + "Action";
					}
				}).basePackage("com.github.davidfantasy.mybatisplustools.example").port(8068).build();

		MybatisPlusToolsApplication.run(config);

	}

}

       In the above configuration, the sample database we connect to is PostgerSQL, and the corresponding driver needs to be defined in Maven, and the corresponding classes are correctly configured in the above code. Finally, the running port of the program is specified as 8086, which is very similar to SpringBoot. 

3. Instance running

       Run the above main method, and the following output can be seen on the console, which means successful deployment.

1af61643019c4e71ac6f887faae3f2d7.png

       In the output log, you can see the running port of the program and the default template directory address. Enter the access address http://localhost:8068/ in the browser to generate the configuration.

Three, mybatis-plus-generator-ui code generation

1. Table query and browsing

      You can directly browse and query the data table information of the configured data sources, and you can choose one or more to generate template codes.

2b2be25ebf1c496fa031f3ecbc45a22d.png

 2. Output configuration

        Built-in template configurations for 6 types of codes, including Entity, Mapper, Service, and Controller, you can upload templates for replacement, and modify various parameters. Configuration parameters have been reclassified according to the affected file types, and some text descriptions have been added; You can add other types of custom output files yourself. All configuration items will be saved according to the project package name, and only need to be set once.

5d45575e060545e093081a0c1bea48ca.png

 3. Policy configuration

      Add the content that may change each time the code is generated to the code generation options, so as to adjust the generation strategy each time, such as: whether to overwrite the original file, the type of generated file, etc.:

2907b966884b463c95516d5af9672c1b.png

 4. SQL configuration generation

      By inputting query SQL, the corresponding query method, DTO object and ResultMap (result set mapping configuration) can be automatically generated in Mapper (Xml and Java).

652144671dd842f287b022c2f5a9c54c.png

 5. Code generation

b5674e572bc4405cb0f09160b372b4cd.png

 c3514e4ba9d24b8bac98cd603842862b.png

4. Custom extension

1. Relevant template adjustments

       In the relevant pages, you can make corresponding adjustments, download the specific template of the corresponding file in the corresponding btl template, open it with a text tool, and directly modify the source code. One method is taken as an example in the article, and the other methods are the same.

5998559d8d12412c9fff669c6ee290c8.png

57de39b9532c42acb61f5d2aad7cba10.png

 2. Code level configuration

      In some teams, Mapper must be defined as Dao, and the Controller layer needs to be defined as Action. It is possible to modify the code template btl, and another way is to modify the internal mapping. The main class used is NameConverter.

/**
* 自定义Service类文件的名称规则
*/
public String serviceNameConvert(String tableName) {
	return this.entityNameConvert(tableName) + "Service";
}

/**
* 自定义Controller类文件的名称规则
*/
public String controllerNameConvert(String tableName) {
		return this.entityNameConvert(tableName) + "Action";
}

       In addition to Service, Controller, Entity, and FieldName can all implement custom extensions. The following is the core code of the NameConverter class, here is a detailed definition.

package com.github.davidfantasy.mybatisplus.generatorui.mbp;

import cn.hutool.core.util.StrUtil;
import com.github.davidfantasy.mybatisplus.generatorui.dto.Constant;
import com.google.common.base.Strings;

import static com.github.davidfantasy.mybatisplus.generatorui.dto.Constant.DOT_JAVA;
import static com.github.davidfantasy.mybatisplus.generatorui.dto.Constant.DOT_XML;

/**
 * 自定义各类名称转换的规则
 */
public interface NameConverter {

    /**
     * 自定义Entity.java的类名称
     *
     * @param tableName 表名称
     * @return
     */
    default String entityNameConvert(String tableName) {
        if (Strings.isNullOrEmpty(tableName)) {
            return "";
        }
        tableName = tableName.substring(tableName.indexOf(StrUtil.UNDERLINE) + 1, tableName.length());
        return StrUtil.upperFirst(StrUtil.toCamelCase(tableName.toLowerCase()));
    }

    /**
     * 自定义表字段名到实体类属性名的转换规则
     *
     * @param fieldName 表字段名称
     * @return
     */
    default String propertyNameConvert(String fieldName) {
        if (Strings.isNullOrEmpty(fieldName)) {
            return "";
        }
        if (fieldName.contains("_")) {
            return StrUtil.toCamelCase(fieldName.toLowerCase());
        }
        return fieldName;
    }

    /**
     * 自定义Mapper.java的类名称
     */
    default String mapperNameConvert(String tableName) {
        return entityNameConvert(tableName) + "Mapper";
    }

    /**
     * 自定义Mapper.xml的文件名称
     */
    default String mapperXmlNameConvert(String tableName) {
        return entityNameConvert(tableName) + "Mapper";
    }

    /**
     * 自定义Service.java的类名称
     */
    default String serviceNameConvert(String tableName) {
        return "I" + entityNameConvert(tableName) + "Service";
    }

    /**
     * 自定义ServiceImpl.java的类名称
     */
    default String serviceImplNameConvert(String tableName) {
        return entityNameConvert(tableName) + "ServiceImpl";
    }

    /**
     * 自定义Controller.java的类名称
     */
    default String controllerNameConvert(String tableName) {
        return entityNameConvert(tableName) + "Controller";
    }

    /**
     * 自定义其它生成文件的文件名(不包括entity,mapper.java,mapper.xml,service,serviceImpl,controller这6种)
     *
     * @param fileType  在页面上输入的输出文件标识
     * @param tableName 关联的数据表名称名称
     * @return 生成文件的名称,带后缀
     */
    default String outputFileNameConvert(String fileType, String tableName) {
        if (fileType.equals(Constant.FILE_TYPE_ENTITY)) {
            return this.entityNameConvert(tableName) + DOT_JAVA;
        } else if (fileType.equals(Constant.FILE_TYPE_MAPPER)) {
            return this.mapperNameConvert(tableName) + DOT_JAVA;
        } else if (fileType.equals(Constant.FILE_TYPE_MAPPER_XML)) {
            return this.mapperXmlNameConvert(tableName) + DOT_XML;
        } else if (fileType.equals(Constant.FILE_TYPE_SERVICE)) {
            return this.serviceNameConvert(tableName) + DOT_JAVA;
        } else if (fileType.equals(Constant.FILE_TYPE_SERVICEIMPL)) {
            return this.serviceImplNameConvert(tableName) + DOT_JAVA;
        } else if (fileType.equals(Constant.FILE_TYPE_CONTROLLER)) {
            return this.controllerNameConvert(tableName) + DOT_JAVA;
        }
        return this.entityNameConvert(tableName) + fileType;
    }

}

      mybatis-plus-generator-ui is very rich in functions, even for ui can be customized and modified. If you need to customize the UI, after cloning the code, enter the frontend directory for corresponding extension development.

dac019825bd14aad8364a30de1f098c5.png       After the modification is completed, the static resources in src\frontend need to be compiled separately (the source code does not contain compiled pages), and executed in the src\frontend folder:

yarn install
yarn run build

 V. Summary

        The above is what I will talk about today. This article briefly introduces a self-service code generator based on Mybatis-Plus, address: mybatis-plus-generator-ui . The article explains mybatis-plus-generator-ui in detail by means of example integration, from related concepts to actual integration cases, and specific extension development introduction. If there is such a need at work, you might as well adopt this method. I hope this article is helpful to you. Welcome to guide and exchange.

 

Guess you like

Origin blog.csdn.net/yelangkingwuzuhu/article/details/128077533