其他分享
首页 > 其他分享> > Mybatis-plus 代码生成器生成多模块项目代码(新版)

Mybatis-plus 代码生成器生成多模块项目代码(新版)

作者:互联网

Mybatis-Plus 代码生成器生成多模块项目代码(新版)

引言

最近想做个新的个人项目,看到Mybatis-Plus代码生成器出新版了,就去试了下。

依赖

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.5.1</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>3.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.31</version>
    </dependency>

代码

package com.mk.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MK
 * @date 2022/4/21
 */
public class CodeGenerator {
    /**
     * 项目路径
     */
    private static final String PARENT_DIR = System.getProperty("user.dir");
    /**
     * 基本路径
     */
    private static final String SRC_MAIN_JAVA = "/src/main/java/";
    /**
     * xml路径
     */
    private static final String XML_PATH = PARENT_DIR + "/tweeter-http/src/main/resources/mappers";
    /**
     * entity路径
     */
    private static final String ENTITY_PATH = PARENT_DIR + "/tweeter-domain/src/main/java/com/mk/entity";
    /**
     * mapper路径
     */
    private static final String MAPPER_PATH = PARENT_DIR + "/tweeter-dao/src/main/java/com/mk/mapper";
    /**
     * service路径
     */
    private static final String SERVICE_PATH = PARENT_DIR + "/tweeter-service/src/main/java/com/mk/service";
    /**
     * serviceImpl路径
     */
    private static final String SERVICE_IMPL_PATH = PARENT_DIR + "/tweeter-service/src/main/java/com/mk/service/impl/";
    /**
     * controller路径
     */
    private static final String CONTROLLER_PATH = PARENT_DIR + "/tweeter-http/src/main/java/com/mk/controller";

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://106.65.148.222:3306/blog",
                        "root",
                        "MKmysql")
                // 全局配置
                .globalConfig(builder -> builder
                        .author("MK")
                        .enableSwagger()
                )
                // 包配置
                .packageConfig(builder -> builder
                        .parent("")
                        .xml("mappers")
                        .entity("com.mk.entity")
                        .mapper("com.mk.dao")
                        .service("com.mk.service")
                        .serviceImpl("com.mk.service.impl")
                        .controller("com.mk.controller")
                        .pathInfo(getPathInfo())
                )
                // 策略配置
                .strategyConfig(builder -> builder
                        // entity
                        .entityBuilder()
                        .fileOverride()
                        .enableChainModel()
                        .fileOverride()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .logicDeleteColumnName("is_delete")
                        .idType(IdType.ASSIGN_ID)
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        // controller
                        .controllerBuilder()
                        .fileOverride()
                        .enableRestStyle()
                        .formatFileName("%sController")
                        // service
                        .serviceBuilder()
                        .fileOverride()
                        .superServiceClass(IService.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImp")
                        // mapper
                        .mapperBuilder()
                        .fileOverride()
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .superClass(BaseMapper.class)
                        .formatMapperFileName("%sDao")
                        .formatXmlFileName("%sXml")
                        .enableMapperAnnotation()
                )
                // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    /**
     * 获取路径信息map
     *
     * @return {@link Map< OutputFile, String> }
     * @author MK
     * @date 2022/4/21 21:21
     */
    private static Map<OutputFile, String> getPathInfo() {
        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.entity, ENTITY_PATH);
        pathInfo.put(OutputFile.mapper, MAPPER_PATH);
        pathInfo.put(OutputFile.service, SERVICE_PATH);
        pathInfo.put(OutputFile.serviceImpl, SERVICE_IMPL_PATH);
        pathInfo.put(OutputFile.controller, CONTROLLER_PATH);
        pathInfo.put(OutputFile.xml, XML_PATH);
        return pathInfo;
    }
}

效果截图

image

ps

选啥模板引擎就用啥模板的依赖,官方默认的模板不符合自己的需求可以根据自己的需求改下自己弄模板
官方模板路径:com\baomidou\mybatis-plus-generator\3.5.2\mybatis-plus-generator-3.5.2.jar!\templates
文档:https://baomidou.com/pages/981406/#数据库配置-datasourceconfig

标签:代码生成,service,import,mk,baomidou,PATH,Mybatis,plus,com
来源: https://www.cnblogs.com/mqkblog/p/16177453.html