|
@@ -0,0 +1,159 @@
|
|
|
+package com.qx.didi;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.*;
|
|
|
+import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
|
|
|
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+
|
|
|
+ * @author liuzezhong
|
|
|
+ * @title
|
|
|
+ * @date 2021/7/20
|
|
|
+ */
|
|
|
+public class CodeGenerator {
|
|
|
+
|
|
|
+
|
|
|
+ * <p>
|
|
|
+ * 读取控制台内容
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
+ public static String scanner(String tip) {
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ StringBuilder help = new StringBuilder();
|
|
|
+ help.append("请输入" + tip + ":");
|
|
|
+ System.out.println(help.toString());
|
|
|
+ if (scanner.hasNext()) {
|
|
|
+ String ipt = scanner.next();
|
|
|
+ if (StringUtils.isNotBlank(ipt)) {
|
|
|
+ return ipt;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new MybatisPlusException("请输入正确的" + tip + "!");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ AutoGenerator mpg = new AutoGenerator();
|
|
|
+
|
|
|
+
|
|
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
+ String projectPath = System.getProperty("user.dir");
|
|
|
+ gc.setOutputDir(projectPath + "/pdms_oss/pdms-oss-api/src/main/java");
|
|
|
+ gc.setAuthor("jek");
|
|
|
+ gc.setOpen(false);
|
|
|
+ gc.setSwagger2(true);
|
|
|
+ mpg.setGlobalConfig(gc);
|
|
|
+
|
|
|
+
|
|
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
+ dsc.setUrl("jdbc:mysql://47.111.243.220:9306/pdms_oss?useUnicode=true&useSSL=false&characterEncoding=utf8");
|
|
|
+
|
|
|
+ dsc.setDriverName("com.mysql.cj.jdbc.Driver");
|
|
|
+ dsc.setUsername("root");
|
|
|
+ dsc.setPassword("Qx123456");
|
|
|
+
|
|
|
+ dsc.setTypeConvert(new ITypeConvert() {
|
|
|
+ @Override
|
|
|
+ public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
|
|
|
+ String t = fieldType.toLowerCase();
|
|
|
+ if(t.contains("datetime")){
|
|
|
+ return DbColumnType.DATE;
|
|
|
+ }
|
|
|
+ if (t.contains("tinyint")) {
|
|
|
+ return DbColumnType.INTEGER;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new MySqlTypeConvert().processTypeConvert(globalConfig,fieldType);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mpg.setDataSource(dsc);
|
|
|
+
|
|
|
+
|
|
|
+ PackageConfig pc = new PackageConfig();
|
|
|
+ pc.setModuleName(scanner("模块名"));
|
|
|
+ pc.setParent("cn.fastfun");
|
|
|
+ mpg.setPackageInfo(pc);
|
|
|
+
|
|
|
+
|
|
|
+ InjectionConfig cfg = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ String templatePath = "/templates/mapper.xml.ftl";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ List<FileOutConfig> focList = new ArrayList<>();
|
|
|
+
|
|
|
+ focList.add(new FileOutConfig(templatePath) {
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+
|
|
|
+ return projectPath + "/pdms_oss/pdms-oss-api/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ cfg.setFileCreate(new IFileCreate() {
|
|
|
+ @Override
|
|
|
+ public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
|
|
|
+
|
|
|
+ checkDir("调用默认方法创建的目录,自定义目录用");
|
|
|
+ if (fileType == FileType.MAPPER) {
|
|
|
+
|
|
|
+ return !new File(filePath).exists();
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ */
|
|
|
+ cfg.setFileOutConfigList(focList);
|
|
|
+ mpg.setCfg(cfg);
|
|
|
+
|
|
|
+
|
|
|
+ TemplateConfig templateConfig = new TemplateConfig();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ templateConfig.setXml(null);
|
|
|
+ mpg.setTemplate(templateConfig);
|
|
|
+
|
|
|
+
|
|
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
+ strategy.setNaming(NamingStrategy.underline_to_camel);
|
|
|
+ strategy.setColumnNaming(NamingStrategy.underline_to_camel);
|
|
|
+
|
|
|
+ strategy.setEntityLombokModel(true);
|
|
|
+ strategy.setRestControllerStyle(true);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
|
|
|
+ strategy.setControllerMappingHyphenStyle(true);
|
|
|
+ strategy.setTablePrefix(pc.getModuleName() + "_");
|
|
|
+ mpg.setStrategy(strategy);
|
|
|
+ mpg.setTemplateEngine(new FreemarkerTemplateEngine());
|
|
|
+ mpg.execute();
|
|
|
+ }
|
|
|
+}
|