一、代码生成类
public class CodeGenerator {
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
List<FileOutConfig> fileOutConfigList = new ArrayList<>();
// 调整 xml 生成目录演示
fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/mapper/"+ tableInfo.getEntityName() + "Mapper.xml";
}
});
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
@Override
public void initTableMap(TableInfo tableInfo) {
super.initTableMap(tableInfo);
Map<String, Object> map = new HashMap<>();
String serverName = String.valueOf(tableInfo.getServiceName().charAt(0));
String entityName = String.valueOf(tableInfo.getEntityName().charAt(0));
map.put("serverFiledName", tableInfo.getServiceName().replaceFirst(serverName,serverName.toLowerCase()));
map.put("entityFiledName", tableInfo.getEntityName().replaceFirst(entityName,entityName.toLowerCase()));
this.setMap(map);
}
};
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
injectionConfig.setFileOutConfigList(fileOutConfigList);
mpg.setCfg(injectionConfig);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("templates/controller.java");
templateConfig.setXml(null);
// 选择Freemarker 引擎,默认Velocity 引擎
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.setTemplate(templateConfig);
mpg.setCfg(injectionConfig);
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("Nihui");
gc.setOutputDir(projectPath + "/src/main/java");
//是否覆盖同名文件,默认是false
gc.setFileOverride(false);
// 不需要ActiveRecord特性的请改为false
gc.setActiveRecord(true);
// XML 二级缓存
gc.setEnableCache(false);
// XML ResultMap
gc.setBaseResultMap(true);
// XML columList
gc.setBaseColumnList(true);
// 开启Swagger模式
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://192.168.1.248:3306/db_renshe_new?useUnicode=true&useSSL=false&characterEncoding=utf-8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setModuleName("api");
pc.setParent("com.ziicoo");
pc.setEntity("domain");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// 设置表前缀
// strategy.setTablePrefix(new String[]{"jqmj_"});
// 是否采用驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 生成表名
strategy.setInclude(new String[]{"rcw_professional_info"});
strategy.setRestControllerStyle(true);
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定义实体父类
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuilderModel(true);
mpg.setStrategy(strategy);
// 执行生成
mpg.execute();
}
}
二、相关配置文件
见百度网盘