use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project springboot-learning by lyb-geek.
the class CodeGenerator method main.
public static void main(String[] args) throws Exception {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String basePath = CodeGenerator.class.getResource("").getPath();
String projectPath = basePath.substring(0, basePath.indexOf("/target"));
// String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("lyb-geek");
gc.setOpen(false);
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
gc.setServiceName("%sService");
// gc.setSwagger2(true);// 实体属性 Swagger2 注解
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
String url = YmlUtil.getValue("spring.datasource.druid.url").toString();
String username = YmlUtil.getValue("spring.datasource.druid.username").toString();
String pwd = PropertiesUtil.INSTANCE.getProperty("password");
String publicKey = PropertiesUtil.INSTANCE.getProperty("config.decrypt.key");
String password = ConfigTools.decrypt(publicKey, pwd);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("com.github.lybgeek.orm");
pc.setEntity("model");
pc.setMapper("dao");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
// return projectPath + "/src/main/resources/mapperPlus/" + pc.getModuleName()
// + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
// }
// });
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/mybatisplus/" + tableInfo.getEntityName().toLowerCase() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(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();
// 配置自定义输出模板
// 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.github.lybgeek.orm.common.model.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// strategy.setSuperControllerClass("com.github.lybgeek.orm.controller.BaseController");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setSuperEntityColumns("id", "create_date", "update_date");
strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
// 移除表的前缀
// strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project springboot-learning by lyb-geek.
the class CodeGeneratorUtils method execute.
/**
* 代码生成逻辑
* @param codeGeneratorHelper
* @throws Exception
*/
public static void execute(CodeGeneratorHelper codeGeneratorHelper) throws Exception {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 项目路径
String projectPath = codeGeneratorHelper.getProjectPath();
if (StringUtils.isBlank(projectPath)) {
projectPath = getProjectPath();
System.out.println("-----------------项目根路径未配置将使用默认配置路径--->" + projectPath + "----------------");
}
// 全局配置
GlobalConfig gc = getGlobalConfig(codeGeneratorHelper, projectPath);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = getDataSourceConfig(codeGeneratorHelper);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = getPackageConfig(codeGeneratorHelper);
mpg.setPackageInfo(pc);
InjectionConfig cfg = getInjectionConfig(pc, codeGeneratorHelper, projectPath);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = getStrategyConfig(codeGeneratorHelper);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project FCLProject by FlowingCloudL.
the class CodeGenerator method main.
public static void main(String[] args) {
String packagePath = System.getProperty("user.dir") + "/fp-mall/fp-mall-order/src/main/java";
FastAutoGenerator.create("jdbc:mysql://localhost:3306/fp_mall_oms?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC", "root", "123456").globalConfig(builder -> builder.outputDir(packagePath).author("FlowingCloudL").enableSwagger().fileOverride()).packageConfig(builder -> builder.parent("com.fp.mall.order.quick")).strategyConfig(builder -> builder.addInclude(getTables("all")).entityBuilder().enableLombok().addTableFills(new Column("create_time", FieldFill.INSERT), new Column("update_time", FieldFill.UPDATE)).build()).templateEngine(// 使用Freemarker引擎模板,默认的是Velocity引擎模板
new FreemarkerTemplateEngine()).execute();
}
use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project springbootquickstart by Pace2Car.
the class CodeGenerator method generateByTables.
public void generateByTables(String moduleName, String... tableNames) {
// 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(false).setAuthor(AUTHOR).setOutputDir(OUTPUT_FILE).setFileOverride(false).setOpen(false).setServiceName("%sService");
// 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USER_NAME).setPassword(PASSWORD).setDriverName(DRIVER);
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setEntityLombokModel(true).setCapitalMode(true).setNaming(NamingStrategy.underline_to_camel).setColumnNaming(NamingStrategy.underline_to_camel).setInclude(tableNames);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名
return PROJECT_PATH + "/src/main/resources/mapper/" + (moduleName == null ? "" : moduleName) + "/" + tableInfo.getEntityName() + "Mapper" + ".xml";
}
});
cfg.setFileOutConfigList(focList);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setCfg(cfg).setTemplate(templateConfig).setTemplateEngine(new FreemarkerTemplateEngine()).setPackageInfo(// 包配置
new PackageConfig().setModuleName(moduleName).setParent(PACKAGE).setController("controller").setEntity("entity")).execute();
}
use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project newBlog by 1537069101.
the class CodeGenerator method main.
public static void main(String[] args) {
System.out.println("----------------------------============-----------------------------");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
// gc.setOutputDir("D:\\test");
gc.setAuthor("");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://119.91.255.208:3306/wiki?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("Password");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent("cn.zzzyuan");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
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;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
Aggregations