MybatisPlus(MP)代码生成器

导包

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
</dependency>
<!--velocity模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>

编写自动生成工具类

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.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class MybatisPlusCodeGenerator {
public static void main(String[] args) {
// 1. 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("E:\\ideapro\\mybatis_test\\mybatis-04\\src\\main\\java");//生成地址
gc.setAuthor("Your Name");//注释
gc.setOpen(false);
gc.setFileOverride(true);

// 2. 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/by?useUnicode=true&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");

// 3. 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("cn.ky");//设置父报名
pc.setEntity("entity");//pojo类
pc.setMapper("mapper");//mapper类
pc.setXml("mapper.xml");

// 4. 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude("student"); // 生成表名,多个表名用逗号分隔
strategy.setControllerMappingHyphenStyle(true);

// 5. 代码生成器
AutoGenerator mpg = new AutoGenerator();
mpg.setGlobalConfig(gc);
mpg.setDataSource(dsc);
mpg.setPackageInfo(pc);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}

这只是自动生成表所对应的类,如需使用,你还需要引入其它的配置文件