mybatis-plus-generator automatically generates code

mybatis-plus-generator automatically generates code

Dependency package

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--The following dependencies are extensions and plug-ins-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-annotation</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-core</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>

mybatis-plus-generator automatically generates code

After modifying the package name, author, path, and jdbc configuration, run the main() method and enter the table name. Separate multiple tables with commas.

jdbc configuration. Need to modify the database address: port/library name. If useSSL is true, simple passwords (such as 12345) cannot pass verification.
path. PATH. Change it to the project path, usually the directory where /src/main/java is located.

import com.baomidou.mybatisplus.annotation.FieldFill;
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.TableFill;
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;

/**
 * Generate the entity, mapper, service, controller corresponding to the database
 * Modify package name, author, path, jdbc configuration.
 * Run the main() method and enter the table name. Multiple tables are separated by commas.
 */
public class MybatisResourcesGenerator {


    /**
     * Package names. Modify yourself
     */
    private static final String PACKAGE = "com.example.demo";
    /**
     * author. Modify yourself
     */
    public static final String AUTHOR = "xx";
    /**
     * If the output path is incorrect, change PATH to the project path, which is usually the directory where /src/main/java is located.
     */
    private static final String PATH = "abcdefg";

    /**
     * jdbc configuration. Modify yourself.
     *
    * The database address needs to be modified here: port/library name. If useSSL is true, simple passwords (such as 12345) cannot pass verification.
     */
    
    private static final String JDBC_MYSQL = "jdbc:mysql://database address:port/library name?useUnicode=true & amp;characterEncoding=utf-8 & amp;allowMultiQueries=true & amp;useSSL=false & amp;serverTimezone=Asia /Shanghai & amp;useTimezone=true";
    private static final String MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String USER_NAME = "Account";
    private static final String PASSWORD = "Password";

    private static final String TABLE_PREFIX = "tb_";
    private static final String ENTITY_NAME = "Entity";
    private static final String ENTITY_PACKAGE = "domain";
    private static final String SERVICE_NAME = "Service";
    
    private static final String SERVICE_PACKAGE = "service";
    private static final String SERVICE_IMPL_PACKAGE = "service.impl";

    private static final String MAPPER_PACKAGE = "dao";
    private static final String RESOURCES_MAPPER_XML = "/src/main/resources/mapper/";




        /**
     * Generate the entity, mapper, service, controller corresponding to the database
     * Modify package name, author, path, jdbc configuration.
     * Run the main() method and enter the table name. Multiple tables are separated by commas.
     */
    public static void main(String[] args) {
        // Code generator
        AutoGenerator mpg = new AutoGenerator();

        //Get the project root directory
        String projectPath = System.getProperty("user.dir");
        System.out.println("projectPath=" + projectPath);

        //Global configuration
        GlobalConfig gc = getGlobalConfig(projectPath);
        mpg.setGlobalConfig(gc);

        //Data source configuration
        DataSourceConfig dsc = getSourceConfig();
        mpg.setDataSource(dsc);

        // package configuration
        PackageConfig pc = getPackageConfig();
        mpg.setPackageInfo(pc);

        // Custom configuration
        InjectionConfig cfg = getInjectionConfig(projectPath);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        //Strategy configuration
        StrategyConfig strategy = getStrategy();

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    /**
     * If the output path is incorrect, modify PATH
     * @param projectPath
     * @return
     */
    public static GlobalConfig getGlobalConfig(String projectPath) {
        GlobalConfig gc = new GlobalConfig();
        //If the output path is incorrect, modify PATH
        gc.setOutputDir(projectPath + "/" + PATH + "/src/main/java");

        //Author name
        gc.setAuthor(AUTHOR);
        gc.setOpen(false);
        gc.setServiceName("%s" + SERVICE_NAME);
        gc.setEntityName("%s" + ENTITY_NAME);
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        gc.setFileOverride(true);
        return gc;
    }

    /**
     * Data source configuration
     * @return
     */
    public static DataSourceConfig getSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(JDBC_MYSQL);
        // dsc.setSchemaName("public");
        dsc.setDriverName(MYSQL_JDBC_DRIVER);
        dsc.setUsername(USER_NAME);
        dsc.setPassword(PASSWORD);
        return dsc;
    }

    /**
     * Package configuration
     * @return
     */
    public static PackageConfig getPackageConfig() {
        PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("Module Name"));
        pc.setParent(PACKAGE);
        pc.setEntity(ENTITY_PACKAGE);
        pc.setService(SERVICE_PACKAGE);
        pc.setServiceImpl(SERVICE_IMPL_PACKAGE);
        pc.setMapper(MAPPER_PACKAGE);
        return pc;
    }

    /**
     * Custom configuration
     * @param projectPath
     * @return
     */
    public static InjectionConfig getInjectionConfig(String projectPath) {
        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) {
                //Customize input file name
                return projectPath + "/" + PATH + RESOURCES_MAPPER_XML
                         + tableInfo.getMapperName() + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    /**
     * Policy configuration
     * @return
     */
    public static StrategyConfig getStrategy() {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
// strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude(scanner("table name"));
// strategy.setSuperEntityColumns("id_");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(TABLE_PREFIX);
        strategy.setLogicDeleteFieldName("is_delete");
        strategy.setVersionFieldName("op_version");

        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_time", FieldFill.UPDATE));
        strategy.setTableFillList(tableFillList);
        return strategy;
    }

    /**
     * <p>
     * Read the console content
     *</p>
     */
    public static String[] scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter" + tip + ":");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                if(ipt.contains(",")) {
                    return ipt.split(",");
                } else {
                    return new String[] {ipt};
                }
            }
        }
        throw new MybatisPlusException("Please enter the correct one" + tip + "!");
    }
}