MybatisPlus was recently used in the project, and code generators were explored along the way

That’s why I have this blog to keep track of

1. Introduce dependencies

<! -- Code generation --> <! -- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.41.</version> </dependency> <! -- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.016.</version> </dependency> <! -- Beetl template engine --> <! -- https://mvnrepository.com/artifact/com.ibeetl/beetl -->
<dependency>
    <groupId>com.ibeetl</groupId>
    <artifactId>beetl</artifactId>
    <version>3.2. 0.RELEASE</version> </dependency> <! -- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.1816.</version>
    <scope>provided</scope>
    </dependency>
Copy the code

2. Create a parent classBasePO

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/** * can put some attributes or methods that are common to all PO **@author dujc
 * @date2020/3/7 * /
@Setter
@Getter
public class BasePO implements Serializable {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    /** * Logical delete field, 0 indicates not deleted, 1 indicates deleted */
    @TableLogic
    @TableField(select = false)
    private Long deleted = 0L;
}
Copy the code

3. Create aCodeGenerator, write the main logic

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import java.util.Scanner;

/** * Code generator * to run the main method **@author abm
 */
public class CodeGenerator {

    /** * 

* read the console contents *

*/
private 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(ipt ! =null && !ipt.isEmpty()) { returnipt; }}throw new MybatisPlusException("Please enter the correct one" + tip + "!"); } public static void main(String[] args) { // Beetl template engine BeetlTemplateEngine engine = new BeetlTemplateEngine(); // Code generator AutoGenerator mpg = new AutoGenerator(); // Global configuration GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/generate/"); gc.setAuthor("miao"); gc.setOpen(false); gc.setSwagger2(false); mpg.setGlobalConfig(gc); // Data source configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUrl("jdbc:mysql://" + scanner("Database IP :port") + "/" + scanner("Database name") + "? useUnicode=true&useSSL=false&characterEncoding=utf8"); System.out.println("Database URL:" + dsc.getUrl()); dsc.setUsername(scanner("Database account name")); dsc.setPassword(scanner("Database password")); mpg.setDataSource(dsc); / / package configuration PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("Module name")); pc.setParent("cn.miao"); mpg.setPackageInfo(pc); // Policy configuration StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperEntityClass("cn.miao.code.common.po.BasePO"); // Public fields written in the parent class strategy.setSuperEntityColumns("id"."deleted"); strategy.setInclude(scanner("Table to be generated, separated by multiple Commas").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setVersionFieldName("version"); mpg.setStrategy(strategy); mpg.setTemplateEngine(engine); mpg.execute(); }}Copy the code

4. Perform verification

5. The verification is complete