From ab0d6fc03b83af59309efa395ab9b06ad8666d4b Mon Sep 17 00:00:00 2001 From: xujiahong Date: Wed, 5 Apr 2017 11:29:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=BD=E7=95=A5MysqlGenerator.java=E7=9A=84?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baomidou/mybatisplus/test/generator/.gitignore | 1 + .../mybatisplus/test/generator/MysqlGenerator.java | 350 ++++++++++----------- 2 files changed, 176 insertions(+), 175 deletions(-) create mode 100644 parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/.gitignore diff --git a/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/.gitignore b/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/.gitignore new file mode 100644 index 0000000..f5d5a21 --- /dev/null +++ b/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/.gitignore @@ -0,0 +1 @@ +/MysqlGenerator.java diff --git a/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java b/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java index 9d7b1f1..6e7d825 100644 --- a/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java +++ b/parent/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java @@ -1,175 +1,175 @@ -/** - * Copyright (c) 2011-2016, hubin (jobob@qq.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.baomidou.mybatisplus.test.generator; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; - -import com.baomidou.mybatisplus.generator.AutoGenerator; -import com.baomidou.mybatisplus.generator.InjectionConfig; -import com.baomidou.mybatisplus.generator.config.DataSourceConfig; -import com.baomidou.mybatisplus.generator.config.GlobalConfig; -import com.baomidou.mybatisplus.generator.config.PackageConfig; -import com.baomidou.mybatisplus.generator.config.StrategyConfig; -import com.baomidou.mybatisplus.generator.config.rules.DbType; -import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; -import com.baomidou.mybatisplus.mapper.EntityWrapper; - -/** - *

- * 代码生成器演示 - *

- * - * - * - * @author hubin - * @date 2016-12-01 - */ -public class MysqlGenerator { - - private static void customSet(GlobalConfig gc, StrategyConfig strategy, PackageConfig pc) { - gc.setOutputDir("E://mybatis-plus-generate"); - gc.setAuthor("fangcheng"); - - // strategy.setInclude(new String[] { - // "SYS_USERS","SYS_USER_ROLE_MAPS","SYS_ROLES" }); // 需要生成的表 - strategy.setInclude(new String[] { "DIAGNOSE_VERSION", "DIAGNOSE_SOURCE", "DIAGNOSE_CONF" }); // 需要生成的表 - // pc.setParent("com.lyms.hospital"); - pc.setParent("com.lyms.base.common"); - - pc.setFunctionName("conf"); // com.lyms.hospital.web.controller.sys - // 加在controller后面 - // pc.setModuleName("sys"); //com.lyms.hospital.sys.controller - // 加在controller前面 - - // 以下基本不变 - pc.setServiceImpl(pc.getService() + ".impl"); - pc.setMapper("dao"); - pc.setXml("dao"); - } - - @Test - public void entityWarpperTest() { - EntityWrapper ew = new EntityWrapper(); - ew.where("name={0}", "'zhangsan'").and("id=1").orNew("status={0}", "0").or("status=1") - .notLike("nlike", "notvalue").andNew("new=xx").like("hhh", "ddd").andNew("pwd=11").isNotNull("n1,n2") - .isNull("n3").groupBy("x1").groupBy("x2,x3").having("x1=11").having("x3=433").orderBy("dd") - .orderBy("d1,d2"); - System.out.println(ew.getSqlSegment()); - } - - /** - *

- * MySQL 生成演示 - *

- */ - public static void main(String[] args) { - AutoGenerator mpg = new AutoGenerator(); - - // 全局配置 - GlobalConfig gc = new GlobalConfig(); - gc.setOutputDir("E://mybatis-plus-generate"); - gc.setFileOverride(true); - gc.setActiveRecord(false); - gc.setEnableCache(false);// XML 二级缓存 - gc.setBaseResultMap(true);// XML ResultMap - gc.setBaseColumnList(true);// XML columList - // gc.setAuthor("Yanghu"); - - // 自定义文件命名,注意 %s 会自动填充表实体属性! - // gc.setMapperName("%sDao"); - // gc.setXmlName("%sDao"); - gc.setServiceName("%sService"); - // gc.setServiceImplName("%sServiceDiy"); - // gc.setControllerName("%sAction"); - mpg.setGlobalConfig(gc); - - // 数据源配置 - DataSourceConfig dsc = new DataSourceConfig(); - dsc.setDbType(DbType.MYSQL); - dsc.setDriverName("com.mysql.jdbc.Driver"); - dsc.setUsername("hospital"); - dsc.setPassword("lyms2015"); - dsc.setUrl("jdbc:mysql://119.90.57.26:3306/hospital?characterEncoding=utf8"); - mpg.setDataSource(dsc); - - // 策略配置 - StrategyConfig strategy = new StrategyConfig(); - // strategy.setTablePrefix("bmd_");// 此处可以修改为您的表前缀 - strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 - strategy.setInclude(new String[] { "user" }); // 需要生成的表 - // strategy.setExclude(new String[]{"test"}); // 排除生成的表 - // 字段名生成策略 - strategy.setFieldNaming(NamingStrategy.underline_to_camel); - // 自定义实体父类 - // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); - // 自定义实体,公共字段 - // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); - // 自定义 mapper 父类 - // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); - // 自定义 service 父类 - strategy.setSuperServiceClass("com.lyms.web.service.BaseService"); - // 自定义 service 实现类父类 - // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); - // 自定义 controller 父类 - strategy.setSuperControllerClass("com.lyms.web.controller.BaseController"); - // 【实体】是否生成字段常量(默认 false) - // public static final String ID = "test_id"; - // strategy.setEntityColumnConstant(true); - // 【实体】是否为构建者模型(默认 false) - // public User setName(String name) {this.name = name; return this;} - // strategy.setEntityBuliderModel(true); - mpg.setStrategy(strategy); - - // 包配置 - PackageConfig pc = new PackageConfig(); - pc.setController("controller"); - // pc.setModuleName("test"); - mpg.setPackageInfo(pc); - - // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 - InjectionConfig cfg = new InjectionConfig() { - @Override - public void initMap() { - Map map = new HashMap(); - map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); - this.setMap(map); - } - }; - mpg.setCfg(cfg); - - // 自定义模板配置 - // TemplateConfig tc = new TemplateConfig(); - // tc.setController("..."); - // tc.setEntity("..."); - // tc.setMapper("template/mapper.java.vm"); - // tc.setXml("..."); - // tc.setService("..."); - // tc.setServiceImpl("..."); - // mpg.setTemplate(tc); - - customSet(gc, strategy, pc);// 自定义,一般清空修改这个就可以了 - - // 执行生成 - mpg.execute(); - - // 打印注入设置 - System.err.println(mpg.getCfg().getMap().get("abc")); - } - -} +/** + * Copyright (c) 2011-2016, hubin (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.baomidou.mybatisplus.test.generator; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import com.baomidou.mybatisplus.generator.AutoGenerator; +import com.baomidou.mybatisplus.generator.InjectionConfig; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.GlobalConfig; +import com.baomidou.mybatisplus.generator.config.PackageConfig; +import com.baomidou.mybatisplus.generator.config.StrategyConfig; +import com.baomidou.mybatisplus.generator.config.rules.DbType; +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; +import com.baomidou.mybatisplus.mapper.EntityWrapper; + +/** + *

+ * 代码生成器演示 + *

+ * + * + * + * @author hubin + * @date 2016-12-01 + */ +public class MysqlGenerator { + + private static void customSet(GlobalConfig gc, StrategyConfig strategy, PackageConfig pc) { + gc.setOutputDir("E://mybatis-plus-generate"); + gc.setAuthor("fangcheng"); + + // strategy.setInclude(new String[] { + // "SYS_USERS","SYS_USER_ROLE_MAPS","SYS_ROLES" }); // 需要生成的表 + strategy.setInclude(new String[] { "HISTORY_SOURCE"}); // 需要生成的表 + // pc.setParent("com.lyms.hospital"); + pc.setParent("com.lyms.hospital"); + + pc.setFunctionName("history"); // com.lyms.hospital.web.controller.sys + // 加在controller后面 + // pc.setModuleName("sys"); //com.lyms.hospital.sys.controller + // 加在controller前面 + + // 以下基本不变 + pc.setServiceImpl(pc.getService() + ".impl"); + pc.setMapper("dao"); + pc.setXml("dao"); + } + + @Test + public void entityWarpperTest() { + EntityWrapper ew = new EntityWrapper(); + ew.where("name={0}", "'zhangsan'").and("id=1").orNew("status={0}", "0").or("status=1") + .notLike("nlike", "notvalue").andNew("new=xx").like("hhh", "ddd").andNew("pwd=11").isNotNull("n1,n2") + .isNull("n3").groupBy("x1").groupBy("x2,x3").having("x1=11").having("x3=433").orderBy("dd") + .orderBy("d1,d2"); + System.out.println(ew.getSqlSegment()); + } + + /** + *

+ * MySQL 生成演示 + *

+ */ + public static void main(String[] args) { + AutoGenerator mpg = new AutoGenerator(); + + // 全局配置 + GlobalConfig gc = new GlobalConfig(); + gc.setOutputDir("E://mybatis-plus-generate"); + gc.setFileOverride(true); + gc.setActiveRecord(false); + gc.setEnableCache(false);// XML 二级缓存 + gc.setBaseResultMap(true);// XML ResultMap + gc.setBaseColumnList(true);// XML columList + // gc.setAuthor("Yanghu"); + + // 自定义文件命名,注意 %s 会自动填充表实体属性! + // gc.setMapperName("%sDao"); + // gc.setXmlName("%sDao"); + gc.setServiceName("%sService"); + // gc.setServiceImplName("%sServiceDiy"); + // gc.setControllerName("%sAction"); + mpg.setGlobalConfig(gc); + + // 数据源配置 + DataSourceConfig dsc = new DataSourceConfig(); + dsc.setDbType(DbType.MYSQL); + dsc.setDriverName("com.mysql.jdbc.Driver"); + dsc.setUsername("hospital"); + dsc.setPassword("lyms2015"); + dsc.setUrl("jdbc:mysql://119.90.57.26:3306/hospital?characterEncoding=utf8"); + mpg.setDataSource(dsc); + + // 策略配置 + StrategyConfig strategy = new StrategyConfig(); + // strategy.setTablePrefix("bmd_");// 此处可以修改为您的表前缀 + strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 + strategy.setInclude(new String[] { "user" }); // 需要生成的表 + // strategy.setExclude(new String[]{"test"}); // 排除生成的表 + // 字段名生成策略 + strategy.setFieldNaming(NamingStrategy.underline_to_camel); + // 自定义实体父类 + // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); + // 自定义实体,公共字段 + // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); + // 自定义 mapper 父类 + // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); + // 自定义 service 父类 + strategy.setSuperServiceClass("com.lyms.web.service.BaseService"); + // 自定义 service 实现类父类 + // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); + // 自定义 controller 父类 + strategy.setSuperControllerClass("com.lyms.web.controller.BaseController"); + // 【实体】是否生成字段常量(默认 false) + // public static final String ID = "test_id"; + // strategy.setEntityColumnConstant(true); + // 【实体】是否为构建者模型(默认 false) + // public User setName(String name) {this.name = name; return this;} + // strategy.setEntityBuliderModel(true); + mpg.setStrategy(strategy); + + // 包配置 + PackageConfig pc = new PackageConfig(); + pc.setController("controller"); + // pc.setModuleName("test"); + mpg.setPackageInfo(pc); + + // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 + InjectionConfig cfg = new InjectionConfig() { + @Override + public void initMap() { + Map map = new HashMap(); + map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); + this.setMap(map); + } + }; + mpg.setCfg(cfg); + + // 自定义模板配置 + // TemplateConfig tc = new TemplateConfig(); + // tc.setController("..."); + // tc.setEntity("..."); + // tc.setMapper("template/mapper.java.vm"); + // tc.setXml("..."); + // tc.setService("..."); + // tc.setServiceImpl("..."); + // mpg.setTemplate(tc); + + customSet(gc, strategy, pc);// 自定义,一般清空修改这个就可以了 + + // 执行生成 + mpg.execute(); + + // 打印注入设置 + System.err.println(mpg.getCfg().getMap().get("abc")); + } + +} -- 1.8.3.1