|
|
@@ -0,0 +1,87 @@
|
|
|
+package com.mokamrp;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
+import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
+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.NamingStrategy;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class AutoMPPangu {
|
|
|
+ /**
|
|
|
+ * 代码生成 示例代码
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testGenerator() {
|
|
|
+
|
|
|
+ //1. 全局配置
|
|
|
+ String outPutDir = "D://project/moka-private/src/main/java";
|
|
|
+ GlobalConfig config = new GlobalConfig();
|
|
|
+ config.setActiveRecord(true) // 是否支持AR模式
|
|
|
+ .setAuthor("leon") // 作者
|
|
|
+ .setOutputDir(outPutDir) // 生成路径
|
|
|
+ .setFileOverride(true) // 文件覆盖
|
|
|
+ .setIdType(IdType.AUTO) // 主键策略
|
|
|
+ .setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I
|
|
|
+ // IEmployeeService
|
|
|
+ .setBaseResultMap(true) //生成sql映射文件
|
|
|
+ .setBaseColumnList(true);//生成sql片段
|
|
|
+
|
|
|
+ //2. 数据源配置
|
|
|
+ DataSourceConfig dsConfig = new DataSourceConfig();
|
|
|
+ dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
|
|
|
+ .setDriverName("com.mysql.cj.jdbc.Driver")
|
|
|
+ .setUrl("jdbc:mysql://172.18.71.27:3306/mochat")
|
|
|
+ .setUsername("typer")
|
|
|
+ .setPassword("jch9sh_shl");
|
|
|
+
|
|
|
+ //3. 策略配置
|
|
|
+ StrategyConfig stConfig = new StrategyConfig();
|
|
|
+ stConfig.setCapitalMode(true) //全局大写命名
|
|
|
+ .setRestControllerStyle(true) //.RestController
|
|
|
+ .setSuperControllerClass("BaseController")
|
|
|
+ .setColumnNaming(NamingStrategy.underline_to_camel)
|
|
|
+ .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略,下划线转驼峰命名
|
|
|
+ .setTablePrefix("pan_") //表名前缀
|
|
|
+ .setInclude("pan_custservice"); //生成的表
|
|
|
+
|
|
|
+ //4. 包名策略配置
|
|
|
+ PackageConfig pkConfig = new PackageConfig();
|
|
|
+ pkConfig.setParent("com.mokamrp.privates")
|
|
|
+ .setMapper("mapper.pangu")
|
|
|
+ .setService("service.pangu")
|
|
|
+ .setController("controller.pangu")
|
|
|
+ .setEntity("mapper.pangu.pojo")
|
|
|
+ .setXml("mapper.pangu");
|
|
|
+ //5. 注入自定义配置
|
|
|
+ InjectionConfig cfg = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ this.setMap(map);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ //6. 整合配置
|
|
|
+ AutoGenerator ag = new AutoGenerator();
|
|
|
+ ag.setGlobalConfig(config)
|
|
|
+ .setDataSource(dsConfig)
|
|
|
+ .setStrategy(stConfig)
|
|
|
+ .setPackageInfo(pkConfig);
|
|
|
+ //. @leon 自定义模板
|
|
|
+ TemplateConfig tc = new TemplateConfig();
|
|
|
+// tc.setService("/template/service.java.vm");
|
|
|
+// tc.setServiceImpl("/template/serviceImpl.java.vm");
|
|
|
+ tc.setController("/template/controller.java.vm");
|
|
|
+ ag.setTemplate(tc);
|
|
|
+ //7. 执行
|
|
|
+ ag.execute();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|