|
| 1 | +package com.dashuai.learning.mybatisplus; |
| 2 | + |
| 3 | +import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; |
| 4 | +import com.baomidou.mybatisplus.core.toolkit.StringPool; |
| 5 | +import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| 6 | +import com.baomidou.mybatisplus.generator.AutoGenerator; |
| 7 | +import com.baomidou.mybatisplus.generator.InjectionConfig; |
| 8 | +import com.baomidou.mybatisplus.generator.config.*; |
| 9 | +import com.baomidou.mybatisplus.generator.config.po.TableInfo; |
| 10 | +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Scanner; |
| 15 | + |
| 16 | +public class CodeGenerator { |
| 17 | + /** |
| 18 | + * <p> |
| 19 | + * 读取控制台内容 |
| 20 | + * </p> |
| 21 | + */ |
| 22 | + public static String scanner(String tip) { |
| 23 | + Scanner scanner = new Scanner(System.in); |
| 24 | + System.out.println("请输入" + tip + ":"); |
| 25 | + if (scanner.hasNext()) { |
| 26 | + String ipt = scanner.next(); |
| 27 | + if (StringUtils.isNotEmpty(ipt)) { |
| 28 | + return ipt; |
| 29 | + } |
| 30 | + } |
| 31 | + throw new MybatisPlusException("请输入正确的" + tip + "!"); |
| 32 | + } |
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + // 代码生成器 |
| 36 | + AutoGenerator mpg = new AutoGenerator(); |
| 37 | + |
| 38 | + // 全局配置 |
| 39 | + GlobalConfig gc = new GlobalConfig(); |
| 40 | + String projectPath = System.getProperty("user.dir"); |
| 41 | + gc.setOutputDir(projectPath + "/SpringBoot-Mybatis-Plus/src/main/java"); |
| 42 | + gc.setAuthor("zoey"); |
| 43 | + gc.setOpen(false); |
| 44 | + // gc.setSwagger2(true); 实体属性 Swagger2 注解 |
| 45 | + mpg.setGlobalConfig(gc); |
| 46 | + |
| 47 | + // 数据源配置 |
| 48 | + DataSourceConfig dsc = new DataSourceConfig(); |
| 49 | + dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8"); |
| 50 | + // dsc.setSchemaName("public"); |
| 51 | + dsc.setDriverName("com.mysql.jdbc.Driver"); |
| 52 | + dsc.setUsername("root"); |
| 53 | + dsc.setPassword("root"); |
| 54 | + mpg.setDataSource(dsc); |
| 55 | + |
| 56 | + // 包配置 |
| 57 | + PackageConfig pc = new PackageConfig(); |
| 58 | + pc.setModuleName(scanner("模块名")); |
| 59 | + pc.setParent("com.dashuai.learning.mybatisplus"); |
| 60 | + mpg.setPackageInfo(pc); |
| 61 | + |
| 62 | + // 自定义配置 |
| 63 | + InjectionConfig cfg = new InjectionConfig() { |
| 64 | + @Override |
| 65 | + public void initMap() { |
| 66 | + // to do nothing |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // 如果模板引擎是 freemarker |
| 71 | +// String templatePath = "/templates/mapper.xml.ftl"; |
| 72 | + // 如果模板引擎是 velocity |
| 73 | + String templatePath = "/templates/mapper.xml.vm"; |
| 74 | + |
| 75 | + // 自定义输出配置 |
| 76 | + List<FileOutConfig> focList = new ArrayList<>(); |
| 77 | + // 自定义配置会被优先输出 |
| 78 | + focList.add(new FileOutConfig(templatePath) { |
| 79 | + @Override |
| 80 | + public String outputFile(TableInfo tableInfo) { |
| 81 | + // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! |
| 82 | + return projectPath + "/SpringBoot-Mybatis-Plus/src/main/resources/mapper/" + pc.getModuleName() |
| 83 | + + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; |
| 84 | + } |
| 85 | + }); |
| 86 | + /* |
| 87 | + cfg.setFileCreate(new IFileCreate() { |
| 88 | + @Override |
| 89 | + public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { |
| 90 | + // 判断自定义文件夹是否需要创建 |
| 91 | + checkDir("调用默认方法创建的目录"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + }); |
| 95 | + */ |
| 96 | + cfg.setFileOutConfigList(focList); |
| 97 | + mpg.setCfg(cfg); |
| 98 | + |
| 99 | + // 配置模板 |
| 100 | + TemplateConfig templateConfig = new TemplateConfig(); |
| 101 | + |
| 102 | + // 配置自定义输出模板 |
| 103 | + //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 |
| 104 | + // templateConfig.setEntity("templates/entity2.java"); |
| 105 | + // templateConfig.setService(); |
| 106 | + // templateConfig.setController(); |
| 107 | + |
| 108 | + templateConfig.setXml(null); |
| 109 | + mpg.setTemplate(templateConfig); |
| 110 | + |
| 111 | + // 策略配置 |
| 112 | + StrategyConfig strategy = new StrategyConfig(); |
| 113 | + strategy.setNaming(NamingStrategy.underline_to_camel); |
| 114 | + strategy.setColumnNaming(NamingStrategy.underline_to_camel); |
| 115 | +// strategy.setSuperEntityClass("com.dashuai.learning.mybatisplus.common.BaseEntity"); |
| 116 | + strategy.setEntityLombokModel(true); |
| 117 | + strategy.setRestControllerStyle(true); |
| 118 | + // 公共父类 |
| 119 | +// strategy.setSuperControllerClass("com.dashuai.learning.mybatisplus.common.BaseController"); |
| 120 | + // 写于父类中的公共字段 |
| 121 | + strategy.setSuperEntityColumns("id"); |
| 122 | + strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); |
| 123 | + strategy.setControllerMappingHyphenStyle(true); |
| 124 | + strategy.setTablePrefix(pc.getModuleName() + "_"); |
| 125 | + mpg.setStrategy(strategy); |
| 126 | +// mpg.setTemplateEngine(new FreemarkerTemplateEngine()); |
| 127 | + mpg.execute(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments