mybaitis-plus官网上有代码生成器的配置demo,单并没有对分布式多模块的项目做相关的说明,我这边也是引用mybatis plus官网上的demo做了一些简单的修改支持了spring cloud或者dubbo分布式项目目录分布。
package com.kfl;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.*;
public class CodeGenerator {
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) {
//获取项目目录
String parent_dir = System.getProperty("user.dir");
//获取pojo目录
String model_name = scanner("请输入模块名");
String pojodir = parent_dir + "/kfl-common-pojo/src/main/java/com/kfl/pojo/"+model_name+"/";
//获取mapper目录
String mapperdir = parent_dir + "/kfl-common-mapper/src/main/java/com/kfl/mapper/"+model_name+"/";
//获取模块名
//获取service目录
String servicedir = parent_dir + "/kfl-service-"+model_name+"-api/src/main/java/com/kfl/service/api/"+model_name+"/";
String serviceImpldir = parent_dir + "/kfl-service-" + model_name + "/src/main/java/com/kfl/service/" + model_name+"/";
//代码生成
AutoGenerator mpg = new AutoGenerator();
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
GlobalConfig gc = new GlobalConfig();
gc.setActiveRecord(true);
// gc.setOutputDir("C://");
//是否支持swaggeer
gc.setSwagger2(true);
gc.setAuthor("admin");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
//数据库配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/kfl");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
mpg.setDataSource(dataSourceConfig);
//包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.kfl");
pc.setEntity("pojo." + model_name);
pc.setService("service.api" + model_name);
pc.setServiceImpl("service." + model_name);
pc.setMapper("mapper." + model_name);
mpg.setPackageInfo(pc);
//设置自定义输出目录(分布式项目使用)
Map<String, String> pathInfo = new HashMap<>();
pathInfo.put(ConstVal.ENTITY_PATH, pojodir);
pathInfo.put(ConstVal.MAPPER_PATH, mapperdir);
pathInfo.put(ConstVal.XML_PATH, mapperdir);
pathInfo.put(ConstVal.SERVICE_PATH, servicedir);
pathInfo.put(ConstVal.SERVICE_IMPL_PATH, serviceImpldir);
pc.setPathInfo(pathInfo);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityBuilderModel(false);
//是否支持Lombok
strategy.setEntityLombokModel(true);
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setTablePrefix("KFL_");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
mpg.setStrategy(strategy);
//模板配置
TemplateConfig templateConfig = new TemplateConfig();
//设置自定义模板
templateConfig.setService("/templates/myService.java");
templateConfig.setServiceImpl("/templates/myServiceImpl.java");
mpg.setTemplate(templateConfig);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}