Boost Java Development with MyBatis-Plus Generator UI: A Complete Guide
This article introduces the MyBatis-Plus Generator UI, a web‑based tool that streamlines Spring Boot code generation for entities, mappers, services, and controllers, explains how to integrate it via Maven, configure databases, customize templates, and extend its functionality with custom name converters.
Preface
When developing with MyBatis, many developers adopt MyBatis‑Plus to improve productivity, but the built‑in code generators often lack flexibility and multi‑database support. A highly customizable, UI‑driven generator that works with most databases is therefore needed.
What is MyBatis‑Plus Generator UI?
MyBatis‑Plus Generator UI wraps the standard MyBatis‑Plus generator with a web UI, enabling rapid generation of Spring Boot‑compatible code such as Entity, Mapper, Mapper.xml, Service, and Controller. It supports custom templates, output parameters, and direct SQL‑based generation.
Key features include:
Table query : List tables from the configured relational database.
Output configuration : Define templates for Entity, Mapper, Service, Controller, etc.
Project import : Import configuration from other projects.
Template download : Share template files.
Strategy configuration : Set generation strategies such as file overwrite.
Template upload : Upload custom templates.
SQL input upload : Provide SQL statements directly.
SQL code generation : Generate code based on SQL scripts.
How to Use MyBatis‑Plus Generator UI
1. Add Maven dependency
<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 main entry class
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")
// .schemaName("myBusiness") // for POSTGRE_SQL, ORACLE, DB2
.nameConverter(new NameConverter() {
/** Custom Service class name */
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/** Custom Controller class name */
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
})
.basePackage("com.github.davidfantasy.mybatisplustools.example")
.port(8068)
.build();
MybatisPlusToolsApplication.run(config);
}
}Running the main method starts the UI on the specified port (e.g., http://localhost:8068/).
Code Generation Features
1. Table Query and Browse
Browse and select tables from the connected data source, then generate code for the chosen tables.
2. Output Configuration
Configure templates for Entity, Mapper, Service, Controller, etc., upload custom templates, and adjust parameters per file type.
3. Strategy Configuration
Define generation strategies such as file overwrite, file type selection, and other options.
4. SQL‑Based Generation
Enter a SELECT statement to automatically generate corresponding Mapper methods, DTOs, and ResultMap entries.
5. Generated Code Preview
Custom Extensions
1. Template Adjustments
Download the BTL template files, edit them with a text editor, and re‑upload to customize UI output.
2. Code‑Level Configuration via NameConverter
Implement custom naming rules for Entity, Mapper, Service, and Controller classes by extending the NameConverter interface.
/** Custom Service class name */
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
/** Custom Controller class name */
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}The full NameConverter interface provides default methods for entity, property, mapper, XML, service, and controller naming.
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;
/** Custom naming rules */
public interface NameConverter {
default String entityNameConvert(String tableName) {
if (Strings.isNullOrEmpty(tableName)) return "";
tableName = tableName.substring(tableName.indexOf(StrUtil.UNDERLINE) + 1);
return StrUtil.upperFirst(StrUtil.toCamelCase(tableName.toLowerCase()));
}
default String propertyNameConvert(String fieldName) {
if (Strings.isNullOrEmpty(fieldName)) return "";
return fieldName.contains("_") ? StrUtil.toCamelCase(fieldName.toLowerCase()) : fieldName;
}
default String mapperNameConvert(String tableName) { return entityNameConvert(tableName) + "Mapper"; }
default String mapperXmlNameConvert(String tableName) { return entityNameConvert(tableName) + "Mapper"; }
default String serviceNameConvert(String tableName) { return "I" + entityNameConvert(tableName) + "Service"; }
default String serviceImplNameConvert(String tableName) { return entityNameConvert(tableName) + "ServiceImpl"; }
default String controllerNameConvert(String tableName) { return entityNameConvert(tableName) + "Controller"; }
default String outputFileNameConvert(String fileType, String tableName) {
if (fileType.equals(Constant.FILE_TYPE_ENTITY)) return entityNameConvert(tableName) + DOT_JAVA;
if (fileType.equals(Constant.FILE_TYPE_MAPPER)) return mapperNameConvert(tableName) + DOT_JAVA;
if (fileType.equals(Constant.FILE_TYPE_MAPPER_XML)) return mapperXmlNameConvert(tableName) + DOT_XML;
if (fileType.equals(Constant.FILE_TYPE_SERVICE)) return serviceNameConvert(tableName) + DOT_JAVA;
if (fileType.equals(Constant.FILE_TYPE_SERVICEIMPL)) return serviceImplNameConvert(tableName) + DOT_JAVA;
if (fileType.equals(Constant.FILE_TYPE_CONTROLLER)) return controllerNameConvert(tableName) + DOT_JAVA;
return entityNameConvert(tableName) + fileType;
}
}Conclusion
The MyBatis‑Plus Generator UI offers a powerful, UI‑driven approach to generate boilerplate code for Spring Boot projects, supports multiple databases, and can be fully customized through template editing and custom name converters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
