基于mybatis-plus的代码自动生成工具(自定义模板)

2023-05-16

  1. MyBatis-Plus是一个MyBatis框架的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。对于mybatis-plus不了解的同学,可以去MyBatis-Plus官网看看。真的能让开发效率显著提高,本文就来和大家一起分享下它的代码自动生成工具。

  2. 其实在MyBatis-Plus的官网就为我们提供了代码生成器的使用教程,它可以为完成最基本的代码生成,但是想要实现自己的效果,还需要我们自己去设置生成模板,话不多说,直接开干。

  3. 效果展示
    目录结构

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * <p>
 * 用户信息表
 * </p>
 *
 * @author 
 * @since 2019-11-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="User对象", description="用户信息表")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String ID = "id";

    public static final String USERNAME = "username";

    public static final String PASSWORD = "password";


    /**
     * 主键
     */
    @ApiModelProperty(value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 用户名
     */
    @ApiModelProperty(value = "用户名")
    @TableField("username")
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty(value = "密码")
    @TableField("password")
    private String password;

}
  1. 添加项目依赖(添加swagger和lomback是因为在生成实体类中需要用到)

    <!--mybatis plus 代码自动生成工具-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.2.0</version>
    </dependency>
    <!--freemarker模板引擎-->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.28</version>
    </dependency>
    <!--lomback-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
     <!--mysql驱动-->
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Swagger2 Begin -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- Swagger2 End -->
    
  2. 编写代码生成器代码

package cn.mybatis.plus.test;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Auther: PWJ
 * @Date: 2019/7/2 14:17
 * @Description:
 */
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //用于多个模块下生成到精确的目录下
//        String projectPath = "E:/IDEA WorkSpace/demo_springcoud2/demo_security_oauth2";
        //生成文件的输出目录
        gc.setOutputDir(projectPath + "/src/main/java");
        //开发人员
        gc.setAuthor("pwj");
        //是否打开输出目录
        gc.setOpen(false);
        // 实体属性 Swagger2 注解
        gc.setSwagger2(true);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/smart_vault?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT");
        //设置驱动
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        //用户名
        dsc.setUsername("root");
        //密码
        dsc.setPassword("root");
        //设置数据源
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //添加这个后 会以一个实体为一个模块 比如user实体会生成user模块 每个模块下都会生成三层
//        pc.setModuleName(scanner("模块名"));
        pc.setParent("cn.mybatis.plus.test");
        pc.setEntity("domain");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
//                return projectPath + "/src/main/resources/mappers/" + pc.getModuleName()
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;

            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板(如果不配置这些,则会按照官方的配置进行生成)
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        templateConfig.setEntity("templates/entity2.java");
        templateConfig.setMapper("templates/mapper2.java");
        templateConfig.setController("templates/controller2.java");
        templateConfig.setServiceImpl("templates/serviceImpl2.java");
        //设置为空则代表不需要生成
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //数据库表映射到实体的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //为实体类添加公共基类
//        strategy.setSuperEntityClass("com.wt.demo01.BaseEntity");
        //实体类采用lombok的形式
        strategy.setEntityLombokModel(true);
        //设置controller为restcontroller
        strategy.setRestControllerStyle(true);
        //【实体】是否生成字段常量(默认 false)
        strategy.setEntityColumnConstant(true);
        //是否生成实体时,生成字段配置 即在字段属性上加上@TableField("")注解
        strategy.setEntityTableFieldAnnotationEnable(true);

        // controller的公共父类
//        strategy.setSuperControllerClass("com.wt.demo01.BaseController");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        //驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        //表前缀
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        //设置模板引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}

更多的关于自动生成的配置大家可以在官网的代码生成器配置上进行了解。

  1. 自定义模板:controller2.java.ftl
package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import ${package.Service}.${table.serviceName};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@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>

    @Autowired
    private ${table.serviceName} service;

}
</#if>

  1. 自定义模板:serviceImpl2.java.ftl
