文档:https://baomidou.com/guide/generator.html
配置参数:https://baomidou.com/config/generator-config.html
依赖
<!--代码生成器--> <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.2</version> </dependency> <!-- 数据库驱动 --> <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>
最简单的代码示例
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; /** * 自动生成类 **/ public class CodeGenerator { /** * 必要的配置 */ // 生成代码的表名 private static final String TABLE_NAME = "table_person"; // 数据库相关配置 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"; // 注释显示的作者 private static final String AUTHOR = "mouday"; // 代码生成指定包名 private static final String PACKAGE_NAME = "com.example"; public static void main(String[] args) { // ======= 全局配置 ======= GlobalConfig globalConfig = new GlobalConfig(); String dir = System.getProperty("user.dir"); globalConfig.setOutputDir(dir + "/src/main/java"); // 输出目录 globalConfig.setOpen(false); // 不打开输出目录 globalConfig.setAuthor(AUTHOR); // 开发人员 // ======= 数据源配置 ======= DataSourceConfig dataSource = new DataSourceConfig(); dataSource.setDriverName(DRIVER_NAME); dataSource.setUsername(USERNAME); dataSource.setPassword(PASSWORD); dataSource.setUrl(JDBC_URL); // ======= 包配置 ======= PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(PACKAGE_NAME); // ======= 策略配置 ======= StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); // 表名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 字段 strategy.setInclude(TABLE_NAME); // 需要生成的表 strategy.setEntityLombokModel(true); // lombok模型 strategy.setRestControllerStyle(true); // Rest控制器 // 自动填充配置 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); // ======= 执行生成 ======= AutoGenerator autoGenerator = new AutoGenerator(); autoGenerator.setGlobalConfig(globalConfig); autoGenerator.setDataSource(dataSource); autoGenerator.setPackageInfo(packageConfig); autoGenerator.setStrategy(strategy); autoGenerator.execute(); } }