mybatis plus代碼生成器


package com.tycin.blog.generator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.tycin.blog.configure.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
@Component
public class CodeGenerator {
// 常量設置
// private static String ENTITY_TEMPLATE = "";
private static Configuration configuration= new Configuration();
private static final String JAVA_OUTPUT_PATH = "/java/com/tycin/blog/";
private static final String RESOURCE_OUTPUT_PATH = "/resources/";
private static final String[] TABLES = new String[]{"t_article","t_attachment","t_collection","t_collection_type","t_permission","t_record","t_role","t_user","t_user_role","t_role_permission"};
public 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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + tip + "!");
}
public static void main(String[] args){
// 代碼生成器
AutoGenerator autoGenerator = new AutoGenerator();
// 全局配置
GlobalConfig globalConfig = new GlobalConfig();
// 項目路徑
String projectPath = System.getProperty("user.dir");
// 設置是否覆蓋已有文件
globalConfig.setFileOverride(true);
// 是否在xml中添加二級緩存配置(不推薦使用)
globalConfig.setEnableCache(false);
// 開啟kotlin模式()
globalConfig.setKotlin(false);
// 開啟activeRecord模式
globalConfig.setActiveRecord(true);
// 開啟baseresultMap
globalConfig.setBaseResultMap(true);
// 開啟baseColumnList
globalConfig.setBaseColumnList(true);
// 開啟時間類型對應策略(選擇TIME_PACK以便使用LocalDateTime)
globalConfig.setDateType(DateType.TIME_PACK);
// 實體命名方式
globalConfig.setEntityName("%sEntity");
// mapper命名方式
globalConfig.setMapperName("%sMapper");
// mapper xml命名方式
globalConfig.setXmlName("%sMapperXml");
// service命名方式
globalConfig.setServiceName("%sService");
// service impl 命名方式
globalConfig.setServiceImplName("%sServiceImpl");
// controller命名方式
globalConfig.setControllerName("%sController");
// 指定生成的主鍵ID類型
globalConfig.setIdType(IdType.ASSIGN_UUID);
// 代碼生成存放目錄
globalConfig.setOutputDir(projectPath + "/src/main");
// 設置作者
globalConfig.setAuthor("cjb");
// 設置是否打開輸出目錄
globalConfig.setOpen(false);
// 實體屬性 Swagger2 注解
globalConfig.setSwagger2(true);
// 將配置導入生成器
autoGenerator.setGlobalConfig(globalConfig);

// 模版設置
// TemplateConfig templateConfig = new TemplateConfig();
// templateConfig.setEntity();
// templateConfig.setEntityKt();
// templateConfig.setService();
// templateConfig.setServiceImpl();
// templateConfig.setMapper();
// templateConfig.setXml();
// templateConfig.setController();

// 數據源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
// 配置數據源的Url
dataSourceConfig.setUrl(configuration.getUrl());
// 配置數據源的模式(postgresql中的概念)
dataSourceConfig.setSchemaName("public");
// 配置驅動名稱
dataSourceConfig.setDriverName(configuration.getDriver());
// 配置數據庫用戶名
dataSourceConfig.setUsername(configuration.getUsername());
// 配置數據庫密碼
dataSourceConfig.setPassword(configuration.getPassword());
// 配置數據庫信息查詢類
dataSourceConfig.setDbQuery(new PostgreSqlQuery());
// 配置數據庫類型
dataSourceConfig.setDbType(DbType.POSTGRE_SQL);
// 配置類型轉換
dataSourceConfig.setTypeConvert(new PostgreSqlTypeConvert());
// 將數據源配置置入生成器
autoGenerator.setDataSource(dataSourceConfig);
// 數據庫表配置(策略配置)
StrategyConfig strategyConfig = new StrategyConfig();
// 駝峰轉連字符
// strategyConfig.setControllerMappingHyphenStyle(false);
// 設置生成@RestController
// strategyConfig.setRestControllerStyle(true);
// 設置boolean類型字段是否移除is前綴
// strategyConfig.setEntityBooleanColumnRemoveIsPrefix(false);
// 設置實體是否生成字段常量
strategyConfig.setEntityColumnConstant(true);
// 需要包含的表名(與exclude二選一)
// strategyConfig.setInclude();
// 模糊匹配表名
// strategyConfig.setLikeTable();
// 需要排除的表名,當enableSqlFilter為false時,允許正則表達式
// strategyConfig.setExclude();
// 模糊排除表名
// strategyConfig.setNotLikeTable();
// 設置實體是否為構建者模型
// strategyConfig.setChainModel(false);
// 設置實體是否為lombok模型
strategyConfig.setEntityLombokModel(true);
// 設置生成實體類時生成字段注解
strategyConfig.setEntityTableFieldAnnotationEnable(true);
// 設置樂觀鎖屬性名稱
strategyConfig.setVersionFieldName("version");
// 設置邏輯刪除屬性名稱
strategyConfig.setLogicDeleteFieldName("deleted");

// 設置需要生成的數據庫表
strategyConfig.setInclude(TABLES);
// 設置是否大寫命名
strategyConfig.setCapitalMode(true);
// 設置是否跳過視圖
strategyConfig.setSkipView(true);
// 設置數據庫表映射到實體的命名策略
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
// 設置數據庫表字段映射到實體的命名策略(未設置按照naming執行)
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
// 設置表前綴
strategyConfig.setTablePrefix("t_");
// 設置字段前綴
// strategyConfig.setFieldPrefix("");
// 設置自定義繼承的entity類全稱,帶包名
// strategyConfig.setSuperEntityClass("/home/tycin-deepin/Documents/gitRepos/blogsystem/blog/src/main/java/com/tycin/blog/base/BaseEntity.java");
// 自定義基礎的entity類,公共字段
// strategyConfig.setSuperEntityColumns();
// 自定義繼承的Mapper類全稱,帶包名
// strategyConfig.setSuperMapperClass();
// 自定義繼承的service類全稱,帶包名
// strategyConfig.setSuperServiceClass();
// 自定義繼承的ServiceImpl類全稱,帶包名
// strategyConfig.setSuperServiceImplClass();
// 自定義繼承的controller類全稱,帶包名
// strategyConfig.setSuperControllerClass();
// 默認激活進行sql模糊表名匹配
strategyConfig.setEnableSqlFilter(false);
// 將策略配置導入生成器
autoGenerator.setStrategy(strategyConfig);

// 包配置
PackageConfig packageConfig = new PackageConfig();
// packageConfig.setModuleName(scanner("模塊名"));
// 設置父包名
packageConfig.setParent("com.tycin.blog");
// 設置controller包名
packageConfig.setController("controller");
// 設置service包名
packageConfig.setService("service");
// 設置mapper包名
packageConfig.setMapper("mapper");
// 設置xml包名
packageConfig.setXml("mapper_xml");
// 設置entity包名
packageConfig.setEntity("entity");
// 設置serviceImpl包名
packageConfig.setServiceImpl("serviceImpl");
// 設置路徑配置信息
HashMap<String,String> pathInfo = new HashMap<>();
pathInfo.put("entity_path",globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getEntity());
pathInfo.put("controller_path",globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getController());
pathInfo.put("xml_path",globalConfig.getOutputDir()+RESOURCE_OUTPUT_PATH+packageConfig.getXml());
pathInfo.put("service_path",globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getService());
pathInfo.put("service_impl_path",globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getServiceImpl());
pathInfo.put("mapper_path",globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getMapper());
packageConfig.setPathInfo(pathInfo);
// 將包設置導入生成器
autoGenerator.setPackageInfo(packageConfig);

// 自定義配置
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// todo
}
};
// 自定義輸出配置
List<FileOutConfig> fileOutConfigList = new ArrayList<>();
// 自定義配置會被優先輸出
// 配置實體類輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*ENTITY_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir()+JAVA_OUTPUT_PATH+packageConfig.getEntity()
+StringPool.SLASH+tableInfo.getEntityName()+ StringPool.DOT_JAVA;
}
});
// 配置mapper xml文件輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*MAPPER_XML_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir() + RESOURCE_OUTPUT_PATH + packageConfig.getXml()+StringPool.SLASH
+tableInfo.getMapperName() + StringPool.DOT_XML;
}
});
// 配置mapper文件輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*MAPPER_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir() + JAVA_OUTPUT_PATH + packageConfig.getMapper() + StringPool.SLASH
+ tableInfo.getMapperName() + StringPool.DOT_JAVA;
}
});
// 配置service文件輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*SERVICE_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir() + JAVA_OUTPUT_PATH + packageConfig.getService() + StringPool.SLASH
+ tableInfo.getServiceName() + StringPool.DOT_JAVA;
}
});
// 配置serviceImpl文件輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*SERVICE_IMPL_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir() + JAVA_OUTPUT_PATH + packageConfig.getServiceImpl() + StringPool.SLASH
+ tableInfo.getServiceImplName() + StringPool.DOT_JAVA;
}
});
// 配置controller文件輸出目錄路徑
fileOutConfigList.add(new FileOutConfig(/*CONTROLLER_TEMPLATE*/) {
@Override
public String outputFile(TableInfo tableInfo) {
return globalConfig.getOutputDir() + JAVA_OUTPUT_PATH + packageConfig.getController() + StringPool.SLASH
+ tableInfo.getControllerName() + StringPool.DOT_JAVA;
}
});
// injectionConfig.setFileCreate(new IFileCreate() {
// @Override
// public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// // 判斷自定義文件夾是否需要創建
// checkDir(globalConfig.getOutputDir()+RESOURCE_OUTPUT_PATH+packageConfig.getXml());
// if (fileType == FileType.MAPPER) {
// // 已經生成 mapper 文件判斷存在,不想重新生成返回 false
// return !new File(filePath).exists();
// }
// // 允許生成模板文件
// return true;
// }
// });
injectionConfig.setFileOutConfigList(fileOutConfigList);
autoGenerator.setCfg(injectionConfig);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
autoGenerator.execute();
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM