mybatis-plus自动生成代码(简洁版本)
package com.netft;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
/**
* 代码自动生成器
*/
public class FengCode {
public static void main(String[] args) {
// 构建一个代码生成器对象
AutoGenerator generator = new AutoGenerator();
// 配置策略
// 1.全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("ft"); //设置作者
gc.setOpen(false); // 是否打开资源管理器
gc.setFileOverride(false); // 是否覆盖
gc.setServiceName("%sService"); // 去Service的I前缀
gc.setIdType(IdType.ID_WORKER_STR); // string类型的主键id 雪花算法
gc.setDateType(DateType.ONLY_DATE); // 日期的类型
gc.setSwagger2(false);
generator.setGlobalConfig(gc);
// 2. 设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/mp?serverTimezone=UTC&characterEncoding=utf8&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
generator.setDataSource(dsc);
// 3. 包的配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.netft");
pc.setEntity("pojo"); // 实体包名
pc.setMapper("mapper"); // 持久层包名
pc.setService("service"); // 服务层包名
pc.setController("controller");
generator.setPackageInfo(pc);
// 4. 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix("tb_"); // 去掉表明前缀
strategy.setNaming(NamingStrategy.underline_to_camel); // 包下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 列名下划线转驼峰
strategy.setEntityLombokModel(true); // 是否使用lombok
strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除
// // 自动填充配置
// TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
// TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
// ArrayList<TableFill> tableFills = new ArrayList<>();
// tableFills.add(createTime);
// tableFills.add(updateTime);
// strategy.setTableFillList(tableFills);
// // 乐观锁的配置
strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true); // 开启restful风格
// strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
strategy.setInclude("tb_user"); // 逆向工程使用的表 如果要生成多个,这里可以传入String[]
generator.setStrategy(strategy);
// 执行
generator.execute();
}
}
生成效果: