前言

  1. 代码生成器顾名思义就是为我们生成一些代码,省去了我们一些时间。
  2. MyBatis-Plus 的代码生成器可以生成 Entity、Mapper、Mapper XML、Service、Controller 模块代码。

须知

  1. MyBatis-Plus 从 3.0.3 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖,才能实现代码生成器功能。

玩熟 MyBatis-Plus 代码生成器

1.新建 MyBatis-Plus 代码生成器项目

  1. 使用 Spring 脚手架创建 SpringBoot 项目,如果不太熟悉 IDEA 快速生成 SpringBoot 项目,可以先看下面一篇博客,几分钟就搞定。
    SpringBoot 快速入门

2.添加代码生成器依赖

 

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.2.0</version>
</dependency>

3.添加模板引擎依赖

  1. MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。
  2. 下面三个依赖任选其一
    <!-- https://mvnrepository/artifact/org.apache.velocity/velocity-engine-core -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.2</version>
    </dependency>
    
    <!-- https://mvnrepository/artifact/org.freemarker/freemarker -->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.30</version>
    </dependency>
    
    <!-- https://mvnrepository/artifact/com.ibeetl/beetl -->
    <dependency>
        <groupId>com.ibeetl</groupId>
        <artifactId>beetl</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
    
  3. 如果我们选择了非默认引擎,需要在 AutoGenerator 中 设置模板引擎。
    AutoGenerator generator = new AutoGenerator();
    
    // set freemarker engine
    generator.setTemplateEngine(new FreemarkerTemplateEngine());
    
    // set beetl engine
    generator.setTemplateEngine(new BeetlTemplateEngine());
    
    // set custom engine (reference class is your custom engine class)
    generator.setTemplateEngine(new CustomTemplateEngine());
    
    // other config
    ...
    

4.完整pom.xml

  1. 代码生成器需要依赖数据库表,所以也需要 MySQL 驱动包
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
      
        <groupId>cn.zwq</groupId>
        <artifactId>mybatis-plus-auto-generator</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>mybatis-plus-auto-generator</name>
        <description>mybatis-plus-auto-generator</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
        
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--MyBatis-Plus代码生成器需要的依赖,开始-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.2.0</version>
            </dependency>
            
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
            </dependency>
            
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <!--MyBatis-Plus代码生成器需要的依赖,结束-->
        </dependencies>
    </project>
    

5.全局配置


6.数据库信息配置

7.包配置

8.策略配置

  1. 配置根据哪张表生成代码

9.完整 MyBatis-Plus 代码生成器代码

MyBatis-Plus 代码生成器所有配置都可以使用 Java Config 完成,不需要单独在 XML 配置。

package com.kll.inventory.mapper;

import com.baomidou.mybatisplus.annotation.DbType;

import com.baomidou.mybatisplus.annotation.IdType;

import com.baomidou.mybatisplus.generator.AutoGenerator;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;

import com.baomidou.mybatisplus.generator.config.GlobalConfig;

import com.baomidou.mybatisplus.generator.config.PackageConfig;

import com.baomidou.mybatisplus.generator.config.StrategyConfig;

import com.baomidou.mybatisplus.generator.config.rules.DateType;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**

* @author zxk

* @version 创建时间:2021年1月29日 上午10:32:34

* 类说明

*/

public class SggCodeGenerator {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        // 1、创建代码生成器

        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置

        GlobalConfig gc = new GlobalConfig();

        String projectPath = System.getProperty("user.dir");

        gc.setOutputDir(projectPath + "/src/main/java");

        gc.setAuthor("zxk");

        gc.setOpen(false); //生成后是否打开资源管理器

        gc.setFileOverride(false); //重新生成时文件是否覆盖

        gc.setServiceName("%sService"); //去掉Service接口的首字母I

        gc.setIdType(IdType.ID_WORKER_STR); //主键策略

        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型

        gc.setSwagger2(false);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置

        DataSourceConfig dsc = new DataSourceConfig();

        dsc.setUrl("jdbc:mysql://11.1.1.111:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false");

        dsc.setDriverName("com.mysql.cj.jdbc.Driver");

        dsc.setUsername("root");

        dsc.setPassword("root");

        dsc.setDbType(DbType.MYSQL);

        mpg.setDataSource(dsc);

        // 4、包配置

        PackageConfig pc = new PackageConfig();

        pc.setModuleName(null); //模块名

        pc.setParent("com.ceshi.inventory");

        pc.setController("controller");

        pc.setEntity("model");

        pc.setService("service");

        pc.setMapper("mapper");

        mpg.setPackageInfo(pc);

        // 5、策略配置

        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude("sc_invoice_detail");//对那一张表生成代码

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略

        strategy.setTablePrefix("sc_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略

        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器

        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行

        mpg.execute();

    }

}


 

运行代码生成器代码

  1. 主要运行其中的 main 方法即可,然后就自动根据哪张表生成代码了。

总结

  1. 代码生成器的全部代码都在这篇博客当中了,没有那块代码是缺漏,相信大家看完这篇博客之后就能玩熟代码生成器了。

备注:

mybatis-plus代码生成的时候报异常

Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Context

原因:缺少org.apache.velocity的依赖
解决办法:在pom文件添加依赖即可

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>

 

转载来源:
作者:JTravler
链接:https://www.jianshu/p/9d8ab1bb84bb
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

更多推荐

MyBatis-Plus 代码生成器超详细讲解