Last active
May 28, 2020 08:59
-
-
Save xuanyu-h/e99fcff1e405b68213e4e3b1206b799d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CodeGenerator { | |
private static final String PROJECT_PATH = System.getProperty("user.dir"); | |
private static final String AUTHOR = "xxx"; | |
private static final String DB_URL = "xxx"; | |
private static final String DB_DRIVER_NAME = "org.postgresql.Driver"; | |
private static final String DB_USERNAME = "xxx"; | |
private static final String DB_PASSWORD = "xxx"; | |
private static final String PARENT_PACKAGE = "com.example.demo"; | |
private static final String[] EXCLUDE_TABLES = List.of("flyway_schema_history").toArray(new String[0]); | |
private static final Boolean FILE_OVERRIDE = true; | |
public static void main(String[] args) { | |
var generator = new AutoGenerator().setGlobalConfig(initGlobalConfig()) | |
.setDataSource(initDataSourceConfig()) | |
.setPackageInfo(initPackageConfig()) | |
.setTemplate(initTemplateConfig()) | |
.setStrategy(initStrategyConfig()); | |
generator.execute(); | |
} | |
private static GlobalConfig initGlobalConfig() { | |
return new GlobalConfig().setAuthor(AUTHOR) | |
.setOutputDir(PROJECT_PATH + "/src/main/java") | |
.setOpen(false) | |
.setSwagger2(false) | |
.setFileOverride(FILE_OVERRIDE) | |
.setDateType(DateType.TIME_PACK); | |
} | |
private static DataSourceConfig initDataSourceConfig() { | |
return new DataSourceConfig().setUrl(DB_URL) | |
.setDriverName(DB_DRIVER_NAME) | |
.setUsername(DB_USERNAME) | |
.setPassword(DB_PASSWORD); | |
} | |
private static PackageConfig initPackageConfig() { | |
return new PackageConfig().setParent(PARENT_PACKAGE) | |
.setEntity("model") | |
.setService("service") | |
.setServiceImpl("service.impl") | |
.setMapper("mapper"); | |
} | |
private static TemplateConfig initTemplateConfig() { | |
return new TemplateConfig() | |
.disable(TemplateType.XML) | |
.setEntity("templates/entity.java") | |
.setService("templates/service.java") | |
.setServiceImpl("templates/serviceImpl.java") | |
.setMapper("templates/mapper.java") | |
.setController("templates/controller.java"); | |
} | |
private static StrategyConfig initStrategyConfig() { | |
return new StrategyConfig().setNaming(NamingStrategy.underline_to_camel) | |
.setColumnNaming(NamingStrategy.underline_to_camel) | |
.setSuperEntityClass(BaseModel.class) | |
.setEntitySerialVersionUID(true) | |
.setEntityLombokModel(true) | |
.setChainModel(true) | |
.setEntityBooleanColumnRemoveIsPrefix(true) | |
.setRestControllerStyle(true) | |
.setExclude(EXCLUDE_TABLES); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ${package.Controller}; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
#if(${restControllerStyle}) | |
import org.springframework.web.bind.annotation.RestController; | |
#else | |
import org.springframework.stereotype.Controller; | |
#end | |
#if(${superControllerClassPackage}) | |
import ${superControllerClassPackage}; | |
#end | |
/** | |
* @author ${author} | |
* @since ${date} | |
*/ | |
#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 | |
} | |
#end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Data; | |
import lombok.EqualsAndHashCode; | |
#if(${chainModel}) | |
import lombok.experimental.Accessors; | |
#end | |
#end | |
/** | |
* @author ${author} | |
* @since ${date} | |
*/ | |
#if(${entityLombokModel}) | |
@Data | |
#if(${superEntityClass}) | |
@EqualsAndHashCode(callSuper = true) | |
#else | |
@EqualsAndHashCode(callSuper = false) | |
#end | |
#if(${chainModel}) | |
@Accessors(chain = true) | |
#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 | |
/** | |
* ${field.comment} | |
*/ | |
#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(${versionFieldName}==${field.name}) | |
@Version | |
#end | |
## 逻辑删除注解 | |
#if(${logicDeleteFieldName}==${field.name}) | |
@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}) | |
@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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ${package.Mapper}; | |
import ${package.Entity}.${entity}; | |
import ${superMapperClassPackage}; | |
/** | |
* @author ${author} | |
* @since ${date} | |
*/ | |
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.baomidou</groupId> | |
<artifactId>mybatis-plus-boot-starter</artifactId> | |
<version>3.3.2</version> | |
</dependency> | |
<dependency> | |
<groupId>com.baomidou</groupId> | |
<artifactId>mybatis-plus-generator</artifactId> | |
<version>3.3.2</version> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ${package.Service}; | |
import ${package.Entity}.${entity}; | |
import ${superServiceClassPackage}; | |
import org.springframework.stereotype.Repository; | |
/** | |
* @author ${author} | |
* @since ${date} | |
*/ | |
@Repository | |
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ${package.ServiceImpl}; | |
import ${package.Entity}.${entity}; | |
import ${package.Mapper}.${table.mapperName}; | |
import ${package.Service}.${table.serviceName}; | |
import ${superServiceImplClassPackage}; | |
import org.springframework.stereotype.Service; | |
/** | |
* @author ${author} | |
* @since ${date} | |
*/ | |
@Service | |
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment