4.mybatis-plus 代码生成器(旧)

gitee源码:https://gitee.com/zhisuhuajian/code-generater/tree/master

4.1 导入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<!--freemarker模板引擎-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>
<!--mybatis-plus代码生成器-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.0</version>
</dependency>  

4.2 编写使用类

CodeGenerator

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
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;
 * @author hollow
public class CodeGenerator {
     * 读取控制台内容
    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.isNotBlank(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");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("hollow");
        //生成后是否打开资源管理器
        gc.setOpen(false);
        //service命名风格:默认以I开头表示service接口,例如IAccountService,个人不习惯,此处是为了去掉I开头
        gc.setServiceName("%sService");
        gc.setEntityName("%sDO");
        gc.setMapperName("%sDao");
        //主键生成策略,这里的效果是: @TableId(value = "id", type = IdType.AUTO)
        gc.setIdType(IdType.ASSIGN_UUID);
        //xml 开启BaseResultMap
        gc.setBaseResultMap(true);
        //xml 开启BaseColumnList
        gc.setBaseColumnList(true);
        //日期格式
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/parking?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF8&allowPublicKeyRetrieval=true");
        dsc.setSchemaName("tb");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        //数据库类型
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName();
        pc.setParent("com.hollow.codegenerater");
        pc.setMapper("dao");
        pc.setEntity("entity"); //存实体类的包名
        pc.setService("service"); //存service业务类接口的包名
        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/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getMapperName() + StringPool.DOT_XML;
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //数据库表映射到实体的命名策略 - 下划线转驼峰
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体的命名策略 - 下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        //使用lombok
        strategy.setEntityLombokModel(true);
        //@RestController
        strategy.setRestControllerStyle(true);
        //去掉表名的特点前缀:一般数据块表名=项目名称+真实表名
        strategy.setTablePrefix("tb" + "_");
        // 公共父类
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();

自用命名规范,符合阿里巴巴规范标准

4.3 运行使用

按要求输入
在这里插入图片描述
结果图

controller & dao
在这里插入图片描述

entity

service

mapper

4.mybatis-plus 代码生成器(旧)4.1 导入依赖&lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt; &lt;version&gt;3.4.0&lt;/version&gt;&lt;/dependency&gt;&lt;!--freemarker模板引擎--&gt;
mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目码 :mybatis-plus-generator.zip 在mybatis plus 升级到3.5.1版本之后,代码生成器依然可以使用【点我传送】,但是既然官方已经出了新的版本的代码生成器,说明代码生成器已经不再维护了,以后的主流肯定是新的代码生成器,那话不多说,下面上新的代码生成器的示例。 maven 依赖示例,可以根据情况自行变更版本 <!-- mysql 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId
<meta charset="utf-8"> <title>蓝盒子</title> <style>body { margin: 0; padding: 0;background: black; overflow: hidden;}</style> <script
文章目录一、入门介绍:二、代码案例:1、引入MyBatis-Plus相关依赖(在搭建完成的springboot项目的基础上)2、编写MyBatis-Plus代码生成器代码三、效果演示: 使用之前先了解MyBatis-Plus官方文档的介绍: 代码生成器(新) 一、入门介绍: 通过了解官方文档后,实现代码生成器需要配置六大步骤: DataSourceConfig(数据配置,通过该配置,指定需要生成代码的具体数据库) StrategyConfig(数据库表配置,通过该配置,可指定需要生成哪些表或者
今天我们来浅谈一下MyBatisPlus代码生成器,今天是教会大家怎么使用,并不会讲一下太底层的东西,使用MyBatisPlus代码生成器需要的前置知识。 MyBatis MyBatisPlus MySQL 我这里使用的是最新版本的MyBatisPlus的代码生成器3.5.1版本,这个版本不兼容历史版本,版本的代码生成器使用方法也差不多,小伙伴们可以看一下官方文档。 重点开始了!!!,我这里是在SpringBoot项目中使用MyBatisPlus,其他项目中使用也是同理 第一步(引入依赖):这一步
可以理解为对模板进行填空,主要包括:生成代码器、全局配置、数据配置、包配置、策略配置。当然中间还有其它很多配置未涉及。 import com.baomidou.mybatisplus.annotation.DbType; import.
MyBatis-Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上增加了许多实用的功能。其中,MyBatis-Plus代码生成器是其中非常实用的一部分,可以帮助开发者快速生成 MyBatis 的 Mapper、Model、Service、Controller 等代码,提高开发效率。 MyBatis-Plus 代码生成器使用步骤如下: 1. 引入 MyBatis-Plus 依赖 在项目的 pom.xml 文件中引入 MyBatis-Plus 的依赖,可以通过 Maven 或 Gradle 进行引入。 2. 配置代码生成器 在项目的配置文件中,添加 MyBatis-Plus 代码生成器的配置,包括数据配置、生成文件路径配置等。 3. 运行代码生成器 在项目中,运行 MyBatis-Plus 代码生成器,根据配置生成对应的代码文件。 4. 可选:自定义模板 如果默认的模板无法满足需求,可以自定义模板,然后在配置文件中指定自定义模板路径。 总的来说,MyBatis-Plus 代码生成器是一个非常实用的工具,可以帮助开发者快速生成基础代码,提高开发效率。