1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| package top.diruipu.demo.server.generator;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class Generator {
private static String tableNames="t_log_copy"; private static String dataSourceUrl="jdbc:mysql://192.168.0.108:3306/demo?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai"; private static String dataSourceDriver="com.mysql.cj.jdbc.Driver"; private static String dataSourceUser="root"; private static String dataSourcePwd="root";
public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); String projectPath = System.getProperty("user.dir"); setGlobalConfig(mpg,projectPath); setDataSourceConfig(mpg); PackageConfig pc = new PackageConfig(); pc.setParent("cc.mrbird. .server.system"); mpg.setPackageInfo(pc); setCfg(mpg,projectPath,pc); setTemplate(mpg); setStrategy(mpg,false); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }
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 + "!"); }
private static void setGlobalConfig(AutoGenerator mpg,String projectPath){ GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(projectPath + "/ -server/ -server-system/src/main/java") .setActiveRecord(false) .setAuthor("zrp") .setOpen(false) .setBaseColumnList(true) .setBaseResultMap(true) .setEnableCache(false) .setIdType(IdType.UUID) .setDateType(DateType.ONLY_DATE) .setSwagger2(true);
mpg.setGlobalConfig(gc); }
private static void setDataSourceConfig(AutoGenerator mpg){ DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(dataSourceUrl); dsc.setDriverName(dataSourceDriver); dsc.setUsername(dataSourceUser); dsc.setPassword(dataSourcePwd); mpg.setDataSource(dsc); }
private static void setCfg(AutoGenerator mpg,String projectPath,PackageConfig 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 + "/src/main/resources/mapper/" + (pc.getModuleName()==null?"":pc.getModuleName()+"/") + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } });
cfg.setFileOutConfigList(focList); mpg.setCfg(cfg);
} private static void setTemplate(AutoGenerator mpg){ TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setService("/generator/templates/service.java"); templateConfig.setServiceImpl("/generator/templates/serviceImpl.java"); templateConfig.setController("/generator/templates/controller.java"); templateConfig.setXml(null); mpg.setTemplate(templateConfig); } private static void setStrategy(AutoGenerator mpg,Boolean isScannerTableName){ StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper"); strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService"); strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true);
if (Boolean.TRUE.equals(isScannerTableName)){ strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); }else{ strategy.setInclude(tableNames.split(",")); }
strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_"); mpg.setStrategy(strategy); }
}
|