Commit a3ac934dcc596cf24ada264ddf4d8796a6972ec6
Exists in
master
Merge branch 'master' of https://git.healthbaby.com.cn/jiangjiazhi/center
Showing 2 changed files
parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/.gitignore
View file @
a3ac934
parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java
View file @
a3ac934
| 1 | +/** | |
| 2 | + * Copyright (c) 2011-2016, hubin (jobob@qq.com). | |
| 3 | + * | |
| 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 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 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.HashMap; | |
| 19 | +import java.util.Map; | |
| 20 | + | |
| 21 | +import org.junit.Test; | |
| 22 | + | |
| 23 | +import com.baomidou.mybatisplus.generator.AutoGenerator; | |
| 24 | +import com.baomidou.mybatisplus.generator.InjectionConfig; | |
| 25 | +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; | |
| 26 | +import com.baomidou.mybatisplus.generator.config.GlobalConfig; | |
| 27 | +import com.baomidou.mybatisplus.generator.config.PackageConfig; | |
| 28 | +import com.baomidou.mybatisplus.generator.config.StrategyConfig; | |
| 29 | +import com.baomidou.mybatisplus.generator.config.rules.DbType; | |
| 30 | +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; | |
| 31 | +import com.baomidou.mybatisplus.mapper.EntityWrapper; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * <p> | |
| 35 | + * 代码生成器演示 | |
| 36 | + * </p> | |
| 37 | + * | |
| 38 | + * | |
| 39 | + * | |
| 40 | + * @author hubin | |
| 41 | + * @date 2016-12-01 | |
| 42 | + */ | |
| 43 | +public class MysqlGenerator { | |
| 44 | + | |
| 45 | + private static void customSet(GlobalConfig gc, StrategyConfig strategy, PackageConfig pc) { | |
| 46 | + gc.setOutputDir("E://mybatis-plus-generate"); | |
| 47 | + gc.setAuthor("maliang"); | |
| 48 | + | |
| 49 | + // strategy.setInclude(new String[] { | |
| 50 | + // "SYS_USERS","SYS_USER_ROLE_MAPS","SYS_ROLES" }); // 需要生成的表 | |
| 51 | + strategy.setInclude(new String[] { "HIGHRISK_VERSION" }); // 需要生成的表 | |
| 52 | + // pc.setParent("com.lyms.hospital"); | |
| 53 | + pc.setParent("com.lyms.base.common"); | |
| 54 | + | |
| 55 | + pc.setFunctionName("conf"); // com.lyms.hospital.web.controller.sys | |
| 56 | + // 加在controller后面 | |
| 57 | + // pc.setModuleName("sys"); //com.lyms.hospital.sys.controller | |
| 58 | + // 加在controller前面 | |
| 59 | + | |
| 60 | + // 以下基本不变 | |
| 61 | + pc.setServiceImpl(pc.getService() + ".impl"); | |
| 62 | + pc.setMapper("dao"); | |
| 63 | + pc.setXml("dao"); | |
| 64 | + } | |
| 65 | + | |
| 66 | + @Test | |
| 67 | + public void entityWarpperTest() { | |
| 68 | + EntityWrapper<Object> ew = new EntityWrapper<Object>(); | |
| 69 | + ew.where("name={0}", "'zhangsan'").and("id=1").orNew("status={0}", "0").or("status=1") | |
| 70 | + .notLike("nlike", "notvalue").andNew("new=xx").like("hhh", "ddd").andNew("pwd=11").isNotNull("n1,n2") | |
| 71 | + .isNull("n3").groupBy("x1").groupBy("x2,x3").having("x1=11").having("x3=433").orderBy("dd") | |
| 72 | + .orderBy("d1,d2"); | |
| 73 | + System.out.println(ew.getSqlSegment()); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * <p> | |
| 78 | + * MySQL 生成演示 | |
| 79 | + * </p> | |
| 80 | + */ | |
| 81 | + public static void main(String[] args) { | |
| 82 | + AutoGenerator mpg = new AutoGenerator(); | |
| 83 | + | |
| 84 | + // 全局配置 | |
| 85 | + GlobalConfig gc = new GlobalConfig(); | |
| 86 | + gc.setOutputDir("E://mybatis-plus-generate"); | |
| 87 | + gc.setFileOverride(true); | |
| 88 | + gc.setActiveRecord(false); | |
| 89 | + gc.setEnableCache(false);// XML 二级缓存 | |
| 90 | + gc.setBaseResultMap(true);// XML ResultMap | |
| 91 | + gc.setBaseColumnList(true);// XML columList | |
| 92 | + // gc.setAuthor("Yanghu"); | |
| 93 | + | |
| 94 | + // 自定义文件命名,注意 %s 会自动填充表实体属性! | |
| 95 | + // gc.setMapperName("%sDao"); | |
| 96 | + // gc.setXmlName("%sDao"); | |
| 97 | + gc.setServiceName("%sService"); | |
| 98 | + // gc.setServiceImplName("%sServiceDiy"); | |
| 99 | + // gc.setControllerName("%sAction"); | |
| 100 | + mpg.setGlobalConfig(gc); | |
| 101 | + | |
| 102 | + // 数据源配置 | |
| 103 | + DataSourceConfig dsc = new DataSourceConfig(); | |
| 104 | + dsc.setDbType(DbType.MYSQL); | |
| 105 | + dsc.setDriverName("com.mysql.jdbc.Driver"); | |
| 106 | + dsc.setUsername("hospital"); | |
| 107 | + dsc.setPassword("lyms2015"); | |
| 108 | + dsc.setUrl("jdbc:mysql://119.90.57.26:3306/hospital?characterEncoding=utf8"); | |
| 109 | + mpg.setDataSource(dsc); | |
| 110 | + | |
| 111 | + // 策略配置 | |
| 112 | + StrategyConfig strategy = new StrategyConfig(); | |
| 113 | + // strategy.setTablePrefix("bmd_");// 此处可以修改为您的表前缀 | |
| 114 | + strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 | |
| 115 | + strategy.setInclude(new String[] { "user" }); // 需要生成的表 | |
| 116 | + // strategy.setExclude(new String[]{"test"}); // 排除生成的表 | |
| 117 | + // 字段名生成策略 | |
| 118 | + strategy.setFieldNaming(NamingStrategy.underline_to_camel); | |
| 119 | + // 自定义实体父类 | |
| 120 | + // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); | |
| 121 | + // 自定义实体,公共字段 | |
| 122 | + // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); | |
| 123 | + // 自定义 mapper 父类 | |
| 124 | + // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); | |
| 125 | + // 自定义 service 父类 | |
| 126 | + strategy.setSuperServiceClass("com.lyms.web.service.BaseService"); | |
| 127 | + // 自定义 service 实现类父类 | |
| 128 | + // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); | |
| 129 | + // 自定义 controller 父类 | |
| 130 | + strategy.setSuperControllerClass("com.lyms.web.controller.BaseController"); | |
| 131 | + // 【实体】是否生成字段常量(默认 false) | |
| 132 | + // public static final String ID = "test_id"; | |
| 133 | + // strategy.setEntityColumnConstant(true); | |
| 134 | + // 【实体】是否为构建者模型(默认 false) | |
| 135 | + // public User setName(String name) {this.name = name; return this;} | |
| 136 | + // strategy.setEntityBuliderModel(true); | |
| 137 | + mpg.setStrategy(strategy); | |
| 138 | + | |
| 139 | + // 包配置 | |
| 140 | + PackageConfig pc = new PackageConfig(); | |
| 141 | + pc.setController("controller"); | |
| 142 | + // pc.setModuleName("test"); | |
| 143 | + mpg.setPackageInfo(pc); | |
| 144 | + | |
| 145 | + // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 | |
| 146 | + InjectionConfig cfg = new InjectionConfig() { | |
| 147 | + @Override | |
| 148 | + public void initMap() { | |
| 149 | + Map<String, Object> map = new HashMap<String, Object>(); | |
| 150 | + map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); | |
| 151 | + this.setMap(map); | |
| 152 | + } | |
| 153 | + }; | |
| 154 | + mpg.setCfg(cfg); | |
| 155 | + | |
| 156 | + // 自定义模板配置 | |
| 157 | + // TemplateConfig tc = new TemplateConfig(); | |
| 158 | + // tc.setController("..."); | |
| 159 | + // tc.setEntity("..."); | |
| 160 | + // tc.setMapper("template/mapper.java.vm"); | |
| 161 | + // tc.setXml("..."); | |
| 162 | + // tc.setService("..."); | |
| 163 | + // tc.setServiceImpl("..."); | |
| 164 | + // mpg.setTemplate(tc); | |
| 165 | + | |
| 166 | + customSet(gc, strategy, pc);// 自定义,一般清空修改这个就可以了 | |
| 167 | + | |
| 168 | + // 执行生成 | |
| 169 | + mpg.execute(); | |
| 170 | + | |
| 171 | + // 打印注入设置 | |
| 172 | + System.err.println(mpg.getCfg().getMap().get("abc")); | |
| 173 | + } | |
| 174 | + | |
| 175 | +} |