use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project java-example by saxingz.
the class GenCode method gen.
// /**
// * <p>
// * 读取控制台内容
// * </p>
// */
// public static String scanner(String tip) {
// Scanner scanner = new Scanner(System.in);
// StringBuilder help = new StringBuilder();
// help.append("请输入" + tip + ":");
// System.out.println(help.toString());
// if (scanner.hasNext()) {
// String ipt = scanner.next();
// if (StringUtils.isNotBlank(ipt)) {
// return ipt;
// }
// }
// throw new MybatisPlusException("请输入正确的" + tip + "!");
// }
@Test
public void gen() {
String[] tables = new String[] { "channel" };
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("saxing");
gc.setOpen(false);
gc.setFileOverride(false);
gc.setIdType(IdType.AUTO);
// 实体属性 Swagger2 注解
gc.setSwagger2(true);
gc.setDateType(DateType.ONLY_DATE);
// gc.setEntityName("%sDO");
// gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(jdbcUrl);
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(username);
dsc.setPassword(password);
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName("");
pc.setParent("org.saxing.a0041_wemedia");
pc.setEntity("domain.entity");
pc.setService("logic");
pc.setServiceImpl("logic.impl");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
this.getConfig().getTableInfoList().forEach(tableInfo -> {
String entityName = tableInfo.getEntityName();
if (!entityName.endsWith("DO")) {
// 实体类和表名不一致
tableInfo.setConvert(true);
tableInfo.setEntityName(entityName + "DO");
tableInfo.setServiceName("I" + entityName + "Logic");
tableInfo.setServiceImplName(entityName + "Logic");
}
});
}
};
// 自定义输出配置
// 如果模板引擎是 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/" + pc.getModuleName()
// + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
String entityName = tableInfo.getEntityName();
entityName = entityName.endsWith("DO") ? entityName.substring(0, entityName.length() - 2) : entityName;
return projectPath + "/src/main/resources/mapper/" + /*+ pc.getModuleName()*/
"/" + entityName + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
// 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// 此处设置为null,就不会再java下创建xml的文件夹了
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.setLogicDeleteFieldName("is_deleted");
// 用数据库自带的时间更新
// TableFill createdTime = new TableFill("created_time", FieldFill.INSERT);
// TableFill updatedTime = new TableFill("updated_time", FieldFill.INSERT_UPDATE);
// strategy.setTableFillList(Arrays.asList(createdTime, updatedTime));
// 写于父类中的公共字段
strategy.setInclude(tables);
strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.execute();
}
use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project springboot-learning by lyb-geek.
the class CodeGeneratorUtils method getInjectionConfig.
/**
* InjectionConfig配置
* @param codeGeneratorHelper
* @return
*/
private static InjectionConfig getInjectionConfig(PackageConfig pc, CodeGeneratorHelper codeGeneratorHelper, String projectPath) {
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
String templatePath = "/templates/mapper.xml.ftl";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/" + codeGeneratorHelper.getMapperXmlLoction() + "/" + pc.getModuleName().toLowerCase() + "/" + tableInfo.getEntityName() + codeGeneratorHelper.getMapperNameSuffix() + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
return cfg;
}
use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project code-generator-maven-plugin by mengweijin.
the class DefaultAutoGenerator method execute.
public void execute() {
Parameters parameters = projectInfo.getParameters();
String outputDir = FileUtil.file(projectInfo.getBaseDir(), "target/code-generator/").getAbsolutePath();
// clean directory target/code-generator
FileUtil.del(outputDir);
FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder()).globalConfig(builder -> builder.fileOverride().author(parameters.getAuthor()).enableSwagger().disableOpenDir().outputDir(outputDir).dateType(DateType.TIME_PACK).commentDate("yyyy-MM-dd")).packageConfig(builder -> builder.parent(parameters.getOutputPackage())).templateConfig((Consumer<TemplateConfig.Builder>) TemplateConfig.Builder::disable).strategyConfig(builder -> builder.addInclude(this.trimItemName(parameters.getTables())).addTablePrefix(this.trimItemName(parameters.getTablePrefix())).entityBuilder().superClass(parameters.getSuperEntityClass()).enableChainModel().enableLombok().enableTableFieldAnnotation().versionColumnName("version").versionPropertyName("version").logicDeleteColumnName("deleted").logicDeletePropertyName("deleted").naming(NamingStrategy.underline_to_camel).addSuperEntityColumns(this.generateDefaultSuperEntityColumns()).controllerBuilder().superClass(parameters.getSuperControllerClass()).enableHyphenStyle().enableRestStyle().serviceBuilder().superServiceClass(parameters.getSuperServiceClass()).superServiceImplClass(parameters.getSuperServiceImplClass()).mapperBuilder().superClass(parameters.getSuperDaoClass()).enableBaseColumnList().enableBaseResultMap()).injectionConfig(builder -> {
builder.beforeOutputFile(((tableInfo, objectMap) -> {
enhanceObjectMap(objectMap, parameters);
FileOutput.outputFile(tableInfo, objectMap, projectInfo, outputDir);
}));
}).templateEngine(TemplateEngineFactory.getTemplateEngine(this.projectInfo.getParameters().getTemplateType()));
fastAutoGenerator.execute();
}
use of com.baomidou.mybatisplus.generator.config.po.TableInfo 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.config.po.TableInfo in project mybatis-plus-plugin by kana112233.
the class GenUtil method generatorCode.
public static void generatorCode(String tableName, GenConfig genConfig) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// String projectPath = System.getProperty("user.dir");
String projectPath = genConfig.getRootFolder();
gc.setOutputDir(projectPath + File.separator + genConfig.getModuleName() + "/src/main/java");
gc.setAuthor(genConfig.getAuthor());
gc.setOpen(false);
gc.setFileOverride(genConfig.isCover());
// 实体属性 Swagger2 注解
gc.setSwagger2(genConfig.isSwagger());
// 设置基础resultMap
gc.setBaseResultMap(genConfig.isResultMap());
// 是否在xml中添加二级缓存配置
gc.setEnableCache(genConfig.isEnableCache());
// 时间类型对应策略
// gc.setDateType()
// 开启 baseColumnList
gc.setBaseColumnList(genConfig.isBaseColumnList());
// 设置主键id
gc.setIdType(IDTYPES[genConfig.getIdtype()].getIdType());
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(MysqlUtil.getInstance().getDbUrl());
// dsc.setSchemaName("public");
dsc.setDriverName(MysqlUtil.getInstance().getJdbcDriver());
dsc.setUsername(MysqlUtil.getInstance().getUsername());
dsc.setPassword(MysqlUtil.getInstance().getPassword());
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 在pack下的文件,现在不要设置null值
pc.setModuleName(null);
pc.setParent(genConfig.getPack());
// 配置输出的包名
pc.setEntity(genConfig.getEntityName());
pc.setMapper(genConfig.getMapperName());
pc.setController(genConfig.getControllerName());
pc.setService(genConfig.getServiceName());
pc.setServiceImpl(genConfig.getServiceImplName());
String xmlName = "mapper";
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// 生成自定义的Map obj.Result
Map<String, Object> map = new HashMap<>();
map.put("obj", pc.getParent() + ".obj");
String idType = "String";
map.put("camelTableName", underlineToCamel(tableName));
setMap(map);
}
};
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
String templatePath = "/templates";
String mapperTemplatePath = templatePath + "/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(mapperTemplatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
String result = projectPath + "/" + genConfig.getModuleName() + "/src/main/resources/" + xmlName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
return result;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
// 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
templateConfig.setEntity(templatePath + "/entity.java");
templateConfig.setMapper(templatePath + "/mapper.java");
templateConfig.setEntityKt(templatePath + "/entity.kt");
templateConfig.setService(templatePath + "/service.java");
templateConfig.setService(templatePath + "/service.java");
templateConfig.setServiceImpl(templatePath + "/serviceImpl.java");
templateConfig.setController(templatePath + "/controller.java");
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.baomidou.ant.common.BaseEntity");
// entity 是否使用lombok
strategy.setEntityLombokModel(genConfig.isLombok());
// 是否使用restController
strategy.setRestControllerStyle(genConfig.isRestController());
// 公共父类
// strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(tableName);
strategy.setControllerMappingHyphenStyle(true);
// 表前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
// 是否使用自动填充
if (genConfig.isFill()) {
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
strategy.setTableFillList(tableFillList);
}
// 乐观锁
// strategy.setVersionFieldName("version_name");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new MyFreemarkerTemplateEngine(projectPath));
mpg.execute();
}
Aggregations