|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2020, baomidou (jobob@qq.com). |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.baomidou.mybatisplus.test.generator; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import com.baomidou.mybatisplus.annotation.DbType; |
| 24 | +import com.baomidou.mybatisplus.annotation.IdType; |
| 25 | +import com.baomidou.mybatisplus.core.toolkit.StringPool; |
| 26 | +import com.baomidou.mybatisplus.generator.AutoGenerator; |
| 27 | +import com.baomidou.mybatisplus.generator.InjectionConfig; |
| 28 | +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; |
| 29 | +import com.baomidou.mybatisplus.generator.config.FileOutConfig; |
| 30 | +import com.baomidou.mybatisplus.generator.config.GlobalConfig; |
| 31 | +import com.baomidou.mybatisplus.generator.config.PackageConfig; |
| 32 | +import com.baomidou.mybatisplus.generator.config.StrategyConfig; |
| 33 | +import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert; |
| 34 | +import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
| 35 | +import com.baomidou.mybatisplus.generator.config.rules.IColumnType; |
| 36 | +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; |
| 37 | +import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; |
| 38 | + |
| 39 | +/** |
| 40 | + * KingbaseESGenerator |
| 41 | + * |
| 42 | + * @author kingbase |
| 43 | + * @since 2019/10/12 |
| 44 | + */ |
| 45 | +public class KingbaseESGenerator extends GeneratorTest { |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + int result = scanner(); |
| 49 | + AutoGenerator mpg = new AutoGenerator(); |
| 50 | + |
| 51 | + // 全局配置 |
| 52 | + GlobalConfig gc = new GlobalConfig(); |
| 53 | + gc.setOutputDir("D://"); |
| 54 | + gc.setFileOverride(true); |
| 55 | + gc.setActiveRecord(true);// 开启 activeRecord 模式 |
| 56 | + gc.setEnableCache(false);// XML 二级缓存 |
| 57 | + gc.setBaseResultMap(true);// XML ResultMap |
| 58 | + gc.setBaseColumnList(false);// XML columList |
| 59 | + //gc.setKotlin(true); // 是否生成 kotlin 代码 |
| 60 | + //gc.setSwagger2(true); // 是否生成 Swagger2 注解 |
| 61 | + gc.setAuthor("kingbase"); |
| 62 | + gc.setIdType(IdType.AUTO); |
| 63 | + |
| 64 | + // 自定义文件命名,注意 %s 会自动填充表实体属性! |
| 65 | + // gc.setEntityName("%sEntity"); |
| 66 | + // gc.setMapperName("%sDao"); |
| 67 | + // gc.setXmlName("%sDao"); |
| 68 | + // gc.setServiceName("MP%sService"); |
| 69 | + // gc.setServiceImplName("%sServiceDiy"); |
| 70 | + // gc.setControllerName("%sAction"); |
| 71 | + mpg.setGlobalConfig(gc); |
| 72 | + |
| 73 | + // 数据源配置 |
| 74 | + DataSourceConfig dsc = new DataSourceConfig(); |
| 75 | + dsc.setSchemaName("PUBLIC");// 指定 SCHEMA |
| 76 | + dsc.setDbType(DbType.KINGBASE_ES); |
| 77 | + dsc.setTypeConvert(new OracleTypeConvert() { |
| 78 | + // 自定义数据库表字段类型转换【可选】 |
| 79 | + @Override |
| 80 | + public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) { |
| 81 | + System.out.println("转换类型:" + fieldType); |
| 82 | + return super.processTypeConvert(globalConfig, fieldType); |
| 83 | + } |
| 84 | + }); |
| 85 | + // 自定义数据库信息查询 |
| 86 | + dsc.setDbQuery(new MyKingbaseESQuery()); |
| 87 | + dsc.setDriverName("com.kingbase8.Driver"); |
| 88 | + dsc.setUsername("SYSTEM"); |
| 89 | + dsc.setPassword("123456"); |
| 90 | + dsc.setUrl("jdbc:kingbase8://localhost:54321/mybatis-plus"); |
| 91 | + mpg.setDataSource(dsc); |
| 92 | + |
| 93 | + // 策略配置 |
| 94 | + StrategyConfig strategy = new StrategyConfig(); |
| 95 | + // strategy.setCapitalMode(true);// 全局大写命名 |
| 96 | + // strategy.setDbColumnUnderline(true);// 全局下划线命名 |
| 97 | + strategy.setTablePrefix("BMD_", "MP_");// 表前缀 |
| 98 | + strategy.setFieldPrefix("A_"); |
| 99 | + strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 |
| 100 | + strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 允许字段策略独立设置,默认为 naming 策略 |
| 101 | + strategy.setInclude("T_USER", "^MP.*", "OK"); // 需要生成的表,支持正则表达式 |
| 102 | + // strategy.setExclude("test"); // 排除生成的表,支持正则表达式 |
| 103 | + // 自定义实体父类 |
| 104 | + // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); |
| 105 | + // 自定义实体,公共字段 |
| 106 | + // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); |
| 107 | + // 自定义 mapper 父类 |
| 108 | + // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); |
| 109 | + // 自定义 service 父类 |
| 110 | + // strategy.setSuperServiceClass("com.baomidou.demo.TestService"); |
| 111 | + // 自定义 service 实现类父类 |
| 112 | + // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); |
| 113 | + // 自定义 controller 父类 |
| 114 | + // strategy.setSuperControllerClass("com.baomidou.demo.TestController"); |
| 115 | + // 【实体】是否生成字段常量(默认 false) |
| 116 | + // public static final String ID = "test_id"; |
| 117 | + // strategy.setEntityColumnConstant(true); |
| 118 | + // 【实体】是否为构建者模型(默认 false) |
| 119 | + // public User setName(String name) {this.name = name; return this;} |
| 120 | + // strategy.setEntityBuliderModel(true); |
| 121 | + mpg.setStrategy(strategy); |
| 122 | + |
| 123 | + // 包配置 |
| 124 | + PackageConfig pc = new PackageConfig(); |
| 125 | + pc.setModuleName("test"); |
| 126 | + pc.setParent("com.baomidou");// 自定义包路径 |
| 127 | + pc.setController("controller");// 这里是控制器包名,默认 web |
| 128 | + mpg.setPackageInfo(pc); |
| 129 | + |
| 130 | + // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 |
| 131 | + InjectionConfig cfg = new InjectionConfig() { |
| 132 | + @Override |
| 133 | + public void initMap() { |
| 134 | + Map<String, Object> map = new HashMap<>(); |
| 135 | + map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); |
| 136 | + this.setMap(map); |
| 137 | + } |
| 138 | + }; |
| 139 | + List<FileOutConfig> focList = new ArrayList<>(); |
| 140 | + focList.add(new FileOutConfig("/templates/dto.java" + ((1 == result) ? ".ftl" : ".vm")) { |
| 141 | + @Override |
| 142 | + public String outputFile(TableInfo tableInfo) { |
| 143 | + // 自定义输入文件名称 |
| 144 | + return "D://test/my_" + tableInfo.getEntityName() + StringPool.DOT_JAVA; |
| 145 | + } |
| 146 | + }); |
| 147 | + cfg.setFileOutConfigList(focList); |
| 148 | + mpg.setCfg(cfg); |
| 149 | + |
| 150 | + // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy |
| 151 | + // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置: |
| 152 | + // TemplateConfig tc = new TemplateConfig(); |
| 153 | + // tc.setController("..."); |
| 154 | + // tc.setEntity("..."); |
| 155 | + // tc.setMapper("..."); |
| 156 | + // tc.setXml("..."); |
| 157 | + // tc.setService("..."); |
| 158 | + // tc.setServiceImpl("..."); |
| 159 | + // mpg.setTemplate(tc); |
| 160 | + |
| 161 | + // 执行生成 |
| 162 | + if (1 == result) { |
| 163 | + mpg.setTemplateEngine(new FreemarkerTemplateEngine()); |
| 164 | + } |
| 165 | + mpg.execute(); |
| 166 | + // 打印注入设置 |
| 167 | + System.err.println(mpg.getCfg().getMap().get("abc")); |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments