博客
关于我
Mybatis-Plus 开发提速器:mybatis-plus-generator-ui 你确定不了解一下?
阅读量:798 次
发布时间:2023-02-09

本文共 4284 字,大约阅读时间需要 14 分钟。

mybatis-plus-generator-ui代码自助生成器介绍

在基于Mybatis的开发模式中,开发者常借助Mybatis-Plus辅助功能开发,以提升开发效率。然而,Mybatis的代码生成工具与Mybatis-Plus的集成存在一定的兼容性问题,且对多数据库的支持不够理想。针对这些痛点,我们需要一款支持高度定制化、具备图形UI页面以及适配多数据库的代码自助生成框架。本文将详细介绍基于Mybatis-Plus的代码自助生成器mybatis-plus-generator-ui,提供其功能特点、使用方法以及代码生成流程。

一、mybatis-plus-generator-ui是什么?

mybatis-plus-generator-ui是对现有mybatis-plus-generator框架的封装,通过Web UI提供快速生成兼容Spring Boot和Mybatis-Plus框架的各类业务代码。该工具支持生成Entity、Mapper、Mapper.xml、Service、Controller等文件,并允许开发者自定义模板和输出参数,也可以通过SQL查询语句直接生成代码。其功能涵盖表查询、输出配置、项目导入、模板下载、策略配置、模板上传、SQL输入上传以及SQL代码生成等多个方面。

二、使用方法

mybatis-plus-generator-ui以jar包形式为外部项目提供服务,通过数据库配置信息读取数据库数据,并通过Web UI供开发者使用。该工具支持PostgreSQL、Oracle、DB2、MySQL、SQLServer等多种关系型数据库。

1. Maven pom引入

在项目的pom.xml中引入mybatis-plus-generator-ui依赖:

4.0.0
com.yelang
mybatis-plus-generator-ui-case
0.0.1-SNAPSHOT
com.github.davidfantasy
mybatis-plus-generator-ui
1.4.5
org.postgresql
postgresql
42.2.25

2. 新建程序入口,以main函数的方式运行

为了便于开发和部署,mybatis-plus-generator-ui从1.4.0版本开始支持将GeberatorUIServer独立部署为一个单独的Spring Boot项目。通过页面指定目标项目根目录的方式,为多个项目提供源码生成服务。以下是实现代码示例:

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")
.nameConverter(new NameConverter() {
public String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
public String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}
})
.basePackage("com.github.davidfantasy.mybatisplustools.example")
.port(8068)
.build();
MybatisPlusToolsApplication.run(config);
}
}

3. 实例运行

运行上述main方法后,控制台将输出成功部署日志。通过浏览器访问地址http://localhost:8068/即可进入配置生成界面。

三、代码生成

1. 表查询与浏览

开发者可以直接浏览并查询数据库表信息,选择需要生成代码的模板。

2. 输出配置

内置支持Entity、Mapper、Service、Controller等6种类型代码模板配置,允许上传模板替换并修改参数。配置项按文件类型分类存储,便于管理。

3. 策略配置

灵活调整生成策略,例如文件覆盖、生成文件类型等设置,确保代码生成符合项目需求。

4. SQL配置生成

通过输入SQL语句,自动生成Mapper文件、DTO对象和ResultMap配置。

5. 代码生成

根据配置信息,通过SQL脚本生成相应的代码文件。

四、自定义扩展

1. 模板调整

在对应页面中下载模板文件,使用文本工具直接修改源代码进行定制化开发。

2. 代码层级配置

通过调整NameConverter类实现对Mapper、Service、Controller等文件名称的自定义规则。以下是NameConverter类的核心代码示例:

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 {
default String propertyNameConvert(String fieldName) {
if (Strings.isNullOrEmpty(fieldName)) {
return "";
}
if (fieldName.contains("_")) {
return StrUtil.toCamelCase(fieldName.toLowerCase());
}
return fieldName;
}
}
default String serviceNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Service";
}
default String controllerNameConvert(String tableName) {
return this.entityNameConvert(tableName) + "Action";
}

通过自定义NameConverter类,可以实现对生成文件命名规则的个性化定制,满足不同项目的开发需求。

五、总结

以上是对mybatis-plus-generator-ui代码自助生成器的详细介绍,涵盖了其功能、使用方法和代码生成流程。通过该工具,开发者可以快速生成兼容Mybatis-Plus框架的业务代码,显著提升开发效率。如需进一步了解或扩展该项目,可访问其GitHub仓库地址:https://github.com/davidfantasy/mybatis-plus-generator-ui

转载地址:http://wfffk.baihongyu.com/

你可能感兴趣的文章
modprobe
查看>>
ModStart 是一个基于 Laravel 模块化极速开发网站框架
查看>>
Modular RAG:向“平台”级演进
查看>>
module 'queue' has no attribute 'Queue'解决
查看>>
module 'requests' has no attribute 'get' python
查看>>
Module Federation在vue3中使用vue2的组件
查看>>
Module Zero之语言管理
查看>>
module ‘@babel/runtime/helpers/interopRequireDefault.js‘ is not defined报错解决方法
查看>>
ModuleNotFoundError: No module named ‘setuptools_rust‘
查看>>
ModuleNotFoundError: No module named ‘webdriver_manager‘ 错误即使在安装 webdrivermanager 之后
查看>>
ModuleNotFoundError: No module named ‘_bz2‘
查看>>
ModuleNotFoundError: No module named ‘_lzma‘
查看>>
ModuleNotFoundError:没有名为“versioneer“的模块
查看>>
ModuleNotFoundError: no module named ‘pip‘解决方法
查看>>
ModuleNotFoundError:Spyder中没有名为Pip&39;的模块
查看>>
Module加载
查看>>
MogoTemplate基本入门(Mongodb数据库基本增删改查)
查看>>
Mojarra JSF ViewState 反序列化漏洞复现
查看>>
My new English
查看>>
Mojo:比 Python 快 35000 倍的编程语言诞生!
查看>>