package ${package.ServiceImpl};

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

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @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} {

    @Autowired
    private ${table.mapperName} mapper;

}
</#if>

  1. 自定义模板:entity2.java.ftl
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 entityLombokModel>
import lombok.*;
</#if>

/**
 * <p>
 * ${table.comment!}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if entityLombokModel>
@Data
    <#if superEntityClass??>
@EqualsAndHashCode(callSuper = true)
    <#else>
@EqualsAndHashCode(callSuper = false)
    </#if>
@Builder
@AllArgsConstructor
@NoArgsConstructor
</#if>
<#if table.convert>
@TableName("${table.name}")
</#if>
<#if swagger2>
@ApiModel(value="${entity}对象", description="${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>

<#if entitySerialVersionUID>
    private static final long serialVersionUID = 1L;
</#if>

<#if entityColumnConstant>
    <#list table.fields as field>
    public static final String ${field.name?upper_case} = "${field.name}";

    </#list>
</#if>
<#-- ----------  BEGIN 字段循环遍历  ---------->
<#list table.fields as field>
    <#if field.keyFlag>
        <#assign keyPropertyName="${field.propertyName}"/>
    </#if>

    <#if field.comment!?length gt 0>
        <#if swagger2>
    /**
     * ${field.comment}
     */
    @ApiModelProperty(value = "${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 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>
}
  1. 自定义模板:mapper2.java.ftl
package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * ${table.comment!} Mapper 接口
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Mapper
@Repository
<#if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {

}
</#if>

总结

所有的模板都可以根据个人的需求进行相应的更改,不会的都可以参照官方原来的模板进行修改,找不到的可以在com.baomidou.mybatisplus.generator包下的templates下进行寻找,附上截图。找到后将其复制到自己项目的templates目录下,在进行项目自定义修改即可(记得修改配置)。
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于mybatis-plus的代码自动生成工具(自定义模板) 的相关文章

  • 设备管理

    彩蛋 操作系统总目录 戳我 文章目录 I O设备的基本概念与分类什么是I 0设备 I O设备分类按使用特性分类按照传输速度分类按照信息交换的单位分类 I O控制器I 0设备的机械部件I 0设备的电子部件 I 0控制器 I O控制器的组成内存

