直接复制粘贴(把数据库地址,生成代码地址等修改后)就可以直接运行
package com.zz.bzyw.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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.*;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.config.rules.PropertyInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MysqlGenerator {
public static String scanner(String tip){
Scanner scanner=new Scanner(System.in);
StringBuilder help=new StringBuilder();
help.append("请输入"+tip+":");
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();
String projectPath="项目地址";
gc.setOutputDir(projectPath+"/src/main/java");
gc.setAuthor("创建人");
gc.setOpen(false);
gc.setSwagger2(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setFileOverride(true);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc=new DataSourceConfig();
dsc.setUrl("jdbc:oracle:thin:@10.0.10.111:1521/orclpdb");
dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setTypeConvert(new OracleTypeConvert(){
@Override
public PropertyInfo processTypeConvert(GlobalConfig globalConfig, String fieldType) {
String t = fieldType.toLowerCase();
if (t.contains("char")) {
return DbColumnType.STRING;
} else {
if (!t.contains("date") && !t.contains("timestamp")) {
if (t.contains("number")) {
if (t.matches("number\\(+\\d\\)")) {
return DbColumnType.INTEGER;
}
if (t.matches("number\\(+\\d{2}+\\)")) {
return DbColumnType.LONG;
}
return DbColumnType.DOUBLE;
}
if (t.contains("float")) {
return DbColumnType.FLOAT;
}
if (t.contains("clob")) {
return DbColumnType.STRING;
}
if (t.contains("blob")) {
return DbColumnType.BLOB;
}
if (t.contains("binary")) {
return DbColumnType.BYTE_ARRAY;
}
if (t.contains("raw")) {
return DbColumnType.BYTE_ARRAY;
}
} else {
switch(globalConfig.getDateType()) {
case ONLY_DATE:
return DbColumnType.DATE;
case SQL_PACK:
return DbColumnType.TIMESTAMP;
case TIME_PACK:
return DbColumnType.LOCAL_DATE_TIME;
}
}
return DbColumnType.STRING;
}
}
});
mpg.setDataSource(dsc);
PackageConfig pc=new PackageConfig();
pc.setParent("com.zz.bzyw");
pc.setEntity("entity");
pc.setMapper("dao");
mpg.setPackageInfo(pc);
InjectionConfig cfg=new InjectionConfig() {
@Override
public void initMap() {
}
};
String templatePath="/templates/mapper.xml.vm";
List<FileOutConfig> focList=new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath+"/src/main/resources/mapper/"+
"/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig templateConfig=new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
StrategyConfig strategy=new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setRestControllerStyle(true);
strategy.setEntityLombokModel(true);
strategy.setInclude(scanner("表名,多个英文逗号分隔").split(","));
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}
运行后在控制台输入数据库中的需要生成相应代码的表的表名(多张表用“,”隔开)