package com.wangbin.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.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MyGenerator {
//作者
private static final String author = "wangbin";
//数据库
private static final String url = "jdbc:mysql://192.168.0.106:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&allowMultiQueries=true";
private static final String driverName = "com.mysql.cj.jdbc.Driver";
private static final String userName = "root";
private static final String password = "123456";
private static final String table_name = "demoentity";
//表对应的基类名
//如StudentController,StudentService Role +Service
private static final String name_prex = "DemoEntity";//遵循首字母大写
//生成在该目录下
private static final String package_name = "com.wangbin";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(author);
gc.setOpen(false);
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
gc.setFileOverride(true);
gc.setEntityName(name_prex);
gc.setMapperName(name_prex + "Mapper");
gc.setControllerName(name_prex + "Controller");
gc.setServiceName(name_prex + "Service");
gc.setServiceImplName(name_prex + "ServiceImpl");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setSchemaName("public");
dsc.setDriverName(driverName);
dsc.setUsername(userName);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(package_name);//你哪个父目录下创建包
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
//TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
//templateConfig.setXml(null);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置,数据库表配置
StrategyConfig strategy = new StrategyConfig();
//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段映射到实体类的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定义继承entity类,添加这一个会在生成实体类的时候继承entity
//strategy.setSuperEntityClass("com.wy.testCodeGenerator.entity");
//实体是否为lombok模型
strategy.setEntityLombokModel(true);
//生成@RestController控制器
strategy.setRestControllerStyle(true);
//是否继承controller
// strategy.setSuperControllerClass("com.wy.testCodeGenerator.controller");
// strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//strategy.setSuperEntityColumns("id");
strategy.setInclude(table_name);
//驼峰转连字符串
strategy.setControllerMappingHyphenStyle(true);
//表前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
实体里的注解需要lombok支持,这个请自行整合
亲测有效,cv既用,谢谢!