Mybatis-Plus代码生成器-FreeMarker引擎

代码生成操作步骤

  1. 引入依赖
  2. 添加配置
  3. ftl文件
  4. 编写配置类

配置类编写内容

  • new 代码生成器AutoGenerator
  • 全局配置GlobalConfig
  • 数据源配置DataSourceConfig
  • 包配置PackageConfig
  • 模板配置TemplateConfig
  • 策略配置StrategyConfig
  • 切面配置InjectionConfig(可不要,配置额外输出文件。如:SpringCloud的Feign接口)
  • FreeMarker引擎配置
  • execute执行

FreeMarker模板介绍

Free Marker模板对象属性
对象 属性/方法 作用 用法
package package.Entity 获取包名 package ${package.Entity};
package.Mapper 获取Mapper包名 package ${package.Mapper};
package.Controller ${package.Controller}
table table.importPackages
table.comment 表描述 ${table.comment!}
table.convert
table.name 表名 ${table.name}
table.mapperName mapper名称 ${table.mapperName}
table.controllerName ${table.controllerName}
table.fields 表字段list ${table.fields}
superMapperClass mapper父类 ${superMapperClass}
superMapperClassPackage mapper父类包 import ${superMapperClassPackage}
entity 实体类名 ${entity}
date 日期 ${date}
author 作者 ${author}
field field.comment 字段描述 ${field.comment}
field.keyFlag
field.keyIdentityFlag
field.name 字段名 ${field.name}
field.convert
field.propertyType 字段属性
field.capitalName
field.propertyName 字段属性名
FreeMarker模板控制语句
语句 条件 用法
if 为类,要加??

