今天使用了mybatis-plus旧版代码自动生成器。

  1 package com.wms.common;
  2 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3 import com.baomidou.mybatisplus.core.toolkit.StringPool;
  4 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  5 import com.baomidou.mybatisplus.generator.AutoGenerator;
  6 import com.baomidou.mybatisplus.generator.InjectionConfig;
  7 import com.baomidou.mybatisplus.generator.config.*;
  8 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  9 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 10 import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 import java.util.Scanner;
 14 public class CodeGenerator {
 15     /**
 16      * <p>
 17      * 读取控制台内容
 18      * </p>
 19      */
 20     public static String scanner(String tip) {
 21         Scanner scanner = new Scanner(System.in);
 22         StringBuilder help = new StringBuilder();
 23         help.append("请输⼊" + tip + ":");
 24         System.out.println(help.toString());
 25         if (scanner.hasNext()) {
 26             String ipt = scanner.next();
 27             if (StringUtils.isNotBlank(ipt)) {
 28                 return ipt;
 29             }
 30         }
 31         throw new MybatisPlusException("请输⼊正确的" + tip + "!");
 32     }
 33     /**
 34      * 操作步骤:
 35      * 1.修改数据源包括地址密码信息,对应代码标记:⼀、 下同
 36      * 2.模块配置,可以修改包名
 37      * 3.修改模板(这步可忽略)
 38      * @param args
 39      */
 40     public static void main(String[] args) {
 41         // 代码⽣成器
 42         AutoGenerator mpg = new AutoGenerator();
 43         // 全局配置
 44         GlobalConfig gc = new GlobalConfig();
 45         String projectPath = System.getProperty("user.dir");
 46         gc.setOutputDir(projectPath + "/src/main/java");
 47         gc.setAuthor("yzq");
 48         gc.setOpen(false);
 49         gc.setSwagger2(true); //实体属性 Swagger2 注解
 50         gc.setBaseResultMap(true);// XML ResultMap
 51         gc.setBaseColumnList(true);// XML columList
 52         //去掉service接⼝⾸字⺟的I, 如DO为User则叫UserService
 53         gc.setServiceName("%sService");
 54         mpg.setGlobalConfig(gc);
 55         // 数据源配置
 56         DataSourceConfig dsc = new DataSourceConfig();
 57         // ⼀、修改数据源
 58         dsc.setUrl("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF8&useSSL=false");
 59                 // dsc.setSchemaName("public");
 60                 dsc.setDriverName("com.mysql.cj.jdbc.Driver");
 61         dsc.setUsername("root");
 62         dsc.setPassword("123456");
 63         mpg.setDataSource(dsc);
 64         // 包配置
 65         PackageConfig pc = new PackageConfig();
 66         //pc.setModuleName(scanner("模块名"));
 67         // ⼆、模块配置
 68         pc.setParent("com.wms")
 69                 .setEntity("entity")
 70                 .setMapper("mapper")
 71                 .setService("service")
 72                 .setServiceImpl("service.impl")
 73                 .setController("controller");
 74         mpg.setPackageInfo(pc);
 75         // ⾃定义配置
 76         InjectionConfig cfg = new InjectionConfig() {
 77             @Override
 78             public void initMap() {
 79                 // to do nothing
 80             }
 81         };
 82         // 如果模板引擎是 freemarker
 83         String templatePath = "templates/mapper.xml.ftl";
 84         // 如果模板引擎是 velocity
 85         // String templatePath = "/templates/mapper.xml.vm";
 86         // ⾃定义输出配置
 87         List<FileOutConfig> focList = new ArrayList<>();
 88         // ⾃定义配置会被优先输出
 89         focList.add(new FileOutConfig(templatePath) {
 90             @Override
 91             public String outputFile(TableInfo tableInfo) {
 92                 // ⾃定义输出⽂件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发⽣变化!!
 93                 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
 94                         + "/" + tableInfo.getEntityName() + "Mapper" +
 95                         StringPool.DOT_XML;
 96             }
 97         });
 98  /*
 99  cfg.setFileCreate(new IFileCreate() {
100  @Override
101  public boolean isCreate(ConfigBuilder configBuilder, FileType fileType,
102 String filePath) {
103  // 判断⾃定义⽂件夹是否需要创建
104  checkDir("调⽤默认⽅法创建的⽬录,⾃定义⽬录⽤");
105  if (fileType == FileType.MAPPER) {
106  // 已经⽣成 mapper ⽂件判断存在,不想重新⽣成返回 false
107  return !new File(filePath).exists();
108  }
109  // 允许⽣成模板⽂件
110  return true;
111  }
112  });
113  */
114         cfg.setFileOutConfigList(focList);
115         mpg.setCfg(cfg);
116         // 配置模板
117         TemplateConfig templateConfig = new TemplateConfig();
118         // 配置⾃定义输出模板
119         //指定⾃定义模板路径,注意不要带上.ftl/.vm, 会根据使⽤的模板引擎⾃动识别
120         // 三、修改模板
121  /*templateConfig.setEntity("templates/entity2.java");
122  templateConfig.setService("templates/service2.java");
123  templateConfig.setController("templates/controller2.java");
124  templateConfig.setMapper("templates/mapper2.java");
125  templateConfig.setServiceImpl("templates/serviceimpl2.java");*/
126         templateConfig.setXml(null);
127         mpg.setTemplate(templateConfig);
128         // 策略配置
129         StrategyConfig strategy = new StrategyConfig();
130         strategy.setNaming(NamingStrategy.underline_to_camel);
131         strategy.setColumnNaming(NamingStrategy.underline_to_camel);
132         // strategy.setSuperEntityClass("你⾃⼰的⽗类实体,没有就不⽤设置!");
133         //strategy.setSuperEntityClass("BaseEntity");
134         strategy.setEntityLombokModel(true);
135         strategy.setRestControllerStyle(true);
136         // 公共⽗类
137         //strategy.setSuperControllerClass("BaseController");
138         // strategy.setSuperControllerClass("你⾃⼰的⽗类控制器,没有就不⽤设置!");
139         // 写于⽗类中的公共字段
140         // strategy.setSuperEntityColumns("id");
141         strategy.setInclude(scanner("表名,多个英⽂逗号分割").split(","));
142         strategy.setControllerMappingHyphenStyle(true);
143         //strategy.setTablePrefix(pc.getModuleName() + "_");
144         // 忽略表前缀tb_,⽐如说tb_user,直接映射成user对象
145         // 四、注意是否要去掉表前缀
146         //strategy.setTablePrefix("tb_");
147         mpg.setStrategy(strategy);
148         mpg.setTemplateEngine(new FreemarkerTemplateEngine());
149         mpg.execute();
150     }
151 }
View Code

主要就是路径修改与数据库连接,然后依据表名来创建,速度很快,非常适合我这种手残党。

 

[报错解决](Error Creating bean with name ‘xxx‘)类问题解决思路

我的解决办法是添加

@MapperScan("com.wms.mapper")
然后就能成功启动。

 

posted on 2023-10-11 22:07  夜的第七章i  阅读(1)  评论(0编辑  收藏  举报