show-cloud的代碼是可以一鍵生成的。
第一步:建立數據庫表,隨便建立了一個數據庫表
第二步:建立entity文件,該文件需要建立,裏面的內容可以不寫。
第三步:找到測試目錄下的MpGenerator文件
點擊進去後,需要修改一些相應路徑以及相應的表名等相關信息
package show.base.service;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* 代碼生成器演示
* </p>
*/
public class MpGenerator {
/**
* <p>
* MySQL 生成演示
* </p>
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 選擇 freemarker 引擎,默認 Veloctiy
// mpg.setTemplateEngine(new FreemarkerTemplateEngine());
String path = "/Users/ray/project/2019/show-cloud/show-cloud-users-root/show-cloud-users-interface/src/main/java/";
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(path);
gc.setFileOverride(true);
gc.setActiveRecord(true);// 不需要ActiveRecord特性的請改爲false
gc.setEnableCache(false);// XML 二級緩存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
// .setKotlin(true) 是否生成 kotlin 代碼
gc.setAuthor("weiyong");
// 自定義文件命名,注意 %s 會自動填充表實體屬性!
// gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
gc.setServiceName("%sService");
// gc.setServiceImplName("%sServiceDiy");
// gc.setControllerName("%sAction");
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);
}
});
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("weiyong813");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/show_cloud_base?characterEncoding=utf8");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 注意
strategy.setTablePrefix(new String[] { "GS_", "tsys_" });// 此處可以修改爲您的表前綴
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] {"GS_LOGIN_PAGE_MODEL"}); // 需要生成的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定義實體父類
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定義實體,公共字段
// 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.setEntityBuilderModel(true);
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add( new TableFill("CREATE_BY",FieldFill.INSERT));
tableFillList.add( new TableFill("CREATE_TIME",FieldFill.INSERT));
tableFillList.add( new TableFill("UPDATE_BY",FieldFill.INSERT_UPDATE));
tableFillList.add( new TableFill("UPDATE_TIME",FieldFill.INSERT_UPDATE));
strategy.setTableFillList(tableFillList);
strategy.setVersionFieldName("VERSION");
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("org.go.show");
pc.setModuleName("users");
pc.setService("i");
pc.setServiceImpl("service");
pc.setEntity("pojo");
pc.setController("controller");
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");
map.put("version", "v1");
this.setMap(map);
}
};
// 自定義 xxList.jsp 生成
// List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
// focList.add(new FileOutConfig("/template/pages/singleTable/singleTable.html") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定義輸入文件名稱
// return path+"/" + tableInfo.getEntityName() + ".html";
// }
// });
// 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);
// 關閉默認 xml 生成,調整生成 至 根目錄
TemplateConfig tc = new TemplateConfig();
tc.setController("/template/controller.java.vm");
tc.setService("/template/service.java.vm");
tc.setServiceImpl("/template/serviceImpl.java.vm");
// tc.setXml(null);
mpg.setTemplate(tc);
// 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內容修改,
// 放置自己項目的 src/main/resources/templates 目錄下, 默認名稱一下可以不配置,也可以自定義模板名稱
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// 如上任何一個模塊如果設置 空 OR Null 將不生成該模塊。
// mpg.setTemplate(tc);
// 執行生成
mpg.execute();
ConfigBuilder cb = new ConfigBuilder(null, dsc, strategy, null, null);
List<TableInfo> list = cb.getTableInfoList();
// 打印注入設置【可無】
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
第四步:則運行其中的主方法,則完成了相關代碼的生成。總的來說還是蠻方便的。