This commit is contained in:
2024-01-05 10:34:25 +08:00
parent 31121c0567
commit ad9dc21e0f
14 changed files with 613 additions and 1 deletions

View File

@@ -1,3 +1,17 @@
# code-generator
基于mybatis-plus-generator-ui用于生成项目的java基础代码包括Service、Entity、Mapper、Controller等
# 用法
1、在`main/java`下将entity基类所在包建好将基类拷贝进去
2、将`test/java/GeberatorUIServer.java`中数据库相关变量(`dbName``url``username``password`)改为实际数据库的配置
3、将`test/java/GeberatorUIServer.java`中生成类所在包的变量`parent`改为实际的包名生成的java类都在这个包下
4、将`test/java/GeberatorUIServer.java`中表名前缀`tablePrefix`改为实际表前缀生成器如果配置了去除前缀则表中以这个前缀开头的表建立java实体类时类名不会包含此前缀
5、执行`test/java/GeberatorUIServer.java`的main方法
6、浏览器访问[http://localhost:8068/](http://localhost:8068/)进行配置与代码生成

70
pom.xml Normal file
View File

@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.optima.cemis</groupId>
<artifactId>generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>generator</name>
<description>code-generator</description>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.7.18</spring-boot.version>
<mybatis-plus-generator-ui.version>2.0.5</mybatis-plus-generator-ui.version>
<mysql-connector-java.version>8.0.33</mysql-connector-java.version>
<lombok.version>1.18.30</lombok.version>
<mybatis-plus.version>3.5.5</mybatis-plus.version>
<jackson.version>2.16.1</jackson.version>
<springfox-swagger-ui.version>2.10.5</springfox-swagger-ui.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.davidfantasy</groupId>
<artifactId>mybatis-plus-generator-ui</artifactId>
<version>${mybatis-plus-generator-ui.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger-ui.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,26 @@
package com.optima.cemis.base.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 仅仅是一个基类,各个项目中可能不同,此项目中,只是用于避免因类不存在而报错
*/
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@Data
public class BaseEntity<T> extends Model<BaseEntity<T>> {
@TableId("id")
private Serializable id;
@Override
public Serializable pkVal() {
return id;
}
}

View File

@@ -0,0 +1,55 @@
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.github.davidfantasy.mybatisplus.generatorui.GeneratorConfig;
import com.github.davidfantasy.mybatisplus.generatorui.MybatisPlusToolsApplication;
import com.github.davidfantasy.mybatisplus.generatorui.mbp.NameConverter;
import java.util.HashMap;
import java.util.Map;
public class GeberatorUIServer {
/**
* 数据库相关配置
*/
private static final String dbName = "cemis-examine";
private static final String url = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&useSSL=false&characterEncoding=utf8";
private static final String username = "root";
private static final String password = "root";
private static final String driverClassName = "com.mysql.cj.jdbc.Driver";
//项目父包名service、controller等包会建在这个包下
private static final String parent = "com.optima.cemis";
//表名前缀,生成表时会删除
private static final String tablePrefix = "exam_";
public static void main(String[] args) {
GeneratorConfig config = GeneratorConfig.builder()
.jdbcUrl(url)
.userName(username)
.password(password)
.driverClassName(driverClassName)
.dateType(DateType.TIME_PACK)
//数据库schemaMSSQL,PGSQL,ORACLE,DB2类型的数据库需要指定
//.schemaName("myBusiness")
//数据库表前缀生成entity名称时会去掉(v2.0.3新增)
.tablePrefix(tablePrefix)
.nameConverter(new NameConverter() {
/**
* 自定义Service类文件的名称规则
*/
@Override
public String serviceNameConvert(String entityName) {
return entityName + "Service";
}
})
.templateVaribleInjecter(tableInfo -> {
Map<String, Object> map = new HashMap<>();
map.put("mapperAnnotationClass", org.apache.ibatis.annotations.Mapper.class);
return map;
})
//所有生成的java文件的父包名后续也可单独在界面上设置
.basePackage(parent)
.port(8068)
.build();
MybatisPlusToolsApplication.run(config);
}
}

View File

@@ -0,0 +1,3 @@
spring:
mvc:
static-path-pattern: /**

View File

@@ -0,0 +1,90 @@
<% var entityInstanceName = @cn.hutool.core.util.StrUtil.lowerFirst(table.entityName); %>
<% var serviceInstanceName = @cn.hutool.core.util.StrUtil.lowerFirst(table.serviceName); %>
package ${package.Controller};
import cn.cdhncy.cemis.common.core.util.R;
import org.springframework.web.bind.annotation.*;
<% if(!restControllerStyle){ %>
import org.springframework.stereotype.Controller;
<% } %>
<% if(isNotEmpty(superControllerClassPackage)){ %>
import ${superControllerClassPackage};
<% } %>
<% if(isNotEmpty(controllerMethods.list)){ %>
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<% } %>
<% if(isNotEmpty(controllerMethods.hasMethod)){ %>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${table.entityName};
import javax.annotation.Resource;
<% } %>
import java.io.Serializable;
/**
* <p>
* ${table.comment!} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
<% if(restControllerStyle){ %>
@RestController
<% }else{ %>
@Controller
<% } %>
@RequestMapping("<% if(isNotEmpty(package.ModuleName)){ %>/${package.ModuleName}<% } %>/<% if(isNotEmpty(controllerMappingHyphenStyle)){ %>${controllerMappingHyphen}<% }else{ %>${table.entityPath}<% } %>")
<% if(kotlin){ %>
class ${table.controllerName}<% if(isNotEmpty(superControllerClass)){ %> : ${superControllerClass}()<% } %>
<% }else{ %>
<% if(isNotEmpty(superControllerClass)){ %>
public class ${table.controllerName} extends ${superControllerClass} {
<% }else{ %>
public class ${table.controllerName} {
<% } %>
<% if(isNotEmpty(controllerMethods.hasMethod)){ %>
@Resource
private ${table.serviceName} ${serviceInstanceName};
<% } %>
<% if(isNotEmpty(controllerMethods.list)){ %>
@GetMapping(value = "/list")
public R<Page<${table.entityName}>> list(Page<${table.entityName}> page) {
return R.ok(${serviceInstanceName}.page(page), "查询成功");
}
<% } %>
<% if(isNotEmpty(controllerMethods.getById)){ %>
@GetMapping(value = "/{id}")
public R<${table.entityName}> getById(@PathVariable("id") Serializable id) {
return R.ok(${serviceInstanceName}.getById(id), "查询成功");
}
<% } %>
<% if(isNotEmpty(controllerMethods.create)){ %>
@PostMapping(value = "/create")
public R<${table.entityName}> create(${table.entityName} ${entityInstanceName}) {
${serviceInstanceName}.save(${entityInstanceName});
return R.ok(${entityInstanceName}, "保存成功");
}
<% } %>
<% if(isNotEmpty(controllerMethods.delete)){ %>
@GetMapping(value = "/delete/{id}")
public R<Serializable> delete(@PathVariable("id") Serializable id) {
${serviceInstanceName}.removeById(id);
return R.ok(id, "删除成功");
}
<% } %>
<% if(isNotEmpty(controllerMethods.update)){ %>
@PostMapping(value = "/update")
public R<${table.entityName}> update(${table.entityName} ${entityInstanceName}) {
${serviceInstanceName}.updateById(${entityInstanceName});
return R.ok(${serviceInstanceName}.getById(${entityInstanceName}.getId()), "更新成功");
}
<% } %>
}
<% } %>

View File

@@ -0,0 +1,53 @@
package ${config.pkg};
<% for(pkg in config.importPackages){ %>
import ${pkg};
<% } %>
<% if(config.enableLombok){ %>
import lombok.Data;
<% } %>
/**
* ${config.comment!}
*
* @author ${config.author}
* @since ${config.createDate}
*/
<% if(config.enableLombok){ %>
@Data
<% } %>
public class ${config.dtoName} {
<% /** -----------BEGIN 字段循环遍历----------- **/ %>
<% for(field in config.fields){ %>
<%/*字段定义*/%>
private ${field.shortJavaType} ${field.propertyName};
<% } %>
<% /** -----------END 字段循环遍历----------- **/ %>
<% if(!config.enableLombok){ %>
<% for(field in config.fields){ %>
public ${field.shortJavaType} ${field.getMethodName}() {
return ${field.propertyName};
}
public void ${field.setMethodName}(${field.shortJavaType} ${field.propertyName}) {
this.${field.propertyName} = ${field.propertyName};
}
<% } %>
<% } %>
<% if(!config.enableLombok){ %>
@Override
public String toString() {
return "${config.dtoName}{" +
<% for(field in config.fields){ %>
<% if(fieldLP.index==0){ %>
"${field.propertyName}=" + ${field.propertyName} +
<% }else{ %>
", ${field.propertyName}=" + ${field.propertyName} +
<% } %>
<% } %>
"}";
}
<% } %>
}

View File

@@ -0,0 +1,173 @@
package ${package.Entity};
<% for(pkg in table.importPackages){ %>
import ${pkg};
<% } %>
<% if(springdoc){ %>
import io.swagger.v3.oas.annotations.media.Schema;
<% }else if(swagger){ %>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
<% } %>
<% if(entityLombokModel){ %>
import lombok.Getter;
import lombok.Setter;
<% if(chainModel){ %>
import lombok.experimental.Accessors;
<% } %>
<% } %>
/**
* <p>
* ${table.comment!}
* </p>
*
* @author ${author}
* @since ${date}
*/
<% if(entityLombokModel){ %>
@Getter
@Setter
<% if(chainModel){ %>
@Accessors(chain = true)
<% } %>
<% } %>
<% if(table.convert){ %>
@TableName("${schemaName}${table.name}")
<% } %>
<% if(springdoc){ %>
@Schema(name = "${entity}", description = "$!{table.comment}")
<% }else if(swagger){ %>
@ApiModel(value = "${entity}对象", description = "${table.comment!''}")
<% } %>
<% if(isNotEmpty(superEntityClass)){ %>
public class ${entity} extends ${superEntityClass}<% if(activeRecord){ %><${entity}><%}%>{
<% }else if(activeRecord){ %>
public class ${entity} extends Model<${entity}> {
<% }else if(entitySerialVersionUID){ %>
public class ${entity} implements Serializable {
<% }else{ %>
public class ${entity} {
<% } %>
<% if(entitySerialVersionUID){ %>
private static final long serialVersionUID = 1L;
<% } %>
<% var keyPropertyName; %>
<% /** -----------BEGIN 字段循环遍历----------- **/ %>
<% for(field in table.fields){ %>
<%
if(field.keyFlag){
keyPropertyName = field.propertyName;
}
%>
<% if(isNotEmpty(field.comment)){ %>
<% if(springdoc){ %>
@Schema(description = "${field.comment}")
<% }else if(swagger){ %>
@ApiModelProperty(value = "${field.comment}")
<% }else{ %>
/**
* ${field.comment}
*/
<% } %>
<% } %>
<% if(field.keyFlag){ %>
<%
/*主键*/
%>
<% if(field.keyIdentityFlag){ %>
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
<% }else if(isNotEmpty(idType)){ %>
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
<% }else if(field.convert){ %>
@TableId("${field.annotationColumnName}")
<% } %>
<%
/*普通字段*/
%>
<% }else if(isNotEmpty(field.fill)){ %>
<% if(field.convert){ %>
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
<% }else{ %>
@TableField(fill = FieldFill.${field.fill})
<% } %>
<% }else if(field.convert){ %>
@TableField("${field.annotationColumnName}")
<% } %>
<%
/*乐观锁注解*/
%>
<% if(field.versionField){ %>
@Version
<% } %>
<%
/*逻辑删除注解*/
%>
<% if(field.logicDeleteField){ %>
@TableLogic
<% } %>
private ${field.propertyType} ${field.propertyName};
<% } %>
<% /** -----------END 字段循环遍历----------- **/ %>
<% if(!entityLombokModel){ %>
<% for(field in table.fields){ %>
<%
var getprefix ='';
if(field.propertyType=='boolean'){
getprefix='is';
}else{
getprefix='get';
}
%>
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
<% if(chainModel){ %>
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<% }else{ %>
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<% } %>
this.${field.propertyName} = ${field.propertyName};
<% if(chainModel){ %>
return this;
<% } %>
}
<% } %>
<% } %>
<% if(entityColumnConstant){ %>
<% for(field in table.fields){ %>
public static final String ${strutil.toUpperCase(field.name)} = "${field.name}";
<% } %>
<% } %>
<% if(activeRecord){ %>
@Override
public Serializable pkVal() {
<% if(isNotEmpty(keyPropertyName)){ %>
return this.${keyPropertyName};
<% }else{ %>
return super.pkVal();
<% } %>
}
<% } %>
<% if(!entityLombokModel){ %>
@Override
public String toString() {
return "${entity}{" +
<% for(field in table.fields){ %>
<% if(fieldLP.index==0){ %>
"${field.propertyName} = " + ${field.propertyName} +
<% }else{ %>
", ${field.propertyName} = " + ${field.propertyName} +
<% } %>
<% } %>
"}";
}
<% } %>
}

View File

@@ -0,0 +1,26 @@
package ${package.Mapper};
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
<% if(mapperAnnotationClass!=null){ %>
import ${mapperAnnotationClass.name};
<% } %>
/**
* <p>
* ${table.comment!} Mapper 接口
* </p>
*
* @author ${author}
* @since ${date}
*/
<% if(mapperAnnotationClass!=null){ %>
@${mapperAnnotationClass.simpleName}
<% } %>
<% if(kotlin){ %>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<% }else{ %>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
}
<% } %>

View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
<% if(enableCache){ %>
<!-- 开启二级缓存 -->
<cache type="${cacheClassName}"/>
<% } %>
<% if(baseResultMap){ %>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<% for(field in table.fields){ %>
<% /** 生成主键排在第一位 **/ %>
<% if(field.keyFlag){ %>
<id column="${field.name}" property="${field.propertyName}" />
<% } %>
<% } %>
<% for(field in table.commonFields){ %>
<% /** 生成公共字段 **/ %>
<result column="${field.name}" property="${field.propertyName}" />
<% } %>
<% for(field in table.fields){ %>
<% /** 生成普通字段 **/ %>
<% if(!field.keyFlag){ %>
<result column="${field.name}" property="${field.propertyName}" />
<% } %>
<% } %>
</resultMap>
<% } %>
<% if(baseColumnList){ %>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
<% for(field in table.commonFields){ %>
${field.columnName},
<% } %>
${table.fieldNames}
</sql>
<% } %>
</mapper>

View File

@@ -0,0 +1,10 @@
<% if(elementType == 'select'){ %>
<% if(config.resultMap != null){ %>
<select id="${config.mapperElementId}" resultMap="${config.resultMap}">
<% }%>
<% if(config.resultMap == null){ %>
<select id="${config.mapperElementId}" resultType="${config.resultType}">
<% }%>
${sql}
</select>
<% } %>

View File

@@ -0,0 +1,3 @@
<resultMap id="${config.dtoName}Map" type="${config.fullPackage}">
<% for(field in config.fields){ %><result column="${field.columnName}" property="${field.propertyName}" /><% } %>
</resultMap>

View File

@@ -0,0 +1,20 @@
package ${package.Service};
import ${package.Entity}.${entity};
import ${superServiceClassPackage};
/**
* <p>
* ${table.comment!} 服务类
* </p>
*
* @author ${author}
* @since ${date}
*/
<% if(kotlin){ %>
interface ${table.serviceName} : ${superServiceClass}<${entity}>
<% }else{ %>
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
}
<% } %>

View File

@@ -0,0 +1,28 @@
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
<% if(table.serviceInterface){ %>
import ${package.Service}.${table.serviceName};
<% } %>
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
/**
* <p>
* ${table.comment!} 服务实现类
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service
<% if(kotlin){ %>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()<% if(table.serviceInterface){ %>, ${table.serviceName}<% } %> {
}
<% }else{ %>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><% if(table.serviceInterface){ %> implements ${table.serviceName}<% } %> {
}
<% } %>