如何使用MybatisPlusGenerator生成代码

发布时间:2023-05-19

MybatisPlusGenerator是一款MybatisPlus框架自带的代码生成工具,可以快速帮助开发者生成单表的CRUD代码,大大提高了开发效率。本文将从选用MybatisPlusGenerator的必要性、使用前的准备工作、使用MybatisPlusGenerator生成代码的步骤、生成代码后的自定义修改以及其他使用注意事项等几个方面来详细讲解如何使用MybatisPlusGenerator生成代码。

一、选用MybatisPlusGenerator的必要性

在使用Mybatis进行单表增删改查的开发过程中,我们往往需要手写大量的Mapper.xml、Mapper.java以及Entity.java等文件。当表的结构比较复杂时,手写代码的工作量很大,而且需要花费大量的时间和精力。MybatisPlusGenerator则可以自动帮我们生成这些文件,从而免去了大量繁琐的工作,提高了开发效率,减少了出错的可能性。此外,使用MybatisPlusGenerator生成的代码风格统一、规范化,可以提高代码的可读性和可维护性,是一个非常好用的代码生成工具。

二、使用前的准备工作

在使用MybatisPlusGenerator之前,我们需要先完成以下几个准备工作: 1、在项目的pom.xml文件中引入MybatisPlusGenerator依赖:

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>${mybatis-plus.version}</version>
</dependency>

2、在项目的配置文件(如application.properties)中配置数据源等信息:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

三、使用MybatisPlusGenerator生成代码的步骤

使用MybatisPlusGenerator生成代码的步骤如下: 1、编写一个代码生成器的配置类(GeneratorConfig.java),并在该类中指定需要生成代码的表名、包名等相关信息。

package com.example.demo.config;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
 * 代码生成器配置类
 */
public class GeneratorConfig {
    public static void main(String[] args) {
        // 1、创建代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("your_name");
        gc.setOpen(false);
        gc.setFileOverride(true);
        gc.setServiceName("%sService");
        gc.setIdType(IdType.ID_WORKER);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&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("");
        pc.setParent("com.example.demo");
        pc.setMapper("mapper");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("table_name"); // 指定需要生成代码的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 6、执行代码生成器
        mpg.execute();
    }
}

2、运行GeneratorConfig.java类的main()方法即可生成代码。

四、生成代码后的自定义修改

生成的代码可能并不完全符合我们的要求,我们可以对代码进行一些自定义修改。例如,我们可以修改Entity.java中的属性名称以及类型名称,使其更符合我们的习惯。

五、其他使用注意事项

1、MybatisPlusGenerator默认使用了Freemarker作为模板引擎,如果你的项目中没有引入Freemarker依赖,可以在pom.xml文件中添加如下依赖:

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.31</version>
</dependency>

2、MybatisPlusGenerator生成的Mapper接口的方法名和返回类型是根据Entity类中属性的类型和名称自动生成的,有些情况下可能需要手动修改Mapper接口。 3、在生成代码之前需要保证数据库中有需要生成代码的表。

总结

本文对如何使用MybatisPlusGenerator生成代码进行了详细的讲解,从必要性、使用前的准备工作、使用步骤、自定义修改以及其他使用注意事项等多个方面进行了阐述。MybatisPlusGenerator是一个极为方便、实用的代码生成工具,对提高开发效率,减少出错的可能性,统一代码风格具有重要的作用。