【MybatisPlus】Advanced version visualization, configurable and automatic code generation

Today, I saw someone using a more intelligent code generation tool, which is visualized and configurable. It is very convenient. Once configured, it can be used anywhere, and it will not be coupled with the project. Let me briefly talk about how to use it.

1. Introduce mybatis-plus-generator-ui

It mainly encapsulates mybatis-plus-generator, and has UI, all of which are interfaced, very convenient, and you can also customize templates and so on. Support POSTGRE_SQL, ORACLE, DB2, MySQL, SQLSERVER and other common relational databases.

2. How to use

1. Initialization

First create a new maven project by yourself
insert image description here

pom import tool:

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

New startup class: It supports the independent deployment of GeberatorUIServer as a separate spring boot project.

package com.springboot.test;

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

/**
 * @author fei.chen
 * @projectName mybatis-generator-ui
 * @date 2023/5/4下午 4:58
 */
public class GeneratorMain {
    
    
    public static void main(String[] args) {
    
    
        GeneratorConfig config = GeneratorConfig.builder().jdbcUrl("jdbc:postgresql://111.111.111.111:15432/tables")
                .userName("postgres").password("1111111").driverClassName("org.postgresql.Driver")
                // 数据库schema,POSTGRE_SQL,ORACLE,DB2类型的数据库需要指定
                .schemaName("public")
                // 如果需要修改各类生成文件的默认命名规则,可自定义一个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) + "Controller";
                    }
                }).basePackage("com.springboot.test").port(8068).build();

        MybatisPlusToolsApplication.run(config);
    }
}

2. Start

Just execute the main method directly, and specify the running port of the program as 8086 in the main, which is very similar to SpringBoot.
Request url: http://localhost:8068/

Datasheets are shown.
insert image description here

3. Generate the required code

insert image description here
insert image description here
ok Check the project and refresh it, all generated:
insert image description here
4, xml can be configured by yourself
For example, I want to add a query statement of my own in xml and generate it directly
insert image description here

insert image description here

Everything you need is generated, and it is very convenient
insert image description here

3. Configurable

1. Code configuration

The suffix Service, Controller, Entity, and FieldName can be configured on the startup class
insert image description here
to achieve custom extensions, which can be implemented by yourself.
You can view the NameConverter class:

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;
    }

}

2. Change the template for the source code

If you need to customize the UI, after cloning the code, go to the frontend directory and carry out corresponding extension development.
insert image description here

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

4. Source address

Do the rest by yourself~

  • https://github.com/davidfantasy/mybatis-plus-generator-ui

Guess you like

Origin blog.csdn.net/daohangtaiqian/article/details/130491497