package com.qhzx.mad.backstage.config;
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
public class MyBatisPlusCodeGenerator {
public static final String PACKAGE_NAME = "com.qhzx.mad.backstage";
public static void main(String[] args) {
String[] tables = new String[]{"user"};
String[] tablePrefixs = new String[]{"APP_"};
executeCode(PACKAGE_NAME, tables, tablePrefixs);
}
private static void executeCode(String pack, String[] tables, String[] tablePrefixs) {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
gc.setFileOverride(true);
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setEntityName("%sModel");
gc.setAuthor("Lvfang");
gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
DataSourceConfig config = new DataSourceConfig();
config.setUrl("jdbc:mysql://localhost:3306/库名");
config.setDriverName("com.mysql.jdbc.Driver");
config.setUsername("账号");
config.setPassword("密码");
mpg.setDataSource(config);
PackageConfig pc = new PackageConfig();
pc.setParent(pack);
pc.setEntity("user");
mpg.setPackageInfo(pc);
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
public String outputFile(TableInfo tableInfo) {
if (StringUtils.isEmpty(pc.getModuleName())) {
return projectPath + "/src/main/resources/mapper/" + tableInfo.getXmlName() + StringPool.DOT_XML;
} else {
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getXmlName() + StringPool.DOT_XML;
}
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setInclude(tables);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(tablePrefixs);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}