阿超
>
mybatis-plus代码生成器(二)
没有斗争就没有功绩,没有功绩就没有奖赏,而没有行动就没有生活——别林斯基
之前写过一篇,不过当时是快照版直接跑在源码环境里,今天我们使用最新的api
,并且使用GAV
引入
1 2 3 4 5
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency>
|
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package com.ruben;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.TemplateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.junit.jupiter.api.Test;
public class GeneratorTests {
private static final String TABLE_NAME = "user_info";
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai"; private static final String USERNAME = "root"; private static final String PASSWORD = "root";
private static final String AUTHOR = "<achao1441470436@gmail.com>";
private static final String PACKAGE_NAME = "com.ruben";
private static final String OUTPUT_DIR = "D:\\file\\tmp\\generator\\output";
private static final String TEMPLATE_PATH = "./static/generator/";
@Test public void generate() { System.out.println(OUTPUT_DIR); FastAutoGenerator.create(new DataSourceConfig.Builder(JDBC_URL, USERNAME, PASSWORD)) .globalConfig(builder -> builder.author(AUTHOR).fileOverride().outputDir(OUTPUT_DIR)) .packageConfig(builder -> builder.parent(PACKAGE_NAME)) .strategyConfig(builder -> builder
.controllerBuilder().enableRestStyle() .serviceBuilder() .entityBuilder() .superClass(PACKAGE_NAME + ".pojo.common.BaseEntity") .addSuperEntityColumns("is_deleted", "gmt_create", "gmt_modified", "tenant_id") .enableActiveRecord() .idType(IdType.AUTO) .naming(NamingStrategy.underline_to_camel) .columnNaming(NamingStrategy.underline_to_camel) .enableLombok() .enableChainModel()) .templateConfig(builder -> builder.disable(TemplateType.XML) .controller(TEMPLATE_PATH + "controller.java.vm") .entity(TEMPLATE_PATH + "entity.java.vm") .mapper(TEMPLATE_PATH + "mapper.java.vm") .service(TEMPLATE_PATH + "service.java.vm") .serviceImpl(TEMPLATE_PATH + "serviceImpl.java.vm") ) .execute(); } }
|
以及自己使用的controller
的模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| package ${package.Controller};
#if(${restControllerStyle}) import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruben.pojo.common.Result; import ${package.Entity}.${entity}; import ${package.Service}.${table.serviceName}; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; #else import org.springframework.stereotype.Controller; #end #if(${superControllerClassPackage}) import ${superControllerClassPackage}; #end
#if(${restControllerStyle}) @RestController #else @Controller #end @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end") #if(${kotlin}) class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else #if(${superControllerClass}) public class ${table.controllerName} extends ${superControllerClass} { #else public class ${table.controllerName} { #end #set($serviceBeanName = $table.serviceName.substring(1,2).toLowerCase() + $table.serviceName.substring(2)) #set($entityName = $entity.substring(0,1).toLowerCase() + $entity.substring(1))
@Resource private ${table.serviceName} ${serviceBeanName};
@GetMapping public Result get(@RequestParam("id") Long id){ return Result.ok().data(${entity}.builder().build().setId(id).selectById()); }
@GetMapping("page") public Result page(Page<${entity}> page, ${entity} ${entityName}){ return Result.ok().data(${entityName}.selectPage(page,Wrappers.lambdaQuery(${entityName}))); }
@PostMapping public Result insert(@RequestBody @Validated ${entity} ${entityName}){ ${entityName}.insert(); return Result.ok(); }
@PatchMapping public Result updateSelective(@RequestBody @Validated ${entity} ${entityName}){ ${entityName}.updateById(); return Result.ok(); }
@DeleteMapping public Result delete(@RequestParam("id") Long id){ ${entity}.builder().build().setId(id).deleteById(); return Result.ok(); }
}
#end
|
entity
的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| package ${package.Entity};
#foreach($pkg in ${table.importPackages}) import ${pkg}; #end #if(${swagger2}) import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; #end #if(${entityLombokModel}) import lombok.*; #if(${chainModel}) import lombok.experimental.Accessors; #end #end
#if(${entityLombokModel}) @Data @Builder @NoArgsConstructor @AllArgsConstructor #if(${chainModel}) @Accessors(chain = true) #end #if(${superEntityClass}) @EqualsAndHashCode(callSuper = true) #else @EqualsAndHashCode(callSuper = false) #end #end #if(${table.convert}) @TableName("${table.name}") #end #if(${swagger2}) @ApiModel(value = "${entity}对象", description = "$!{table.comment}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} extends Model<${entity}> { #else public class ${entity} implements Serializable{ #end
#if(${entitySerialVersionUID}) private static final long serialVersionUID=1L; #end ## ---------- BEGIN 字段循环遍历 ---------- #foreach($field in ${table.fields})
#if(${field.keyFlag}) #set($keyPropertyName=${field.propertyName}) #end #if("$!field.comment" != "") #if(${swagger2}) @ApiModelProperty(value = "${field.comment}") #else
#end #end #if(${field.keyFlag}) ## 主键 #if(${field.keyIdentityFlag}) @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO) #elseif(!$null.isNull(${idType}) && "$!idType" != "") @TableId(value = "${field.annotationColumnName}", type = IdType.${idType}) #elseif(${field.convert}) @TableId("${field.annotationColumnName}") #end ## 普通字段 #elseif(${field.fill}) ## ----- 存在字段填充设置 ----- #if(${field.convert}) @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill}) #else @TableField(fill = FieldFill.${field.fill}) #end #elseif(${field.convert}) @TableField("${field.annotationColumnName}") #end ## 乐观锁注解 #if(${field.versionField}) @Version #end ## 逻辑删除注解 #if(${field.logicDeleteField}) @TableLogic #end private ${field.propertyType} ${field.propertyName}; #end ## ---------- END 字段循环遍历 ----------
#if(!${entityLombokModel}) #foreach($field in ${table.fields}) #if(${field.propertyType.equals("boolean")}) #set($getprefix="is") #else #set($getprefix="get") #end
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}) { #end this.${field.propertyName} = ${field.propertyName}; #if(${chainModel}) return this; #end } #end ## --foreach end--- #end ## --end of #if(!${entityLombokModel})--
#if(${entityColumnConstant}) #foreach($field in ${table.fields}) public static final String ${field.name.toUpperCase()} ="${field.name}";
#end #end #if(${activeRecord} && !${superEntityClass} ) @Override protected Serializable pkVal(){ #if(${keyPropertyName}) return this.${keyPropertyName}; #else return null; #end }
#end #if(!${entityLombokModel}) @Override public String toString() { return "${entity}{" + #foreach($field in ${table.fields}) #if($!{foreach.index}==0) "${field.propertyName}=" + ${field.propertyName} + #else ", ${field.propertyName}=" + ${field.propertyName} + #end #end "}"; } #end }
|