mybatis plus 代码生成器 设置不生成controller文件
package com.plani.shishi;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.*;
public class MybatisPlusRun {
/**
* 包名 自定义的包名
*/
public static String packageCustom = "";
public static void main(String[] args) {
//添加表名
List<String> tables = new ArrayList<>(10);
tables.add("t_user");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("plani");
gc.setOpen(false);
//开启 BaseResultMap
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setEnableCache(true);
// 实体属性 Swagger2 注解
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/shishi?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
// ("public");
dsc.setDriverName("");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 不需要 直接注释掉
// (scanner("模块名"));
//设置包名
pc.setParent(packageCustom);
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/";
// 如果模板引擎是 velocity
// String templatePath = "/templates/";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
//控制 不生成 controller
templateConfig.setController("");
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// ("templates/");
// ();
// ();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定义继承的Entity类全称,带包名 舍弃 不需要
// ("");
strategy.setEntityLombokModel(true);
//不需要 controller 舍弃
// (true);
// 公共父类
// ("");
// 写于父类中的公共字段
// ("id");
//要添加的表
strategy.setInclude(tables.toArray(new String[]{}));
strategy.setControllerMappingHyphenStyle(true);
//表名前缀 这样生成的bean里面 就没有前缀了
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}