返回

助你入门:手把手教你使用代码生成器构建 Springboot 项目

后端

代码生成器的魔力:让你的 Springboot 开发如虎添翼

代码生成器的妙用

对于 Java 开发者,特别是 Springboot 项目的开发者而言,代码生成器绝对是你的秘密武器。它可以帮助你快速创建项目、生成代码,节省大量时间和精力。同时,它还能保证代码的质量和一致性,让你的项目更加可靠和易于维护。

选择一款适合你的代码生成器

在选择代码生成器时,SpringBoot 版本较低的版本即可,这里推荐三个常用的代码生成器:

  • MyBatis Plus 代码生成器: 一款功能强大的代码生成器,支持多种数据库,使用简单,生成代码质量高。
  • JPA 代码生成器: 专为 JPA 开发者设计的代码生成器,支持多种数据库,生成代码与 JPA 规范完全兼容。
  • Spring Data JPA 代码生成器: 基于 Spring Data JPA 的代码生成器,支持多种数据库,生成代码与 Spring Data JPA 规范完全兼容。

实战演练:一步一步构建 Springboot 项目

1. 环境准备:

  • 安装 JDK 8 或以上版本。
  • 安装 Maven 3 或以上版本。
  • 安装 Springboot CLI 工具。

2. 新建项目:

  • 打开终端,输入命令:spring init project-name,其中project-name为你项目的名称。
  • 等待项目创建完成,进入项目目录。

3. 集成代码生成器:

  • 在项目根目录下,创建一个新的包,如com.example.generator
  • 在该包下,创建一个新的类,如Generator
  • Generator类中,添加以下代码:
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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;

public class Generator {
    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("你的名字");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/你的数据库名");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("你的数据库用户名");
        dsc.setPassword("你的数据库密码");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.example");
        pc.setModuleName("你的模块名");
        mpg.setPackageInfo(pc);

        // 模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setService("templates/service.java");
        templateConfig.setServiceImpl("templates/serviceImpl.java");
        templateConfig.setController("templates/controller.java");
        templateConfig.setEntity("templates/entity.java");
        templateConfig.setMapper("templates/mapper.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.setInclude("你的表名");
        strategy.setSuperEntityClass("你父类的实体类");
        strategy.setSuperEntityColumns("id");
        mpg.setStrategy(strategy);
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

4. 运行代码生成器:

  • 在终端中,进入项目根目录。
  • 输入命令:mvn compile,编译项目。
  • 输入命令:mvn package,打包项目。
  • 输入命令:java -jar target/project-name-1.0-SNAPSHOT.jar,运行项目。

代码生成器的无限可能

通过代码生成器,你可以在几分钟内创建 Springboot 项目并生成代码,极大地提高了开发效率。同时,代码生成器还能保证代码的质量和一致性,让你的项目更加可靠和易于维护。赶紧尝试一下,让代码生成器成为你的开发利器吧!

常见问题解答

1. 代码生成器生成的所有代码都必须使用吗?

不,你可以根据自己的需要选择使用哪些代码。

2. 代码生成器能否生成所有的代码,包括控制器、服务和模型?

是的,代码生成器可以生成所有必要的代码,包括控制器、服务和模型。

3. 如何自定义代码生成器生成的代码?

你可以修改代码生成器中的模板文件来自定义生成的代码。

4. 代码生成器是否支持不同的数据库?

是的,代码生成器支持多种数据库,包括 MySQL、Oracle、SQL Server 和 PostgreSQL。

5. 如何获得代码生成器的最新版本?

你可以从代码生成器的官方网站下载最新版本。