mybatisplus版本的生成器,生成实体类,mapper,service,controller
包名路径控制台自定义,自带swagger注解,继承类自定义
package com.zdy;
import com.baomidou.mybatisplus.annotation.DbType;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author yn
* USER 表示主机名
* @date
* DATA 为文档创建日期,TIME 为详细创建时间
* 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
*/
@Component
public class CodeGenerator {
/**
* 项目路径
*
*/
private static String canonicalPath = "";
/**
* 基本包名
*/
private static String basePackage = "com.zdy.";
/**
* 作者
*/
private static String authorName = "yinna";
private static String[] tables = {"fsr_option"};
/**
* table前缀
*/
private static String[] prefix = {"fsr_"};
/**
* 数据库类型
*/
private static DbType dbType = DbType.MYSQL;
/**
* 数据库配置四要素
*/
private static String driverName ="net.sf.log4jdbc.sql.jdbcapi.DriverSpy";
private static String url ="jdbc:log4jdbc:mysql://192.168.10.241:3306/psycloudpro?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false";
private static String username ="good";
private static String password ="good";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
Scanner scan = new Scanner(System.in);
/**
* 获取项目路径
*/
try {
canonicalPath = new File("").getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(canonicalPath + "/zdy-application/zdy-start/src/main/java");
globalConfig.setFileOverride(true);
globalConfig.setActiveRecord(true);
globalConfig.setEnableCache(false);
globalConfig.setBaseResultMap(true);
globalConfig.setBaseColumnList(true);
globalConfig.setSwagger2(true);
globalConfig.setOpen(false);
globalConfig.setAuthor(authorName);
globalConfig.setMapperName("%sMapper");
globalConfig.setXmlName("%sMapper");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
globalConfig.setControllerName("%sController");
mpg.setGlobalConfig(globalConfig);
// 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDriverName(driverName);
dataSourceConfig.setDbType(dbType);
dataSourceConfig.setUrl(url);
dataSourceConfig.setUsername(username);
dataSourceConfig.setPassword(password);
mpg.setDataSource(dataSourceConfig);
// 包配置
PackageConfig packageConfig = new PackageConfig();
System.out.println("请输入包名");
String next = scan.next();
packageConfig.setParent(basePackage+next);
packageConfig.setController("controller");
packageConfig.setEntity("model.entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
mpg.setPackageInfo(packageConfig);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
/**
* 注入自定义配置
*/
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List<FileOutConfig> fileOutList = new ArrayList<FileOutConfig>();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return canonicalPath + "/zdy-application/zdy-start/src/main/resources/mapper/" + next + "/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
abc.setFileOutConfigList(fileOutList);
mpg.setCfg(abc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService");
strategyConfig.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
strategyConfig.setSuperEntityClass("com.zdy.base.model.base.BaseEntity");
strategyConfig.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
strategyConfig.setSuperEntityColumns("del_flag","crt_user_id","crt_user_name","remark","crt_time","upd_user_id","upd_user_name","upd_time","serialVersionUID");
System.out.println("请输入表名,多个以英文,隔开");
//表
strategyConfig.setInclude(scan.next().split(","));
System.out.println("请输入前缀");
//前缀
strategyConfig.setTablePrefix(scan.next());
strategyConfig.setEntityBuilderModel(true);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
//开启lombok注解
strategyConfig.setEntityLombokModel(true);
mpg.setStrategy(strategyConfig);
mpg.execute();
}
}