package com.neusoft.utils;
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.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* create time
* 2021/12/2 18:22
* mybatis-plus code generator
*/
public class CodeGeneratorNoServiceInterFace {
/**
* <p>
* Read console content
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("Please enter" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("Please enter the correct one" + tip + "!");
}
public static void main(String[] args) {
// Code generator
AutoGenerator mpg = new AutoGenerator();
// Global configuration
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("Author's Name"); // You can do whatever you want here
gc.setOpen(false);
gc.setFileOverride(false); // Whether to cover
gc.setServiceName("%sService");
// gc.setSwagger2(true); Entity property Swagger2 annotation
mpg.setGlobalConfig(gc);
// Data source configuration
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("Database Address");
// ("public");
dsc.setDriverName(""); // Here I am filling in the oracle driver, which can be replaced with the MySQL driver name
dsc.setUsername("username");
dsc.setPassword("password");
mpg.setDataSource(dsc);
// Package configuration
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent("Bail Name"); // Generally: com. Company name or com. Your first letter
mpg.setPackageInfo(pc);
// Custom configuration
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// If the template engine is freemarker
String templatePath = "/templates/";
// If the template engine is velocity
// String templatePath = "/templates/";
// Configuration template
TemplateConfig templateConfig = new TemplateConfig();
// Close the original generation
templateConfig.setService(null);
templateConfig.setServiceImpl(null);
// Custom output configuration
List<FileOutConfig> focList = new ArrayList<>();
// Template file location
String serviceTemplatePath = "/templates/";
// Custom configuration will be output first (file output location)
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// Customize the output file name. If you set the pre-suffix for Entity, please note that the name of xml will change as a result! !
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
// Customize service business class, service file output location
focList.add(new FileOutConfig(serviceTemplatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// Customize the output file name. If you set the pre-suffix for Entity, please note that the name of xml will change as a result! ! Here is the location of the service implementation class:
return projectPath + "/src/main/java/com/fzg/service"
+ "/" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
}
});
/*
(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// Determine whether a custom folder needs to be created
checkDir("Catalogue created by calling the default method, for custom directory");
if (fileType == ) {
// The mapper file has been generated and it is determined that it exists. If you do not want to regenerate it, return false
return !new File(filePath).exists();
}
// Allow template files to be generated
return true;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// Configure custom output templates
//Specify the custom template path, be careful not to bring .ftl/.vm, it will automatically identify based on the template engine used.
// ("templates/");
// ();
// ();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// Policy configuration
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("Table name, multiple English commas split").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}