导入依赖

<!--  mybatis-plus 组件  -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


Java代码:

package cn.dafran.generator;

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

import java.util.ArrayList;

/**
* @Classname CodeGenerator
* @Author 6b92d6
* @Description 代码生成器(Velocity)
*/
public class CodeGenerator {
public static void main(String[] args) {
//需要构建一个代码自动生成器对象
AutoGenerator mpg = new AutoGenerator();
//配置策略
//1、全局配置
GlobalConfig gc= new GlobalConfig();
String projectPath = System.getProperty("user.dir"); //获取当前系统路径
gc.setOutputDir(projectPath+"/src/main/java"); //自动输出目录
gc.setAuthor("6b92d6"); //作者
gc.setOpen(false); //是否打开资源管理器
gc.setFileOverride(false); //是否覆盖原先生成的
gc.setServiceName("%sService"); //去Service的I前缀
gc.setIdType(IdType.ID_WORKER); //生成策略、默认初始算法
gc.setDateType(DateType.ONLY_DATE); //日期类型
gc.setSwagger2(true); //是否配置Swagger文档
mpg.setGlobalConfig(gc);

//2、设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://121.36.76.221:3306/db_ymyy?useSSL=false&useUnicode=true&characterEncoding=utf-8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);

//3、包的配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName("blog");
pc.setParent("cn.dafran");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setController("controller");
mpg.setPackageInfo(pc);

//4.策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user"); // 设置要映射的表名 可以同时多个表
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); // 自动生成Lombok;
strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除
//自动填充配置
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
//乐观锁
strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true); //启动驼峰命名
strategy.setControllerMappingHyphenStyle(true);
// Localhost:8080/hello_id_2 、controller字段下划线命名
mpg.setStrategy(strategy);

mpg.execute(); //执行代码构造器
}
}