MyBatis-Plus 程式碼生成器模板

maven 依賴

 <!--Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis plus 程式碼生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis plus 程式碼生成器模板 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>

程式碼生成器

public class CreateCode {
public static void main(String[] args) { // 程式碼生成器
AutoGenerator mpg = new AutoGenerator(); // 全域性配置
GlobalConfig gc = new GlobalConfig();
final String projectPath = "user.dir";
gc.setOutputDir(projectPath+ "/src/main/java"); //生成的程式碼放哪裡
gc.setFileOverride(true); // 是否覆蓋
gc.setOpen(false); // 是否開啟輸出目錄
// gc.setEnableCache(true); // 是否在xml中新增二級快取配置
gc.setAuthor("XXX"); // 開發人員
// gc.setSwagger2(true); // 開啟 swagger2 模式
gc.setBaseResultMap(true); // XML 開啟 BaseResultMap
gc.setBaseColumnList(true); // XML 開啟 baseColumnList
gc.setEntityName("%s"); // 實體命名方式
gc.setMapperName("%sMapper"); // mapper 命名方式
gc.setXmlName("%sMapper"); // Mapper xml 命名方式
gc.setServiceName("%sService"); // service 命名方式
gc.setServiceImplName("%sServiceImpl"); // service impl 命名方式
gc.setControllerName("%sController"); // controller 命名方式
mpg.setGlobalConfig(gc); // 資料來源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://IP:埠/資料庫名?useUnicode=true&useSSL=true&characterEncoding=utf8&allowPublicKeyRetrieval=true"); // mysql url
dsc.setDriverName("com.mysql.cj.jdbc.Driver"); // mysql 驅動名
dsc.setUsername("root"); // mysql 使用者名稱
dsc.setPassword("root"); // mysql 密碼
dsc.setDbType(DbType.MYSQL); // 資料庫型別
mpg.setDataSource(dsc); // 包名配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.baidu"); //父包名
pc.setModuleName("user"); // 父包模組名
// pc.setEntity(""); // Entity包名
// pc.setService(""); // Service包名
// pc.setServiceImpl(""); // Service Impl包名
// pc.setMapper(""); // Mapper包名
// pc.setXml(""); // Mapper XML包名
// pc.setPathInfo(); // 路徑配置資訊
mpg.setPackageInfo(pc); // 資料庫表配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("account"); // 需要生成的表名
// strategy.setExclude("account"); // 需要排除的表名
// strategy.setTablePrefix("t"); //生成實體時去掉表字首
// strategy.setFieldPrefix("legal_finished_trans_"); //生成實體時去掉表字尾
strategy.setChainModel(true); // 鏈式模型
strategy.setEntityLombokModel(true); //是否生成lombok註解
strategy.setRestControllerStyle(true); //生成 @RestController 控制器
strategy.setNaming(NamingStrategy.underline_to_camel); // 資料庫表名與類名對映
strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 資料庫欄位名與屬性對映
strategy.setControllerMappingHyphenStyle(true); // 駝峰轉連字元
strategy.setEntityTableFieldAnnotationEnable(true); // 生成欄位註解
// 自動注入表字段
TableFill create_time = new TableFill("create_time", FieldFill.INSERT);//設定時的生成策略
TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);//設定更新時間的生成策略
strategy.setTableFillList(Arrays.asList(create_time, update_time)); // 自動注入表字段
mpg.setStrategy(strategy); // 配置模板
// TemplateConfig templateConfig = new TemplateConfig();
// templateConfig.setEntity(""); // Java 實體類模板
// templateConfig.setMapper(""); // mapper 模板
// templateConfig.setXml(null); // mapper xml 模板
// templateConfig.setService(""); // Service 類模板
// templateConfig.setServiceImpl(""); // Service impl 實現類模板
// templateConfig.setController(""); // controller 控制器模板
// mpg.setTemplate(templateConfig);
mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 自定義輸出配置
// String templatePath = "/templates/mapper.xml.ftl";
// List<FileOutConfig> focList = new ArrayList<>();
// // 自定義配置會被優先輸出
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定義輸出檔名 , 如果你 Entity 設定了前後綴、此處注意 xml 的名稱會跟著發生變化!!
// return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
// + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
// }
// });
// cfg.setFileCreate(new IFileCreate() {
// @Override
// public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// // 判斷自定義資料夾是否需要建立
// checkDir("呼叫預設方法建立的目錄,自定義目錄用");
// if (fileType == FileType.MAPPER) {
// // 已經生成 mapper 檔案判斷存在,不想重新生成返回 false
// return !new File(filePath).exists();
// }
// // 允許生成模板檔案
// return true;
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg); mpg.execute();
System.out.println("完成");
}
}