导入依赖
<!--mp-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mp auto code-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
编写自动生成器,然后运行
public class AutoCode {
public static void main(String[] args) {
//构建生成器对象
AutoGenerator mpg = new AutoGenerator();
//配置策略
//1.全局配置
//2.将全局配置给生成器
GlobalConfig gc = new GlobalConfig();
//获得当前目录
String projectPath = System.getProperty("user.dir");
//将代码生成到当前路径下的src/main/java下
gc.setOutputDir(projectPath+"/src/main/java");
gc.setAuthor("Strike");//设置代码作者
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);
//3.设置数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//4.包的配置(各个包的包名位置)
PackageConfig pc = new PackageConfig();
pc.setModuleName("blog");
pc.setParent("com.strike");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
// pc.setController("controller");
mpg.setPackageInfo(pc);
//5.策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("m_blog");//设置要映射的表名
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
// strategyConfig.setLogicDeleteFieldName("xxx");//逻辑删除的字段
// TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);//自动填充
// TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT);
// ArrayList<TableFill> tableFills = new ArrayList<>();
// tableFills.add(gmtCreate);
// tableFills.add(gmtModified);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategyConfig);
//执行
mpg.execute();
}
}