<#if superControllerClass??>

    public class ${table.controllerName} extends ${superControllerClass} {

<#else>

为Boolean值

<#if kotlin>

    interface ${table.mapperName} : ${superMapperClass}<${entity}>

<#else>

字符串

<#if (logicDeleteFieldName!"") == field.name>

    @TableLogic

数字类型

<#if field_index==0>

    "${field.propertyName}=" + ${field.propertyName} +

<#else>

assign 定义变量

<#assign getprefix="get"/> // 定义

${getprefix} // 使用

list 遍历list

<#list table.fields as field>

    <#if field_index==0>

        "${field.propertyName}=" + ${field.propertyName} +

    <#else>

        ", ${field.propertyName}=" + ${field.propertyName} +

   

其他函数
名称 介绍 用例
upper_case 转大写 ${field.name?upper_case}
length 字符串长度

<#if field.comment!?length gt 0>

    /** * ${field.comment} */

gt 大于

<#if field.comment!?length gt 0>

    /** * ${field.comment} */

uncap_first 首字母小写 private ${table.serviceName} ${table.serviceName?uncap_first};
cap_first 首字母大写

ftl配置文件

Mybatis-Plus代码生成器-FreeMarker引擎_第1张图片

package ${cfg.appService};

import ${package.Entity}.${entity};
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
 * 

* ${table.comment!} dubbo接口类 *

* * @author ${author} * @since ${date} */ <#if kotlin> interface ${cfg.appServiceName} <#else> public interface ${cfg.appServiceName} { /** * 根据id获取数据 * * @param id * @return ${entity} */ ${entity} findById(String id); /** * 分页查询 * * @param page * @param query * @return */ IPage<${entity}> selectPage(Page<${entity}> page, ${entity} query); } package ${package.Controller}; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ${package.Entity}.${entity}; import ${package.Service}.${table.serviceName}; <#if restControllerStyle> import org.springframework.web.bind.annotation.RestController; <#else> import org.springframework.stereotype.Controller; <#if superControllerClassPackage??> import ${superControllerClassPackage}; /** * ${table.comment!} 前端控制器 * * @author ${author} * @since ${date} */ <#if restControllerStyle> @RestController <#else> @Controller @RequestMapping("<#if package.ModuleName??>/${package.ModuleName}/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}") <#if kotlin> class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() <#else> <#if superControllerClass??> public class ${table.controllerName} extends ${superControllerClass} { <#else> public class ${table.controllerName} { private Logger logger = LoggerFactory.getLogger(${table.controllerName}.class); @Autowired private ${table.serviceName} ${table.serviceName?uncap_first}; /** * 根据id查询数据 * * @param id * @return */ @RequestMapping(value = {"/findById/{id}"}, method = {RequestMethod.GET}) public ${entity} findById(@PathVariable("id") String id) { return ${table.serviceName?uncap_first}.getById(id); } } package ${package.Entity}; <#list table.importPackages as pkg> import ${pkg}; <#if entityLombokModel> /** *

* ${table.comment!} *

* * @author ${author} * @since ${date} */ <#if table.convert> @TableName("${table.name}") <#if superEntityClass??> public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}> { <#elseif activeRecord> public class ${entity} extends Model<${entity}> { <#else> public class ${entity} implements Serializable { private static final long serialVersionUID = 1L; <#-- ---------- BEGIN 字段循环遍历 ----------> <#list table.fields as field> <#if field.keyFlag> <#assign keyPropertyName="${field.propertyName}"/> <#if field.comment!?length gt 0> /** * ${field.comment} */ <#if field.keyFlag> <#-- 主键 --> <#if field.keyIdentityFlag> @TableId(value = "${field.name}") <#elseif idType??> @TableId(value = "${field.name}") <#elseif field.convert> @TableId("${field.name}") <#-- 普通字段 --> <#elseif field.fill??> <#-- ----- 存在字段填充设置 -----> <#if field.convert> @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) <#else> @TableField(fill = FieldFill.${field.fill}) <#elseif field.convert> @TableField("${field.name}") <#-- 乐观锁注解 --> <#if (versionFieldName!"") == field.name> @Version <#-- 逻辑删除注解 --> <#if (logicDeleteFieldName!"") == field.name> @TableLogic private ${field.propertyType} ${field.propertyName}; <#------------ END 字段循环遍历 ----------> <#--set get 方法生成--> <#list table.fields as field> <#if field.propertyType == "boolean"> <#assign getprefix="is"/> <#else> <#assign getprefix="get"/> 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}) { this.${field.propertyName} = ${field.propertyName}; <#if entityBuilderModel> return this; } <#--set get 方法生成结束--> <#if entityColumnConstant> <#list table.fields as field> public static final String ${field.name?upper_case} = "${field.name}"; <#if activeRecord> @Override protected Serializable pkVal() { <#if keyPropertyName??> return this.${keyPropertyName}; <#else> return null; } <#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} + "}"; } } 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 enableCache> <#if baseResultMap> <#list table.fields as field> <#if field.keyFlag><#--生成主键排在第一位--> <#list table.commonFields as field><#--生成公共字段 --> <#list table.fields as field> <#if !field.keyFlag><#--生成普通字段 --> <#if baseColumnList> <#list table.commonFields as field> ${field.name}, ${table.fieldNames} 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}> { } package ${package.ServiceImpl}; import ${package.Entity}.${entity}; import ${package.Mapper}.${table.mapperName}; import ${package.Service}.${table.serviceName}; import ${superServiceImplClassPackage}; import org.springframework.stereotype.Service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ${table.comment!} 服务实现类 * * @author ${author} * @since ${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} { private Logger logger = LoggerFactory.getLogger(${table.serviceImplName}.class); }

 

代码生成类

package com.lingluo.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author 
 * @since 
 */
public class CodeGenerator {

    /**
     * 执行代码生成 main 方法
     *
     * @param args
     */
    public static void main(String[] args) {
        CodeGenerator.autoGenerator();
    }

    /**
     * 代码生成设置
     */
    public static void autoGenerator(){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("xxxx");
        gc.setOpen(false);
        gc.setIdType(IdType.ID_WORKER);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setServiceName("%sService");
        gc.setFileOverride(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(false);
        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/product_db?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.lingluo");
        pc.setModuleName("base");
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("/templates/entity.java");
        templateConfig.setMapper("/templates/mapper.java");
        templateConfig.setService("/templates/service.java");
        templateConfig.setServiceImpl("/templates/serviceImpl.java");
        templateConfig.setController("/templates/controller.java");
        templateConfig.setXml("/templates/mapper.xml");
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        // 可是输入多个表
        strategy.setInclude("t_alipay_orders");
        strategy.setTablePrefix("t_");
        mpg.setStrategy(strategy);

        // 配置额外的输出文件(InjectionConfig可不要这一部分,如果没有额外的输出文件,额外输出文件,如:Spring Cloud的Figen层)
        // ======================= ↓ ==============================
        String injecPath = "com.lingluo.app";
        InjectionConfig injectionConfig = new MyInjectionConfig(injecPath);
        List list = new ArrayList<>();
        list.add(new MyFileOutConfig("/templates/appService.java.ftl",  projectPath + "/src/main/java" + String.format("/%s/", injecPath.replaceAll("\\.", "/"))));
        injectionConfig.setFileOutConfigList(list);
        mpg.setCfg(injectionConfig);
        // ======================= ↑ ==============================

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    /**
     * 定义切面
     */
    static class MyInjectionConfig extends InjectionConfig {

        /**
         * 文件存储的url
         */
        private String parentPath;

        /**
         * 构造器
         *
         * @param parentPath 文件存储的url 如:com.lingluo.app
         */
        public MyInjectionConfig(String parentPath) {
            this.parentPath = parentPath;
        }

        /**
         * 设置在ftl文件中要用到的参数 如:templates/appService.java.ftl 文件中 ${cfg.appServiceName}
         */
        @Override
        public void initMap() {
            List list = this.getConfig().getTableInfoList();
            Map map = new HashMap<>(10);
            map.put("appService", parentPath);
            for (TableInfo tableInfo : list) {
                map.put("appServiceName", tableInfo.getEntityName() + "AppService");
            }
            this.setMap(map);
        }

    }

    /**
     * 自定义文件输出目录
     */
    static class MyFileOutConfig extends FileOutConfig {

        /**
         * 完整的文件输出目录
         */
        private String filePath;

        /**
         * 文件输出构造器
         *
         * @param templatePath 模板url 如:/templates/appService.java.ftl
         * @param filePath 完成的文件输出目录 如:C:\Users\luohoujian01\myProject\springboot-project/src/main/java/com/lingluo/app/
         */
        public MyFileOutConfig(String templatePath, String filePath) {
            super(templatePath);
            this.filePath = filePath;
        }

        // 自定义输出文件目录,完整的输出目录 如:C:\Users\luohoujian01\myProject\springboot-project/src/main/java/com/lingluo/app/AlipayOrdersAppService.java
        @Override
        public String outputFile(TableInfo tableInfo) {
            return filePath + String.format("%sAppService", tableInfo.getEntityName()) + ".java";
        }
    }
}

你可能感兴趣的:(笔记,java,spring,开发语言)