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("完成");
}
}