前言
- 在基于 Mybatis 的开发模式中,很多开发者还会选择 Mybatis-Plus 来辅助功能开发,以此提高开发的效率。虽然 Mybatis 也有代码生成的工具,但 Mybatis-Plus 由于在 Mybatis 基础上做了一些调整,因此,常规的生成工具生成的代码还有一些不太符合预期。而且对于多数据库的支持不是很好。
- 因此,我们需要一款支持高度定制化,带图形 UI 页面,能适配多数数据库的基础程序生成框架。
文档
github地址:https://github.com/davidfantasy/mybatis-plus-generator-ui
是什么
- 它是对 mybatis-plus-generator 进行封装,通过 Web UI 快速生成兼容 Spring boot,mybatis-plus 框架的各类业务代码。提供交互式的 Web UI 用于生成兼容mybatis-plus 框架的相关功能代码,包括 Entity、Mapper、Mapper.xml、Service、Controller 等,可以自定义模板以及各类输出参数,也可通过 SQL 查询语句直接生成代码。
- mybatis-plus-generator-ui 提供以 jar 包的形式为外部项目提供服务,通过配置的数据库配置去读取数据库的配置信息,并通过 Web UI 的方式提供给开发者使用。mybatis-plus-generator-ui 支持 POSTGRE_SQL、ORACLE、DB2、MySQL、SQLSERVER 等常见的关系型数据库。
功能列表
- Table查询: 查询配置的关系型数据库表的列表查询。
- 输出配置: 对需要生成的相关代码,比如 Entity、Mapper、Servcie、Controller 等代码模板信息进行配置,用于在转换时调用。
- 项目导入: 可以导入其它项目配置好的信息给本项目使用。
- 下载模板: 支持本项目配置的模板信息下载后共享。
- 策略配置: 直接定义各种文件的生成策略。
- 模板上传: 支持从别的项目中下载模板,同上传供本项目使用。
- SQL输入上传: 支持将查询语句直接上传或者复制到输入框中。
- SQL代码生成: 基于SQL脚本生成相应的代码。
基本使用
注意事项:resources/static 下不要出现 index.html 之类的页面,否则无法访问到首页。
1. 引入依赖
<dependency>
<groupId>com.github.davidfantasy</groupId>
<artifactId>mybatis-plus-generator-ui</artifactId>
<version>2.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.18</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
2. test目录下新建启动类GeberatorUIServer
package org.example.paymentdemo.generate;
import com.github.davidfantasy.mybatisplus.generatorui.GeneratorConfig;
import com.github.davidfantasy.mybatisplus.generatorui.MybatisPlusToolsApplication;
import com.github.davidfantasy.mybatisplus.generatorui.mbp.NameConverter;
public class GeberatorUIServer {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/payment_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai";
String username = "root";
String password = "123456";
String driverClassName = "com.mysql.cj.jdbc.Driver";
String schemaName = "payment_demo";
String tablePrefix = "t_";
String basePackage = "org.example.paymentdemo";
Integer port = 8068;
GeneratorConfig config = GeneratorConfig.builder().jdbcUrl(url).userName(username).password(password).driverClassName(driverClassName)
//数据库schema,MSSQL,PGSQL,ORACLE,DB2类型的数据库需要指定
.schemaName(schemaName)
//数据库表前缀,生成entity名称时会去掉(v2.0.3新增)
.tablePrefix(tablePrefix)
// 如果需要修改entity及其属性的命名规则,以及自定义各类生成文件的命名规则,可自定义一个NameConverter实例,覆盖相应的名称转换方法,详细可查看该接口的说明:
.nameConverter(new NameConverter() {
public String serviceNameConvert(String entityName) {
return entityName + "Service";
}
})
//所有生成的 java 文件的父包名,后续也可单独在界面上设置
.basePackage(basePackage)
.port(port)
.build();
MybatisPlusToolsApplication.run(config);
}
}
3. 运行
- 在浏览器中输入访问地址 http://localhost:8068
代码层级的配置
- 在一些团队中,肯定对 Mapper 的定义为 Dao,Controller 层需要定义为 Action,通过修改代码模板 btl 的方式是可以的,还有一种方式是通过调整内部映射的方式来进行修改。主要使用的类是 NameConverter。
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 {
/**
* 自定义Entity.java的类名称(文件名等于类名加固定后缀)
*
* @param tableName 表名称
* @return 转换后的实体类名称
*/
default String entityNameConvert(String tableName, String tablePrefix) {
if (Strings.isNullOrEmpty(tableName)) {
return "";
}
if (!StrUtil.isBlank(tablePrefix) && tableName.startsWith(tablePrefix)) {
tableName = tableName.substring(tablePrefix.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 entityName) {
return entityName + "Mapper";
}
/**
* 自定义Mapper.xml的文件名称(无需添加后缀名)
*/
default String mapperXmlNameConvert(String entityName) {
return entityName + "Mapper";
}
/**
* 自定义Service.java的类名称(文件名等于类名加固定后缀)
*/
default String serviceNameConvert(String entityName) {
return "I" + entityName + "Service";
}
/**
* 自定义ServiceImpl.java的类名称(文件名等于类名加固定后缀)
*/
default String serviceImplNameConvert(String entityName) {
return entityName + "ServiceImpl";
}
/**
* 自定义Controller.java的类名称(文件名等于类名加固定后缀)
*/
default String controllerNameConvert(String entityName) {
return entityName + "Controller";
}
/**
* 用户自定义生成文件的文件名
*
* @param fileType 用户自定义的文件类型
* @param entityName 数据表关联的entity的名称
* @return 生成的文件的文件名,需要添加必要的文件后缀
*/
default String customFileNameConvert(String fileType, String entityName) {
return entityName + fileType;
}
}
mybatis相关配置
1. 配置文件
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
type-aliases-package: com.example.paymentdemo.entity
2. 主启动类添加@MapperScan("org.example.paymentdemo.mapper") 注解
@SpringBootApplication
@MapperScan("org.example.paymentdemo.mapper")
public class PaymentDemoApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentDemoApplication.class, args);
}
}