From ad9dc21e0f87ab932e651c39a7adc86152b76c4a Mon Sep 17 00:00:00 2001 From: yanghuanglin Date: Fri, 5 Jan 2024 10:34:25 +0800 Subject: [PATCH] init --- README.md | 16 +- pom.xml | 70 +++++++ .../optima/cemis/base/entity/BaseEntity.java | 26 +++ src/test/java/GeberatorUIServer.java | 55 ++++++ src/test/resources/application.yml | 3 + .../resources/codetpls/controller.java.btl | 90 +++++++++ src/test/resources/codetpls/dto.btl | 53 ++++++ src/test/resources/codetpls/entity.java.btl | 173 ++++++++++++++++++ src/test/resources/codetpls/mapper.java.btl | 26 +++ src/test/resources/codetpls/mapper.xml.btl | 41 +++++ src/test/resources/codetpls/mapperMethods.btl | 10 + src/test/resources/codetpls/resultMap.btl | 3 + src/test/resources/codetpls/service.java.btl | 20 ++ .../resources/codetpls/serviceImpl.java.btl | 28 +++ 14 files changed, 613 insertions(+), 1 deletion(-) create mode 100644 pom.xml create mode 100644 src/main/java/com/optima/cemis/base/entity/BaseEntity.java create mode 100644 src/test/java/GeberatorUIServer.java create mode 100644 src/test/resources/application.yml create mode 100644 src/test/resources/codetpls/controller.java.btl create mode 100644 src/test/resources/codetpls/dto.btl create mode 100644 src/test/resources/codetpls/entity.java.btl create mode 100644 src/test/resources/codetpls/mapper.java.btl create mode 100644 src/test/resources/codetpls/mapper.xml.btl create mode 100644 src/test/resources/codetpls/mapperMethods.btl create mode 100644 src/test/resources/codetpls/resultMap.btl create mode 100644 src/test/resources/codetpls/service.java.btl create mode 100644 src/test/resources/codetpls/serviceImpl.java.btl diff --git a/README.md b/README.md index e2a1a37..f67a9d5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # code-generator -基于mybatis-plus-generator-ui,用于生成项目的java基础代码,包括Service、Entity、Mapper、Controller等 \ No newline at end of file +基于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/)进行配置与代码生成 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..abe82ec --- /dev/null +++ b/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + com.optima.cemis + generator + 0.0.1-SNAPSHOT + generator + code-generator + + 1.8 + 2.7.18 + 2.0.5 + 8.0.33 + 1.18.30 + 3.5.5 + 2.16.1 + 2.10.5 + + + + com.github.davidfantasy + mybatis-plus-generator-ui + ${mybatis-plus-generator-ui.version} + test + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + provided + + + org.projectlombok + lombok + ${lombok.version} + provided + + + com.baomidou + mybatis-plus + ${mybatis-plus.version} + provided + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + provided + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + provided + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + provided + + + io.springfox + springfox-swagger-ui + ${springfox-swagger-ui.version} + provided + + + diff --git a/src/main/java/com/optima/cemis/base/entity/BaseEntity.java b/src/main/java/com/optima/cemis/base/entity/BaseEntity.java new file mode 100644 index 0000000..7f7185a --- /dev/null +++ b/src/main/java/com/optima/cemis/base/entity/BaseEntity.java @@ -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 extends Model> { + + @TableId("id") + private Serializable id; + + @Override + public Serializable pkVal() { + return id; + } +} diff --git a/src/test/java/GeberatorUIServer.java b/src/test/java/GeberatorUIServer.java new file mode 100644 index 0000000..a09b204 --- /dev/null +++ b/src/test/java/GeberatorUIServer.java @@ -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) + //数据库schema,MSSQL,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 map = new HashMap<>(); + map.put("mapperAnnotationClass", org.apache.ibatis.annotations.Mapper.class); + return map; + }) + //所有生成的java文件的父包名,后续也可单独在界面上设置 + .basePackage(parent) + .port(8068) + .build(); + MybatisPlusToolsApplication.run(config); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..148f815 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,3 @@ +spring: + mvc: + static-path-pattern: /** diff --git a/src/test/resources/codetpls/controller.java.btl b/src/test/resources/codetpls/controller.java.btl new file mode 100644 index 0000000..750113f --- /dev/null +++ b/src/test/resources/codetpls/controller.java.btl @@ -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; + +/** + *

+ * ${table.comment!} 前端控制器 + *

