package com.xxx.mall.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import cn.hutool.core.collection.CollectionUtil;
@Configuration
public class MybatisPlusConfig {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("D:/workspace/mall-server/mall-service/src/main/java");
gc.setAuthor("DY");
gc.setOpen(false);
gc.setFileOverride(false);
gc.setSwagger2(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setEntityName("%sEntity");
gc.setMapperName("%sMapper"); // %s会自动填充表实体属性
// gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//配置自己的数据源
dsc.setUrl("jdbc:postgresql://......./mall_dev");
dsc.setSchemaName("mall");
dsc.setDriverName("org.postgresql.Driver");
dsc.setUsername("admin");
dsc.setPassword("数据库密码");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent("com.xiangxiang.mall");
pc.setController("controller");
pc.setEntity("entity");
pc.setMapper("dao");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setSuperEntityClass("com.xiangxiang.commonbase.entity.BaseEntity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setEntityLombokModel(true);
strategy.setSuperEntityColumns("createTime", "updateTime","creator","updater","isDeleted");
strategy.setRestControllerStyle(true);
//生成去掉前缀
strategy.setTablePrefix(new String[] { "mall_" });
//表名
strategy.setInclude(new String[] { "mall_calculation_parameters","mall_calculation_rules" });
mpg.setStrategy(strategy);
// 自定义配置
InjectionConfig in = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
//自定义配置,在模版中cfg.superColums 获取
// TODO 这里解决子类会生成父类属性的问题,在模版里会用到该配置
map.put("superColums", this.getConfig().getStrategyConfig().getSuperEntityColumns());
this.setMap(map);
}
};
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
String templatePath = "/templates/mapper.xml.btl";
// // 自定义输出配置
// List<FileOutConfig> focList = new ArrayList<>();
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return "D:/workspace/mall-server/mall-service/src/main/resources/mapper/" + "/"
// + tableInfo.getEntityName().split("Entity")[0] + "Mapper" + StringPool.DOT_XML;
// }
// });
in.setFileOutConfigList(CollectionUtil.newArrayList(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return "D:/workspace/mall-server/mall-service/src/main/resources/mapper/" + "/"
+ tableInfo.getEntityName().split("Entity")[0] + "Mapper" + StringPool.DOT_XML;
}
}));
mpg.setCfg(in);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
// 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}