package com.neuxa.is.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
/**
* <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.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setFileOverride(true);
String projectPath = System.getProperty("user.dir") ;
// gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("syp867");
gc.setOpen(false);
// gc.setServiceName("%sService");
// gc.setServiceImplName("%sServiceImpl");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://192.168.1.144:3306/gk_is?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("qwe123ASD");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
scanner("模块名");
/* pc.setModuleName());*/
pc.setParent("com.neuxa.is.fm");
pc.setEntity("persistence.entity");
pc.setMapper("persistence.mapper");
mpg.setPackageInfo(pc);
//自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
String basePath = "/src/main/java/com/neuxa/is/";
List<FileOutConfig> focList = new ArrayList<>();
//dto输出路径
String templatePath = "/template/dto.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-interface" + basePath + "fm/service/dto/" + tableInfo.getEntityName() + "Dto" + StringPool.DOT_JAVA;
}
});
//vo输出路径
templatePath = "/template/vo.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-rest" + basePath + "fm/vo/" + tableInfo.getEntityName() + "Vo" + StringPool.DOT_JAVA;
}
});
//实体输出路径
templatePath = "/templates/entity.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-service" + basePath + "fm/persistence/entity/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
}
});
//mapper输出路径
templatePath = "/templates/mapper.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-service" + basePath + "fm/persistence/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_JAVA;
}
});
//mapperXml输出路径
templatePath = "/templates/mapper.xml.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-service/src/main/resources//mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
//service接口输出路径
templatePath = "/template/service.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-interface" + basePath + "fm/service/" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
}
});
//service实现类输出路径
templatePath = "/template/serviceImpl.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-service" + basePath + "fm/service/impl/" + tableInfo.getEntityName() + "ServiceImpl" + StringPool.DOT_JAVA;
}
});
//controller输出路径
templatePath = "/template/controller.java.vm";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/is-rest" + basePath + "fm/controller/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
templateConfig.setEntity(null);
templateConfig.setMapper(null);
templateConfig.setXml(null);
templateConfig.setController(null);
templateConfig.setService(null);
templateConfig.setServiceImpl(null);
mpg.setTemplate(templateConfig);
// 策略配置
/**
配置的是什么
*/
StrategyConfig strategy = new StrategyConfig();
strategy.setRestControllerStyle(true);
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.neuxa.springbreeze.core.persistence.entity.BaseEntity");
strategy.setSuperControllerClass("com.neuxa.springbreeze.core.controller.BaseController");
strategy.setSuperServiceClass("com.neuxa.springbreeze.core.persistence.service.IBaseService");
strategy.setSuperServiceImplClass("com.neuxa.springbreeze.core.persistence.service.impl.BaseService");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setSuperEntityColumns("id","create_time","update_time","yn");
strategy.setTablePrefix(pc.getModuleName() + "_");
strategy.setEntityLombokModel(true);
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
//执行
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}