+ * + * @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> 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 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()), "更新成功"); + } + <% } %> +} +<% } %> diff --git a/src/test/resources/codetpls/dto.btl b/src/test/resources/codetpls/dto.btl new file mode 100644 index 0000000..476e85a --- /dev/null +++ b/src/test/resources/codetpls/dto.btl @@ -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} + + <% } %> + <% } %> + "}"; + } +<% } %> +} diff --git a/src/test/resources/codetpls/entity.java.btl b/src/test/resources/codetpls/entity.java.btl new file mode 100644 index 0000000..0f0aeef --- /dev/null +++ b/src/test/resources/codetpls/entity.java.btl @@ -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; +<% } %> +<% } %> + +/** + *

+ * ${table.comment!} + *

+ * + * @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} + + <% } %> + <% } %> + "}"; + } +<% } %> +} diff --git a/src/test/resources/codetpls/mapper.java.btl b/src/test/resources/codetpls/mapper.java.btl new file mode 100644 index 0000000..222ab15 --- /dev/null +++ b/src/test/resources/codetpls/mapper.java.btl @@ -0,0 +1,26 @@ +package ${package.Mapper}; + +import ${package.Entity}.${entity}; +import ${superMapperClassPackage}; +<% if(mapperAnnotationClass!=null){ %> +import ${mapperAnnotationClass.name}; +<% } %> + +/** + *

+ * ${table.comment!} Mapper 接口 + *

+ * + * @author ${author} + * @since ${date} + */ +<% if(mapperAnnotationClass!=null){ %> +@${mapperAnnotationClass.simpleName} +<% } %> +<% if(kotlin){ %> +interface ${table.mapperName} : ${superMapperClass}<${entity}> +<% }else{ %> +public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { + +} +<% } %> diff --git a/src/test/resources/codetpls/mapper.xml.btl b/src/test/resources/codetpls/mapper.xml.btl new file mode 100644 index 0000000..0c42a38 --- /dev/null +++ b/src/test/resources/codetpls/mapper.xml.btl @@ -0,0 +1,41 @@ + + + + +<% if(enableCache){ %> + + + +<% } %> +<% if(baseResultMap){ %> + + +<% for(field in table.fields){ %> + <% /** 生成主键排在第一位 **/ %> + <% if(field.keyFlag){ %> + + <% } %> +<% } %> +<% for(field in table.commonFields){ %> + <% /** 生成公共字段 **/ %> + +<% } %> +<% for(field in table.fields){ %> + <% /** 生成普通字段 **/ %> + <% if(!field.keyFlag){ %> + + <% } %> +<% } %> + +<% } %> +<% if(baseColumnList){ %> + + +<% for(field in table.commonFields){ %> + ${field.columnName}, +<% } %> + ${table.fieldNames} + + +<% } %> + diff --git a/src/test/resources/codetpls/mapperMethods.btl b/src/test/resources/codetpls/mapperMethods.btl new file mode 100644 index 0000000..38ac229 --- /dev/null +++ b/src/test/resources/codetpls/mapperMethods.btl @@ -0,0 +1,10 @@ +<% if(elementType == 'select'){ %> + <% if(config.resultMap != null){ %> + + <% }%> + ${sql} + +<% } %> \ No newline at end of file diff --git a/src/test/resources/codetpls/resultMap.btl b/src/test/resources/codetpls/resultMap.btl new file mode 100644 index 0000000..9bf3536 --- /dev/null +++ b/src/test/resources/codetpls/resultMap.btl @@ -0,0 +1,3 @@ + +<% for(field in config.fields){ %><% } %> + \ No newline at end of file diff --git a/src/test/resources/codetpls/service.java.btl b/src/test/resources/codetpls/service.java.btl new file mode 100644 index 0000000..c3f6388 --- /dev/null +++ b/src/test/resources/codetpls/service.java.btl @@ -0,0 +1,20 @@ +package ${package.Service}; + +import ${package.Entity}.${entity}; +import ${superServiceClassPackage}; + +/** + *

+ * ${table.comment!} 服务类 + *

+ * + * @author ${author} + * @since ${date} + */ +<% if(kotlin){ %> +interface ${table.serviceName} : ${superServiceClass}<${entity}> +<% }else{ %> +public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { + +} +<% } %> diff --git a/src/test/resources/codetpls/serviceImpl.java.btl b/src/test/resources/codetpls/serviceImpl.java.btl new file mode 100644 index 0000000..ee2eb6b --- /dev/null +++ b/src/test/resources/codetpls/serviceImpl.java.btl @@ -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; + +/** + *

+ * ${table.comment!} 服务实现类 + *

+ * + * @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}<% } %> { + +} +<% } %>