随机推荐

  • 进程管理

    彩蛋 操作系统总目录 戳我 进程 进程的概念 进程的定义 程序 就是一个指令序列 程序段 数据段 PCB三部分组成了进程实体 进程映像 一般情况下 xff0c 我们把进程实体就简称为进程 例如 xff0c 所谓创建进程 xff0c 实质上是
  • 哲学家进餐问题

    文章目录 哲学家进餐问题问题描述问题分析思想三代码小结 哲学家进餐问题 问题描述 一张圆桌上坐着5名哲学家 xff0c 每两个哲学家之间的桌上摆一根筷子 xff0c 桌子的中间是一碗米饭 哲学家们倾注毕生的精力用于思考和进餐 xff0c 哲
  • 进程同步和互斥

    彩蛋 操作系统总目录 戳我 文章目录 进程互斥的硬件实现方法中断屏蔽方法TestAndSet指令Swap指令小结 信号量机制信号量机制 整型信号量信号量机制 记录型信号量小结 用信号量机制实现信号量机制实现进程互斥信号量机制实现进程同步信号
  • Java Future

    Callable Doug Lea 大师 xff0c 在1 5的杰作 span class token comment 64 see Executor 64 since 1 5 64 author Doug Lea 64 param lt
  • 布隆过滤器

    网页黑名单 垃圾邮件过滤系统 爬虫网站判重 哈希函数 特征 xff1a 典型的哈希函数都有无限的输入值域 当给哈希函数传入相同的输入值时 xff0c 返回值一样 当给哈希函数传入不同的输入值时 xff0c 返回值可能一样 xff0c 也可能
  • mysql45讲知识框架图

    1 一条SQL查询语句是如何执行的 xff1f 在有些场景下 xff0c 执行器调用一次 xff0c 在引擎内部则扫描了多行 xff0c 因此引擎扫描行数跟rows examined并不是完全相同的 主要讲述mysql 服务器和存储引擎之间
  • session 和 cookie

    一 区别 存储位置不同 xff1a session 存储在服务器端 xff1b cookie 存储在浏览器端 安全性不同 xff1a cookie 安全性一般 xff0c 在浏览器存储 xff0c 可以被伪造和修改 容量和个数限制 xff1
  • R语言中的igraph包绘制网络图

    本文转自网络 R语言中的igraph包可以很方便地画出网络图 xff0c 在社交关系分析等领域发挥重要作用 xff0c 下面介绍包中一个重要的函数graph from data frame xff08 xff09 graph from da
  • OnNewIntent四种启动模式下的调用时机

    Activity启动模式 Intent Flags taskAffinity task和back stack总结 1 singleInstance模式 第一次进入 xff1a onCreate onStart 在栈顶再次进入 xff1a o
  • windows10 Ubuntu 双系统安装及美化详细步骤

    Windows 10 Ubuntu 双系统安装踩坑实录 写在前面背景介绍 硬件介绍安装 Ubuntu下载镜像文件 制作启动盘安装步骤 安装必备软件混凝土长方形实体逾越工具安装 Nvidia 显卡驱动远程连接工具即时通讯中文输入法 美化 Ub
  • 如何优雅地停止java程序

    方法一 span class token class name System span span class token punctuation span span class token function exit span span c
  • 删除数组里面的某一个值

    Array prototype remove span class token operator 61 span function span class token punctuation span val span class token
  • ASP.NET中实现点击不同菜单项,在当前页面显示其对应内容

    疑惑 xff1a 用ASP NET开发项目时 xff0c 若想要实现这样的界面 xff1a 顶部是导航栏 xff0c 有多个菜单项 xff0c 点击不同的菜单项后 xff0c 下方显示对应的内容页面 xff1b 或者是左侧是菜单栏 xff0
  • 解决error ‘XXX‘ is not defined no-undef且项目没有eslintrc.js文件问题

    问题描述 这是因为eslint的语法校验导致的问题 xff0c 文件是通过public的index html中 lt script src 61 34 xxxxxx js 34 gt lt script gt 引入的 xff0c 没有定义全
  • CentOS7 下MariaDB安装与简单配置(最新)

    前言 MySQL和MariaDB的区别 xff1a LAMP架构盛极一时 xff0c 这离不开MySQL的免费与易用 xff0c 但是在Oracle收购了Sun之后 xff0c 很多公司开始担忧MySQL的开源前景 xff0c 而最近Ora
  • JAVA基础知识(一)

    目录 1 java的数据类型类型之间的转换 xff08 自动转型 强制转型 特殊点 xff09 2 运算符2 1算术运算符2 2逻辑运算符2 3关系运算符2 4赋值运算符2 5三目运算符2 6位运算符 3 运算符的优先级4 表达式5 转义字
  • JAVA基础知识(二)

    目录 1 循环遍历2 排序算法2 1 冒泡排序2 2 选择排序2 3插入排序 3 数组的查找3 1顺序查找3 2二分查找 xff08 折半查找 xff09 4 Arrays工具类5 可变参数 1 循环遍历 JAVA中的常用的循环遍历有for
  • JAVA基础知识(三)

    目录 1 JAVA关键字1 1关键字 xff1a private1 2关键字 xff1a this1 3关键字 xff1a static1 4关键字 xff1a super1 5关键字 xff1a final 2 继承继承中的构造方法 3
  • JAVA基础知识(四)

    目录 1 抽象类 抽象方法2 接口interface3 多态4 对象转型5 内存分析6 设计原则7 单例设计模式 1 抽象类 抽象方法 1 抽象方法和抽象类必须使用abstract修饰符修饰 xff0c 有抽象方法的类只能被定义成抽象类 x
  • 基于mybatis-plus的代码自动生成工具(自定义模板)

    MyBatis Plus是一个MyBatis框架的增强工具 xff0c 在MyBatis的基础上只做增强不做改变 xff0c 为简化开发 提高效率而生 对于mybatis plus不了解的同学 xff0c 可以去MyBatis Plus官网