public class CodeGenerator {
/**
* 修改生成配置
*/
public static String dbUrl = "数据库连接串";
public static String dbName = "账号";
public static String dbPassword = "密码";
public static String[] removePreTableName = new String[]{""};
//生成路径
public static String parentpackage = "cn.com.do1.zhp.modules.dsf";
//需要执行生成策略的表
public static String[] tables = new String[]{
"表名"
};
/**
* <p>
* MySQL 生成演示
* </p>
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
System.out.println(projectPath);
// gc.setOutputDir(projectPath + "/biz-portal/model/src/main/java");
gc.setFileOverride(true);
gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("generator");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
// 自定义数据库表字段类型转换【可选】
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(dbName);
dsc.setPassword(dbPassword);
dsc.setUrl(dbUrl);
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(removePreTableName);// 此处可以修改移除表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setTablePrefix("tb_");
strategy.setInclude(tables); // 需要生成的表
strategy.setEntityLombokModel(false);
strategy.setRestControllerStyle(true);
// 表填充字段,创建日期、更新日期
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("CREATE_TIME", FieldFill.INSERT));
tableFillList.add(new TableFill("UPDATE_TIME", FieldFill.INSERT_UPDATE));
tableFillList.add(new TableFill("CREATE_USER", FieldFill.INSERT));
tableFillList.add(new TableFill("UPDATE_USER", FieldFill.INSERT_UPDATE));
strategy.setTableFillList(tableFillList);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("");
// pc.setModuleName("");
pc.setEntity("cn.com.do1.dsf.modules.dsf.entity");
pc.setController("cn.com.do1.dsf.controller");
pc.setService("cn.com.do1.dsf.service.serviceInterface");
pc.setServiceImpl("cn.com.do1.dsf.service");
pc.setMapper("cn.com.do1.dsf.dao");
mpg.setPackageInfo(pc);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
List<FileOutConfig> focList = new ArrayList<>();
// 调整 xml 生成目录演示
focList.add(new FileOutConfig("/template/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
tableInfo.setXmlName(tableInfo.getEntityName() + "Dao");
return projectPath + "/web/src/main/resources/mapper/authorize/" + tableInfo.getEntityName() + "Dao.xml";
}
});
focList.add(new FileOutConfig("/template/domain.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/model/src/main/java/cn/com/do1/dsf/modules/dsf/entity/" + tableInfo.getEntityName() + ".java";
}
});
focList.add(new FileOutConfig("/template/controller.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/web/src/main/java/cn/com/do1/dsf/controller/" + tableInfo.getEntityName() + "Controller.java";
}
});
focList.add(new FileOutConfig("/template/service.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
tableInfo.setServiceName(tableInfo.getEntityName() + "Service");
return projectPath + "/web/src/main/java/cn/com/do1/dsf/service/serviceInterface/" + tableInfo.getEntityName() + "Service.java";
}
});
focList.add(new FileOutConfig("/template/serviceImpl.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
tableInfo.setMapperName(tableInfo.getEntityName() + "Dao");
return projectPath + "/web/src/main/java/cn/com/do1/dsf/service/" + tableInfo.getEntityName() + "ServiceImpl.java";
}
});
focList.add(new FileOutConfig("/template/mapper.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
tableInfo.setMapperName(tableInfo.getEntityName() + "Dao");
return projectPath + "/web/src/main/java/cn/com/do1/dsf/dao/" + tableInfo.getEntityName() + "Dao.java";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig tc = new TemplateConfig();
tc.setEntity("/template/domain.java.vm");
tc.setController("/template/controller.java.vm");
tc.setService("/template/service.java.vm");
tc.setServiceImpl("/template/serviceImpl.java.vm");
tc.setMapper("/template/mapper.java.vm");
tc.setXml("/template/mapper.xml.vm");
mpg.setTemplate(tc);
// 执行生成
mpg.execute();
}