编程知识 cdmana.com

Java: mybatis plus autogenerator code generator

file :https://baomidou.com/guide/generator.html
Configuration parameters :https://baomidou.com/config/generator-config.html

rely on

<!-- Code generator -->
<dependency>
  <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.0</version>
</dependency>

<!-- template engine   rely on -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.2</version>
</dependency>

<!--  Database driven  -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

The simplest code example

package com.demo.mybatis;


import com.baomidou.mybatisplus.annotation.FieldFill;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 *  Automatically generate classes 
 **/
public class CodeGenerator {
    

    /**
     *  The necessary configuration 
     */
    //  The table name of the generated code 
    private static final String TABLE_NAME = "table_person";

    //  Database related configuration 
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/data?useUnicode=true&useSSL=false&characterEncoding=utf8";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    //  The author of the comment display 
    private static final String AUTHOR = "mouday";

    //  Code generation specifies the package name 
    private static final String PACKAGE_NAME = "com.example";

    public static void main(String[] args) {
    

        // =======  Global configuration  =======
        GlobalConfig globalConfig = new GlobalConfig();
        String dir = System.getProperty("user.dir");
        globalConfig.setOutputDir(dir + "/src/main/java"); //  The output directory 
        globalConfig.setOpen(false); //  Don't open the output directory 
        globalConfig.setAuthor(AUTHOR); //  Developer 

        // =======  Data source configuration  =======
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setDriverName(DRIVER_NAME);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
        dataSource.setUrl(JDBC_URL);


        // =======  Package configuration  =======
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(PACKAGE_NAME);


        // =======  Policy configuration  =======
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);      //  Table name 
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); //  Field 
        strategy.setInclude(TABLE_NAME);     //  The table that needs to be generated 
        strategy.setEntityLombokModel(true); // lombok Model 
        strategy.setRestControllerStyle(true); // Rest controller 

        //  Auto fill configuration 
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);

        // =======  Perform build  =======
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSource);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setStrategy(strategy);
        autoGenerator.execute();
    }
}

版权声明
本文为[Peng Shiyu]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225150821739Y.html

Scroll to Top