该生成器是基于mybatis-plus的基础上根据个人在实际生产中所遇到过的各种设计而进行思考扩展的。为了提供更好地扩展性,使用者只需提供自定义的模板即可生成对应的文件,也可使用默认模板。目前模板语言只支持freemarker,因为作为一个生成工具个人认为无需考虑性能与使用哪种模板语言上的问题也没有对模板这方面进行深究,个人对freemarker也只是略懂(遇到不懂的表达式自己也是百度一下,无需深学),但也足以作为生成所需。项目地址:maven版,依赖类生成版。通过依赖注入生成的版本之后可能会废弃,仅供研究。虽然觉得功能已足够,但还是希望使用者们发现扩展性强的功能可以提一下建议,采纳后会尽快更新版本。该文章可能不会随功能更新而同步更新,所以具体功能更新信息可到版本更新信息查看。
目前可选择的生成器很多,个人目前所接触过的有tk、mybatis源生、mybatis-plus、jOOQ的生成器,各个项目基本都是基于持久层框架而选择相应的生成器的,这也使得生成器对应的持久层框架适配性最好。当然,扩展性强的也有,mybatis-plus是个人认为以上几个生成器中扩展性最好的,缺点是需要添加依赖,生成器类无法为每个项目重用,特别是在多模块项目中修改量较大。以下是个人基于mybatis-plus生成器的功能与个人的思考扩展后集成为maven生成器插件的主要功能:
io.github.wilson-he
generator-maven-plugin
LATEST
io.github.test
root
tiger
MYSQL
generate
io.github.wilson-he
generator-maven-plugin
LATEST
io.github.test
root
tiger
MYSQL
relation
detail
/templates/custom-entity.java.ftl
true
/templates/manager.ftl
manager
Manager
%sDAO
generate
create table user_base
(
id int auto_increment
primary key,
username varchar(45) null,
password varchar(45) null,
create_time timestamp null,
update_time timestamp null,
is_delete tinyint default 1 not null comment '删除(YES:0-未删除,NO:1-已删除)',
status varchar(10) null comment '状态(ENABLE-启用,DISABLE-禁用)'
);
create table user_detail
(
id int not null
primary key,
sex char null,
age tinyint null,
create_time timestamp default CURRENT_TIMESTAMP null,
update_time timestamp default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
is_delete char default '0' not null,
user_id int null
);
package ${package.Entity};
<#list table.importPackages as pkg>
import ${pkg};
#list>
<#if swagger2>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#if>
<#if table.hasEnums>import ${package.Constant}.${entity}Constant;#if>
<#if entityLombokModel>
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
#if>
/**
*
* ${table.comment!}
*
*
* @author ${author}
* @since ${date}
*/
<#if entityLombokModel>
@Data
<#if superEntityClass??>
@EqualsAndHashCode(callSuper = true)
<#else>
@EqualsAndHashCode(callSuper = false)
#if>
@Accessors(chain = true)
#if>
<#if table.convert>
@TableName("${table.name}")
#if>
<#if swagger2>
@ApiModel("${table.comment!}")
#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}>#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#else>
public class ${entity} implements Serializable {
#if>
<#-- ---------- BEGIN 字段循环遍历 ---------->
<#list table.fields as field>
<#if field.keyFlag>
<#assign keyPropertyName="${field.propertyName}"/>
#if>
<#if field.comment!?length gt 0>
<#if swagger2>
@ApiModelProperty("${field.comment}")
<#else>
/**
* ${field.comment}
*/
#if>
#if>
<#if field.keyFlag>
<#-- 主键 -->
<#if field.keyIdentityFlag>
@TableId(value = "${field.name}", type = IdType.AUTO)
<#elseif idType??>
@TableId(value = "${field.name}", type = IdType.${idType})
<#elseif field.convert>
@TableId("${field.name}")
#if>
<#-- 普通字段 -->
<#elseif field.fill??>
<#-- ----- 存在字段填充设置 ----->
<#if field.convert>
@TableField(value = "${field.name}", fill = FieldFill.${field.fill})
<#else>
@TableField(fill = FieldFill.${field.fill})
#if>
<#elseif field.convert>
@TableField("${field.name}")
#if>
<#-- 乐观锁注解 -->
<#if (versionFieldName!"") == field.name>
@Version
#if>
<#-- 逻辑删除注解 -->
<#if (logicDeleteFieldName!"") == field.name>
@TableLogic
#if>
private ${field.propertyType} ${field.propertyName};
#list>
<#------------ END 字段循环遍历 ---------->
<#if !entityLombokModel>
<#list table.fields as field>
<#if field.propertyType == "boolean">
<#assign getprefix="is"/>
<#else>
<#assign getprefix="get"/>
#if>
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
<#if entityBuilderModel>
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<#else>
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
#if>
this.${field.propertyName} = ${field.propertyName};
<#if entityBuilderModel>
return this;
#if>
}
#list>
#if>
<#if entityColumnConstant>
<#list table.fields as field>
public static final String ${field.name?upper_case} = "${field.name}";
#list>
#if>
<#if activeRecord>
@Override
protected Serializable pkVal() {
<#if keyPropertyName??>
return this.${keyPropertyName};
<#else>
return null;
#if>
}
#if>
<#if !entityLombokModel>
@Override
public String toString() {
return "${entity}{" +
<#list table.fields as field>
<#if field_index==0>
"${field.propertyName}=" + ${field.propertyName} +
<#else>
", ${field.propertyName}=" + ${field.propertyName} +
#if>
#list>
"}";
}
#if>
<#list table.fields as field>
<#if field.constantField>
public Object get${field.propertyName?cap_first}Dict() {
return ${entity}Constant.${field.propertyName?cap_first}.MAP.get(this.${field.propertyName});
}
#if>
#list>
}
package ${package.Constant};
import com.google.common.collect.ImmutableMap;
import java.util.Map;
/**
* ${entity}常量类
*
* @author ${author}
* @since ${date}
*/
public interface ${entity}Constant {
<#list table.fields as field>
<#if field.constantField>
/**
* ${field.comment}
*/
interface ${field.propertyName?capFirst} {
<#list field.fieldEnums as fieldEnum>
/**
* ${fieldEnum.comment}
*/
${field.columnType.type} ${fieldEnum.key} = ${fieldEnum.value};
#list>
<#if field.fieldEnums?size lt 6>
Map<${field.columnType.type}, String> MAP = ImmutableMap.of(
<#list field.fieldEnums as fieldEnum>
<#if fieldEnum_has_next>
${fieldEnum.key}, "${fieldEnum.comment}",
<#else >
${fieldEnum.key}, "${fieldEnum.comment}");
#if>
#list>
<#else>
Map
package ${package.Mapper};
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
/**
*
* ${table.comment!}Mapper 接口
*
*
* @author ${author}
* @since ${date}
*/
<#if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
}
#if>
<#if baseResultMap>
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
#if>
#list>
<#list table.commonFields as field><#--生成公共字段 -->
#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
#if>
#list>
#if>
<#list table.commonFields as field>
${field.name},
#list>
${table.fieldNames}
package ${package.Service};
import ${package.Entity}.${entity};
/**<#if table.comment??>${"\n"} *
* ${entity}-${table.comment!}业务接口
*
* #if>
* @author ${author}
* @since ${date}
*/
<#if kotlin>
interface ${table.serviceName} : ${superServiceClass}<${entity}>
<#else>
public interface ${table.serviceName} {
}
#if>
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**<#if table.comment??>${"\n"} *
* ${entity}-${table.comment!}业务接口
*
* #if>
* @author ${author}
* @since ${date}
*/
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
<#else>
public class ${table.serviceImplName} implements ${table.serviceName}{
@Resource
private ${table.mapperName} ${table.mapperName?uncap_first};
}
#if>
package ${package.Controller};
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
#if>
/**
*
* <#if table.comment??><#if table.comment?ends_with("表")>${table.comment?replace("表","控制器")}<#else>${table.comment}#if>#if>
*
*
* @author ${author}
* @since ${date}
*/
@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
#if>
@PostMapping("/")
public Object add(@Validated @RequestBody Object vo){
return null;
}
@GetMapping("/")
public Object get(@ApiParam(name = "id", value = "<#if table.comment??><#if table.comment?ends_with("表")>${table.comment?replace("表","")}<#else>${table.comment}#if>#if>id") @RequestParam String id){
return null;
}
@GetMapping("/page")
public Object page(@ApiParam(name = "page", value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
@ApiParam(name = "size", value = "每页返回数", defaultValue = "15") @RequestParam(defaultValue = "15") Integer size){
return null;
}
@PutMapping("/")
public Object update(@Validated @RequestBody Object vo){
return null;
}
@DeleteMapping("/")
public Object delete(@ApiParam(name = "id", value = "<#if table.comment??><#if table.comment?ends_with("表")>${table.comment?replace("表","")}<#else>${table.comment}#if>#if>id") @RequestParam String id){
return null;
}
}
#if>
package ${package.Manager};
/**<#if table.comment??>${"\n"} *
* ${entity}-${table.comment!}业务接口
*
* #if>
* @author ${author}
* @since ${date}
*/
public class ${entity}Manager {
}
觉得工具不错的希望可以顺手star一下或donate一下*.*!