MP代码生成器
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率
gitee地址:https://gitee.com/jitheima/mp_generator.git
1、CodeGenerator核心类
下面我们看下餐掌柜平台中是如何集成AutoGenerator ,首相我们找CodeGenerator类,目录如下
在CodeGenerator中我们使用了AutoGenerator,下面我们逐行解释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| package com.itheima.restkeeper.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.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import com.itheima.restkeeper.utils.EmptyUtil;
import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle;
public class CodeGenerator {
public static void autoGenerator(){
final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus-generrator");
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig(); String projectPath =rb.getString("projectPath"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor(rb.getString("author")); gc.setOpen(false); gc.setFileOverride(true); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(true); mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(rb.getString("url")); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername(rb.getString("userName")); dsc.setPassword(rb.getString("password")); mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig(); pc.setModuleName(rb.getString("moduleName")); pc.setParent(rb.getString("parent")); pc.setController("web"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setEntity("pojo"); pc.setMapper("mapper"); mpg.setPackageInfo(pc);
InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { } };
String templatePath = "/templates/mapper.xml.ftl";
List<FileOutConfig> focList = new ArrayList<>();
cfg.setFileOutConfigList(focList); mpg.setCfg(cfg);
TemplateConfig templateConfig = new TemplateConfig(); if ("true".equals(rb.getString("entity"))){ String entityFtlPath = rb.getString("entity.ftl.path"); if (!EmptyUtil.isNullOrEmpty(entityFtlPath)){ templateConfig.setEntity(entityFtlPath); } }else { templateConfig.setEntity(null); }
if ("true".equals(rb.getString("mapper"))){ String mapperFtlPath = rb.getString("mapper.ftl.path"); if (!EmptyUtil.isNullOrEmpty(mapperFtlPath)){ templateConfig.setMapper(mapperFtlPath); } }else { templateConfig.setMapper(null); }
if ("true".equals(rb.getString("service"))){ String serviceFtlPath = rb.getString("service.ftl.path"); if (!EmptyUtil.isNullOrEmpty(serviceFtlPath)){ templateConfig.setService(serviceFtlPath); } }else { templateConfig.setService(null); }
if ("true".equals(rb.getString("serviceImp"))){ String serviceImpFtlPath = rb.getString("serviceImp.ftl.path"); if (!EmptyUtil.isNullOrEmpty(serviceImpFtlPath)){ templateConfig.setServiceImpl(serviceImpFtlPath); } }else { templateConfig.setServiceImpl(null); }
if ("true".equals(rb.getString("controller"))){ String controllerFtlPath = rb.getString("controller.ftl.path"); if (!EmptyUtil.isNullOrEmpty(controllerFtlPath)){ templateConfig.setController(controllerFtlPath); } }else { templateConfig.setController(null); } templateConfig.setXml(null); mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperEntityClass(rb.getString("SuperEntityClass")); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); String[] SuperEntityColumns = rb.getString("superEntityColumns").split(","); strategy.setSuperEntityColumns(SuperEntityColumns); strategy.setInclude(rb.getString("tableName").split(",")); strategy.setControllerMappingHyphenStyle(true); String tablePrefix = rb.getString("tablePrefix"); if (tablePrefix!=null){ strategy.setTablePrefix(tablePrefix); } mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute();
} }
|
2、生成器快速入门
上面我们的定义了CodeGenerator核心类,下面我们需要在项目中使用,各位在使用代码生成器之前,需要明确我们使用的模块,这里我们所有的pojo、mapper、service层都定义在==model-***-service==类型的模块中,这里以model-shop-service为例,其他模块使用方式也类似,在使用之前我们需要定义2个资源:
- ==mybatis-plus-generrator.properties:关于数据库及生成策略定义==
- ==templates:代码生成器模板信息==
,定义信息如下:
2.1、generrator
generrator.properties此文件的作用主要是定义关于数据库及生成策略定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| url=jdbc:mysql://192.168.112.77:3306/restkeeper-shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false
userName=root
password=root
projectPath=F:/restkeeper-prent/restkeeper-super/restkeeper-model-shop/model-shop-service
author=Admin
parent=com.itheima
moduleName=restkeeper
tablePrefix =tab_
tableName=tab_brand,tab_category,tab_dish,tab_dish_flavor,tab_order,tab_order_item,tab_printer,tab_printer_dish,tab_store,tab_table,tab_table_area
SuperEntityClass = com.itheima.restkeeper.basic.BasicPojo
superEntityColumns = id,created_time,updated_time,sharding_id,enable_flag
entity=true entity.ftl.path=/templates/entity.java mapper=false mapper.ftl.path=/templates/mapper.java service=false service.ftl.path=/templates/service.java serviceImp=false serviceImp.ftl.path=/templates/serviceImpl.java controller=false controller.ftl.path=/templates/controller.java
|
2.2、templates
templates里面的内容如下,其主要作用是根据模板生成对应代码,当然这里的文件不需要各位维护,如果你想维护,请先学习freemarker模板引擎,这里不做讲解
2.3、生成器使用
直接在test目录中简历单元测试类直接执行即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.itheima.restkeeper;
import com.itheima.restkeeper.generator.CodeGenerator; import org.junit.Test;
public class ShopGenerator {
@Test public void test(){ CodeGenerator.autoGenerator(); } }
|
这时我们查看日志可以发现在model-shop-service模块中代码已经自动生成