codeGenerator.java
package com.xxkj.myzone;
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.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Project code genertor
*
* @author lshaci
* @since 0.0.3
*/
public class CodeGenerator {
/**
* Project package
*/
private static String projectPackage;
private static String outputFilePath;
private static String outputMapXmlFilePath;
private static String tableNames;
/**
* 包配置
*/
private static String setMapper;
private static String setEntity;
private static String setService;
private static String setController;
/**
* Database url
*/
private static String url;
/**
* Database username
*/
private static String username;
/**
* Database password
*/
private static String password;
/**
* Database driver class
*/
private static String driverClass;
/**
* 文件名後綴
*/
private static String fileSuffix = ".java";
/**
* Init database information
*/
static {
Properties properties = new Properties();
InputStream i = CodeGenerator.class.getResourceAsStream("/generatorConfig.properties");
try {
properties.load(i);
url = properties.getProperty("generator.jdbc.url");
username = properties.getProperty("generator.jdbc.username");
password = properties.getProperty("generator.jdbc.password");
driverClass = properties.getProperty("generator.jdbc.driverClass");
projectPackage = properties.getProperty("projectPackage");
outputFilePath = properties.getProperty("outputFilePath");
outputMapXmlFilePath = properties.getProperty("outputMapXmlFilePath");
tableNames = properties.getProperty("tableNames");
setMapper = properties.getProperty("setMapper");
setEntity = properties.getProperty("setEntity");
setService = properties.getProperty("setService");
setController = properties.getProperty("setController");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* main method, execute code generator
*/
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
String javaPath = projectPackage.replaceAll("\\.", "/");
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("zhangjingxin");
gc.setOpen(false); // 是否打開輸出目錄
//gc.setOutputDir(projectPath + "/src/main/java"); // 輸出文件目錄
gc.setFileOverride(true); // 是否覆蓋已有文件
gc.setSwagger2(true); // 實體屬性 Swagger2 註解
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setBaseResultMap(true); // mapper.xml中生成BaseResultMap
gc.setActiveRecord(true);
gc.setBaseColumnList(true);
mpg.setGlobalConfig(gc);
// 數據源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
//dsc.setSchemaName("public");
dsc.setUsername(username);
dsc.setPassword(password);
dsc.setDriverName(driverClass);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(projectPackage);
//pc.setModuleName("model名"); 自定義包名
pc.setMapper(setMapper);
pc.setEntity(setEntity);
pc.setService(setService);
pc.setController(setController);
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
/**mapper xml文件*/
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設置了前後綴、此處注意 xml 的名稱會跟着發生變化!!
return projectPath + outputMapXmlFilePath + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/**控制層*/
templatePath = "/templates/controller.java.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "controller";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getControllerName());
return entityFile;
}
});
/**業務接口層*/
templatePath = "/templates/service.java.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "service";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getServiceName());
return entityFile;
}
});
/**業務實現層*/
templatePath = "/templates/serviceImpl.java.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "service/impl";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getServiceImplName());
return entityFile;
}
});
/**數據mapper層*/
templatePath = "/templates/mapper.java.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "mapper";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getMapperName());
return entityFile;
}
});
/**數據entity層*/
templatePath = "/templates/entity.java.ftl";
// 自定義配置會優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "model/entity";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getEntityName());
return entityFile;
}
});
/*cfg.setFileOutConfigList(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判斷自定義文件夾是否需要創建
checkDir("調用默認方法創建目錄");
return false;
}
});*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null);
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//strategy.setSuperEntityClass("");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
//strategy.setExclude("");
// 表名
strategy.setInclude(tableNames);
strategy.setControllerMappingHyphenStyle(true);
/**表前綴,要自動去除添加*/
//strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
配置文件
generatorConfig.properties
generator.jdbc.driverClass=com.mysql.cj.jdbc.Driver
generator.jdbc.url=jdbc:mysql://123.56.26.xxx:3308/resume?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
generator.jdbc.username=root
generator.jdbc.password=root
projectPackage=com.xxkj.myzone
outputFilePath=/src/main/java/com/xxkj/myzone/
outputMapXmlFilePath=/src/main/resources/mapper/
# 表名 多表之間逗號分隔
tableNames=my_test
# 包配置
setMapper=mapper
setEntity=model.entity
setService=service
setController=controller
yml新增配置
# mybatis config
mybatis-plus:
type-aliases-package: com.xxkj.myzone.model.entity
mapper-locations: classpath*:mapper/*Mapper.xml
# 這個配置會將執行的sql打印出來
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
啓動類新增註解(mapper接口對應路徑)
@MapperScan("com.xxkj.myzone.mapper")
mybatis-plus 參考文獻
苞米豆官方文檔