1. 程式人生 > >mybatis-plus自動生成程式碼不覆蓋生成,若存在就不生成,多模組就生成到相應的模組

mybatis-plus自動生成程式碼不覆蓋生成,若存在就不生成,多模組就生成到相應的模組

主要思想:在生成程式碼時根據路徑判斷檔案是否存在,若存在就不生成

下面就直接附上程式碼的,簡單粗暴!

/**
 * 根據駝峰命名,首字母大寫
 * @param tabName 原名
 * @return 返回生成後的名字
 *  例如:user_info 返回 UserInfo
 */
public static String getName(String tabName, String reChar) {
   String[] arr = tabName.split(reChar);
   String str = "";
   for (int i = 0; i < arr.length
; i++ ) { String startChar = arr[i].substring(0,1).toUpperCase(); String lastChar = arr[i].substring(1, arr[i].length()); String newStr = startChar + lastChar; str += newStr; } return str; }

package com.spf.common.generator;

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.MySqlTypeConvert; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; import
com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.spf.common.BaseUtils; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Auther ShuPF * @Create 2017/7/5 0005 */ @Slf4j public class MybatisPlusUtils{ public static void main(String[] args) { String[] models = {"ssm-mapper","ssm-model","ssm-service","ssm-web"}; for (String model : models) { shell(model,"user"); } } private static void shell(String model, String tabName){ File file = new File(model); String path = file.getAbsolutePath(); //path = path.substring(0, path.lastIndexOf(File.separator)); AutoGenerator mpg = new AutoGenerator(); // 全域性配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(path+"/src/main/java"); gc.setFileOverride(true); gc.setActiveRecord(true); gc.setEnableCache(false);// XML 二級快取 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(false);// XML columList gc.setAuthor("ShuPF"); // 自定義檔案命名,注意 %s 會自動填充表實體屬性! gc.setMapperName("I%sMapper"); gc.setXmlName("I%sMapper"); gc.setServiceName("I%sService"); gc.setServiceImplName("I%sServiceImpl"); gc.setControllerName("I%sController"); mpg.setGlobalConfig(gc); // 資料來源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert(){ // 自定義資料庫表字段型別轉換【可選】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println("轉換型別:" + fieldType); // 注意!!processTypeConvert 存在預設型別轉換,如果不是你要的效果請自定義返回、非如下直接返回。 return super.processTypeConvert(fieldType); } }); PropertiesUtils propertiesUtils = PropertiesUtils.getInstance(); String url = propertiesUtils.getString("jdbc.url").toString(); String name = propertiesUtils.getString("jdbc.username").toString(); String pwd = propertiesUtils.getString("jdbc.password").toString(); String driver = propertiesUtils.getString("jdbc.driver").toString(); dsc.setDriverName(driver); dsc.setUsername(name); dsc.setPassword(pwd); dsc.setUrl(url); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // strategy.setCapitalMode(true);// 全域性大寫命名 ORACLE 注意 strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此處可以修改為您的表字首 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 strategy.setInclude(new String[] {tabName}); // 需要生成的表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // 自定義實體父類 //strategy.setSuperEntityClass("com.spf.model.Entity"); // 自定義實體,公共欄位 //strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); // 自定義 mapper 父類 // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); // 自定義 service 父類 //strategy.setSuperServiceClass("com.baomidou.demo.TestService"); // 自定義 service 實現類父類 //strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); // 自定義 controller 父類 //strategy.setSuperControllerClass("com.baomidou.demo.TestController"); // 【實體】是否生成欄位常量(預設 false) // public static final String ID = "test_id"; // strategy.setEntityColumnConstant(true); // 【實體】是否為構建者模型(預設 false) // public User setName(String name) {this.name = name; return this;} // strategy.setEntityBuliderModel(true); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.spf"); pc.setController("controller"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setServiceImpl("impl"); //pc.setModuleName("test"); mpg.setPackageInfo(pc); // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無】 InjectionConfig cfg = 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); } }; // 自定義 xxList.jsp 生成 List<FileOutConfig> focList = new ArrayList<FileOutConfig>(); // focList.add(new FileOutConfig("/template/list.jsp.vm") { // @Override // public String outputFile(TableInfo tableInfo) { // // 自定義輸入檔名稱 // return "D://my_" + tableInfo.getEntityName() + ".jsp"; // } // }); // cfg.setFileOutConfigList(focList); // mpg.setCfg(cfg); // 調整 xml 生成目錄演示 focList.add(new FileOutConfig("/templates/mapper.xml.vm") { @Override public String outputFile(TableInfo tableInfo) { return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); //多模組 TemplateConfig tc = getTemplateConfig(gc,pc,model,tabName, false); if (tc.getMapper() == null && tc.getXml() == null && tc.getService() == null && tc.getServiceImpl() == null && tc.getController() == null && tc.getEntity() == null) { return; } // 關閉預設 xml 生成,調整生成 至 根目錄(單模組) // TemplateConfig tc = new TemplateConfig(); // tc.setController("java.com.SpringBoot.ibats.controller"); // tc.setEntity("java.com.SpringBoot.ibats.model.Entity"); // tc.setMapper("java.com.SpringBoot.ibats.mapper"); // tc.setXml("src.resources.sqlBaties"); // tc.setService("java.com.SpringBoot.ibats.service"); // tc.setServiceImpl("java.com.SpringBoot.ibats.service.impl"); mpg.setTemplate(tc); // 自定義模板配置,可以 copy 原始碼 mybatis-plus/src/main/resources/template 下面內容修改, // 放置自己專案的 src/main/resources/template 目錄下, 預設名稱一下可以不配置,也可以自定義模板名稱 // TemplateConfig tc = new TemplateConfig(); // tc.setController("..."); // tc.setEntity("..."); // tc.setMapper("..."); // tc.setXml("..."); // tc.setService("..."); // tc.setServiceImpl("..."); // 如上任何一個模組如果設定 空 OR Null 將不生成該模組。 // mpg.setTemplate(tc); // 執行生成 mpg.execute(); // 列印注入設定【可無】 System.err.println(mpg.getCfg().getMap().get("abc")); } /** * 控制包生成的路徑與是否覆蓋生成 * @param gc // 全域性配置 * @param pc 包配置 * @param model model名 * @param tabName 表名 * @param isCover 是否覆蓋生成程式碼 * @return TemplateConfig */ private static TemplateConfig getTemplateConfig(GlobalConfig gc, PackageConfig pc, String model, String tabName, boolean isCover) { TemplateConfig tc = new TemplateConfig(); String entity = BaseUtils.getName(tabName,"_"); String path = model + "/src/main/java/" +replace( pc.getParent()); if (!isCover) { if ("ssm-mapper".equals(model)) { String mapperPath =path + "/" + replace(pc.getMapper()) + "/" + gc.getMapperName().replace("%s",entity) + ".java"; if (isExists(mapperPath)) { tc.setMapper(null); log.info(gc.getMapperName().replace("%s",entity) + ".java 檔案已存在"); } String mapperXmlPath =path + "/" + replace(pc.getXml()) + "/" + gc.getXmlName().replace("%s",entity) + ".xml"; if (isExists(mapperXmlPath)) { tc.setXml(null); log.info(gc.getXmlName().replace("%s",entity) + ".xml 檔案已存在"); } tc.setController(null); tc.setEntity(null); tc.setService(null); tc.setServiceImpl(null); } else if ("ssm-model".equals(model)) { String modelPath = path + "/" + replace(pc.getEntity()) + "/" + entity + ".java"; if (isExists(modelPath)) { tc.setEntity(null); log.info(entity + ".java 檔案已存在"); } tc.setController(null); tc.setService(null); tc.setServiceImpl(null); tc.setMapper(null); tc.setXml(null); } else if ("ssm-service".equals(model)) { String servicePath = path + "/" +replace(pc.getService()) + "/" + gc.getServiceName().replace("%s",entity) + ".java"; if (isExists(servicePath)) { tc.setService(null); log.info(gc.getServiceName().replace("%s",entity) + ".java 檔案已存在"); } String serviceImplPath = path + "/" +replace(pc.getServiceImpl()) + "/" + gc.getServiceImplName().replace("%s",entity) + ".java"; if (isExists(serviceImplPath)) { tc.setServiceImpl(null); log.info(gc.getServiceImplName().replace("%s",entity) + ".java 檔案已存在"); } tc.setController(null); tc.setMapper(null); tc.setXml(null); tc.setEntity(null); } else if ("ssm-web".equals(model)) { String controllerPath = path + "/" +replace(pc.getController()) + "/" + gc.getControllerName().replace("%s",entity) + ".java";; if (isExists(controllerPath)) { tc.setController(null); log.info(gc.getControllerName().replace("%s",entity) + ".java 檔案已存在"); } tc.setMapper(null); tc.setXml(null); tc.setService(null); tc.setServiceImpl(null); tc.setEntity(null); } } else { if ("ssm-mapper".equals(model)) { tc.setController(null); tc.setEntity(null); tc.setService(null); tc.setServiceImpl(null); } else if ("ssm-model".equals(model)) { tc.setController(null); tc.setService(null); tc.setServiceImpl(null); tc.setMapper(null); tc.setXml(null); } else if ("ssm-service".equals(model)) { tc.setController(null); tc.setMapper(null); tc.setXml(null); tc.setEntity(null); } else if ("ssm-web".equals(model)) { tc.setMapper(null); tc.setXml(null); tc.setService(null); tc.setServiceImpl(null); tc.setEntity(null); } } return tc; } /** * 判斷檔案是否存在 * @param path 路徑 * @return */ private static boolean isExists(String path) { File file = new File(path); return file.exists(); } /** * 將點替換為斜槓 * @param name * @return */ private static String replace(String name) { return name.replace(".","/"); } }
OK!
裡面的資料來源獲取,我是寫的一個單列模式來獲取properties檔案 參考點選開啟連結