日期 |
版本 |
作者 |
说明 |
2022-2-27 |
V-1.0 |
孔留锋 |
文档创建。 |
概要
简介
基于RuoYi-Vue 3.8.1,框架自带的代码生成器,对有些业务来说无用的信息太多,而且生成的实体类中的字段需要和数据库字段强相关(驼峰命名对应),否则就要对生成的实体类进行改造,属性中添加@TableField主注解进行修改。需要对Mybatis-plus 原始生成器进行改造,适配符合架构所需。
改造
gene模块新建
- IDEA 右键新建 maven 模块 取名 gene
修改gene 模块 POM文件
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
| <parent> <artifactId>single</artifactId> <groupId>com.zzsn</groupId> <version>3.8.1</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>gene</artifactId>
<description> Mybatis-plus 原生 代码生成器 </description>
<dependencies> <dependency> <groupId>com.zzsn</groupId> <artifactId>common</artifactId> </dependency>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency>
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
|
代码生成器
test 模块下新建
DataSourceInfo.calss
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Data @NoArgsConstructor @AllArgsConstructor public class DataSourceInfo { private String driverClassName; private String url; private String username; private String password; }
|
Generator.class
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
| @SpringBootTest public class Generator { private DataSourceInfo master = new DataSourceInfo("com.mysql.jdbc.Driver" ,"jdbc:mysql://192.168.10.1:3306/core_data_base?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai" ,"root","@123456."); private DataSourceInfo slave = new DataSourceInfo("com.mysql.jdbc.Driver" ,"jdbc:mysql://192.168.10.1:3306/sentiment_data_base?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai" ,"root","@123456."); @Test public void codeGenerator() { AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = this.getGlobalConfig(true); mpg.setGlobalConfig(gc);
DataSourceConfig dsc = this.getDataSourceConfig(applets); mpg.setDataSource(dsc);
PackageConfig pc = this.getPackageConfig("com.zzsn","sentiment"); mpg.setPackageInfo(pc);
InjectionConfig cfg = this.getInjectionConfig(pc); mpg.setCfg(cfg);
TemplateConfig templateConfig = this.getTemplateConfig(false); mpg.setTemplate(templateConfig);
StrategyConfig strategy = this.getStrategyConfig(pc ,new String[]{"sys_"} ,new String[]{"sys_test" }); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } public GlobalConfig getGlobalConfig(Boolean fileOverride) { GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setFileOverride(fileOverride);
gc.setAuthor("kongliufeng"); gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setIdType(IdType.ASSIGN_ID); return gc; } public DataSourceConfig getDataSourceConfig(DataSourceInfo info) { DataSourceConfig dsc = new DataSourceConfig();
if(info.getDriverClassName().startsWith("com.mysql")){ dsc.setDbType(DbType.MYSQL); }else if(info.getDriverClassName().startsWith("oracle.jdbc")){ dsc.setDbType(DbType.ORACLE); }
dsc.setUrl(info.getUrl()); dsc.setDriverName(info.getDriverClassName()); dsc.setUsername(info.getUsername()); dsc.setPassword(info.getPassword()); return dsc; } public PackageConfig getPackageConfig(String parent ,String moduleName){ PackageConfig pc = new PackageConfig(); pc.setParent(parent);
pc.setModuleName(moduleName);
pc.setEntity("domain");
return pc; } public InjectionConfig getInjectionConfig(PackageConfig pc){
InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() {
} };
String projectPath = System.getProperty("user.dir");
String templatePath = "/templates/mapper.xml.ftl";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) { public String outputFile(TableInfo tableInfo) { return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } });
cfg.setFileOutConfigList(focList);
return cfg; } public TemplateConfig getTemplateConfig(Boolean noGeneratorController){ TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
if(noGeneratorController){ templateConfig.setController(null); } return templateConfig; } public StrategyConfig getStrategyConfig(PackageConfig pc,String[] tablePrefixs,String[] includes){ StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setTablePrefix(tablePrefixs);
strategy.setInclude(includes);
strategy.setExclude();
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
strategy.setEntityTableFieldAnnotationEnable(true);
return strategy; } }
|
试用方式
- Generator.codeGenerator方法修改如下:
- this.getPackageConfig(“com.zzsn”,”sentiment”)–》设置包名,模块名称
- this.getTemplateConfig(false)–》指定是否生成controller类
- this.getStrategyConfig()–》指定去除的前缀,指定生成的表名称
- 运行Generator.codeGenerator方法
- 生成完毕后会在gene模块下ser.mian 中生成文件,移动到各个模块即可