Custom Velocity Templates for MyBatisPlus Code Generation

Controller Template

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

/**
 * @className: ${table.controllerName}
 * @description: $!{table.comment}Front Controller
 * @author: ${author}
 * @date: ${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

Entity Template

package ${package.Entity};

#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import com.baomidou.mybatisplus.annotation.KeySequence;
#if(${springdoc})
import io.swagger.v3.oas.annotations.media.Schema;
#elseif(${swagger})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.*;
#if(${chainModel})
import lombok.experimental.Accessors;
#end
#end

/**
 * @description $!{table.comment}
 * @author ${author}
 * @date ${date}
 */
#if(${entityLombokModel})
@Data
#if(${chainModel})
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name.toUpperCase()}")
@KeySequence("SEQ_${schemaName}${table.name.toUpperCase()}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}Object", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
 public class ${entity} implements Serializable {
#else
public class ${entity} {
#end
#if(${entitySerialVersionUID})
    private static final long serialVersionUID = 1L;
#end

## ----------  BEGIN Field Iteration  ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${swagger})
    @ApiModelProperty("${field.comment}")
#else
    /**
     * ${field.comment}
     */
#end
#end
#if(${field.keyFlag})
## Primary Key
#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
## Regular Fields
#elseif(${field.fill})
## -----   Field Fill Configuration   ----- 
#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
## Optimistic Lock Annotation
#if(${field.versionField})
   @Version
#end
## Logical Delete Annotation
#if(${field.logicDeleteField})
   @TableLogic
#end
    private ${field.propertyType} ${field.propertyName};

#end
## ----------  END Field Iteration  ----------
#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
#end
#if(${entityColumnConstant})
#foreach($field in ${table.fields})
    public static final String ${field.name.toUpperCase()} = "${field.name}";
#end
#end
#if(${activeRecord})
    @Override
    public 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
}

Service Template

package ${package.Service};

import ${package.Entity}.${entity};
import ${superServiceClassPackage};

/**
 * @className: ${table.serviceName}
 * @description: $!{table.comment}Service Interface
 * @author: ${author}
 * @date: ${date}
 */
#if(${kotlin})
interface ${table.serviceName} : ${superServiceClass}<${entity}>
#else
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {

}
#end

ServiceImpl Template

package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;

/**
 * @className: ${table.serviceImplName}
 * @description: $!{table.comment}Service Implementation
 * @author: ${author}
 * @date: ${date}
 */
@Service
#if(${kotlin})
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

        }
#else
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

}
#end

Mapper Template

package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};
#if(${mapperAnnotation})
import org.apache.ibatis.annotations.Mapper;
#end

/**
 * @className: ${table.mapperName}
 * @description: $!{table.comment}Mapper Interface
 * @author: ${author}
 * @date: ${date}
 */
#if(${mapperAnnotation})
@Mapper
#end
#if(${kotlin})
interface ${table.mapperName} : ${superMapperClass}<${entity}>
#else
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {

}
#end

MapperXml Template



<mapper namespace="${package.Mapper}.${table.mapperName}">

#if(${enableCache})
    
    <cache type="${cacheClassName}"/>

#end
    #if(${baseResultMap})
        
        <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
    #if(${field.keyFlag})##Generate primary key first
        <id column="${field.name}" property="${field.propertyName}" />
    #end
#end
            #foreach($field in ${table.commonFields})##Generate common fields
                <result column="${field.name}" property="${field.propertyName}" />
            #end
            #foreach($field in ${table.fields})
                #if(!${field.keyFlag})##Generate regular fields
                    <result column="${field.name}" property="${field.propertyName}" />
                #end
            #end
    </resultMap>

    #end
    #if(${baseColumnList})
    
    <sql id="Base_Column_List">
        <if test="true">
#foreach($field in ${table.commonFields})
${field.columnName},
#end
            ${table.fieldNames}
        </if>
    </sql>

    #end
</mapper>

Generated Style

These templates were created to modify the output formatting of the default templates. The original templates were copied and adjusted to ensure proper alignment in the generated files. This documentation serves as a reference to prevent future loss of these custom templates.

Tags: mybatisplus Velocity Templates Code Generation java

Posted on Wed, 13 May 2026 21:34:02 +0000 by jd57