懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.gen.core.generator.guns.mybatisplus;
2
3 import cn.stylefeng.guns.gen.core.enums.GenDownloadEnum;
4 import cn.stylefeng.guns.gen.core.generator.base.AbstractMpGenerator;
5 import cn.stylefeng.guns.gen.core.generator.restful.mybatisplus.param.MpParam;
6 import com.baomidou.mybatisplus.annotation.DbType;
7 import com.baomidou.mybatisplus.annotation.FieldFill;
8 import com.baomidou.mybatisplus.annotation.IdType;
9 import com.baomidou.mybatisplus.generator.config.FileOutConfig;
10 import com.baomidou.mybatisplus.generator.config.po.TableFill;
11 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
12 import com.baomidou.mybatisplus.generator.config.rules.DateType;
13 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19
20 /**
21  * 默认的mybatis-plus生成器
22  *
23  * @author fengshuonan
24  * @date 2018-12-13-2:20 PM
25  */
26 public class GunsMpGenerator extends AbstractMpGenerator {
27
28     private MpParam mpContextParam;
29
30     public GunsMpGenerator(MpParam mpContextParam) {
31         this.mpContextParam = mpContextParam;
32     }
33
34     /**
35      * 代码生成之前,配置代码生成器所需要的配置
36      *
37      * @author fengshuonan
38      * @Date 2018/12/13 2:48 PM
39      */
40     @Override
41     protected void beforeGeneration() {
42
43         // 全局配置
44         globalConfig.setFileOverride(true);
45         globalConfig.setActiveRecord(false);
46         globalConfig.setBaseResultMap(true);
47         globalConfig.setBaseColumnList(true);
48         globalConfig.setEnableCache(false);
49         globalConfig.setOpen(false);
50         globalConfig.setAuthor(contextParam.getAuthor());
51         globalConfig.setIdType(IdType.ID_WORKER);
52
53         //打包下载,则生成到默认目录,直接下载到项目目录则路径上加个/java
54         if (contextParam.getGenDownloadEnum().equals(GenDownloadEnum.DEFAULT_PATH)) {
55             globalConfig.setOutputDir(contextParam.getOutputPath());
56         } else {
57             globalConfig.setOutputDir(contextParam.getOutputPath() + File.separator + "java");
58         }
59
60         //时间格式还用旧的Date
61         globalConfig.setDateType(DateType.ONLY_DATE);
62
63         // 自定义文件命名,注意 %s 会自动填充表实体属性!
64         if (mpContextParam.getGeneratorInterface()) {
65             globalConfig.setServiceName("%sService");
66             globalConfig.setServiceImplName("%sServiceImpl");
67         } else {
68             globalConfig.setServiceName("%sService");
69             globalConfig.setServiceImplName("%sService");
70         }
71
72         // 数据源配置
73         if (contextParam.getJdbcUrl().contains("oracle")) {
74             dataSourceConfig.setDbType(DbType.ORACLE);
75         } else if (contextParam.getJdbcUrl().contains("postgresql")) {
76             dataSourceConfig.setDbType(DbType.POSTGRE_SQL);
77         } else if (contextParam.getJdbcUrl().contains("sqlserver")) {
78             dataSourceConfig.setDbType(DbType.SQL_SERVER);
79         } else {
80             dataSourceConfig.setDbType(DbType.MYSQL);
81         }
82         dataSourceConfig.setDriverName(contextParam.getJdbcDriver());
83         dataSourceConfig.setUrl(contextParam.getJdbcUrl());
84         dataSourceConfig.setUsername(contextParam.getJdbcUserName());
85         dataSourceConfig.setPassword(contextParam.getJdbcPassword());
86
87         // 策略配置
88         // 大写命名
89         strategyConfig.setCapitalMode(false);
90
91         // 此处可以移除表前缀表前缀
92         strategyConfig.setTablePrefix(this.mpContextParam.getRemoveTablePrefix());
93
94         // 表名生成策略
95         strategyConfig.setNaming(NamingStrategy.underline_to_camel);
96         strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
97         strategyConfig.setEntityTableFieldAnnotationEnable(true);
98
99         // 需要生成的表
100         strategyConfig.setInclude(this.mpContextParam.getIncludeTables());
101
102         // 公共字段填充
103         ArrayList<TableFill> tableFills = new ArrayList<>();
104         tableFills.add(new TableFill("CREATE_TIME", FieldFill.INSERT));
105         tableFills.add(new TableFill("UPDATE_TIME", FieldFill.UPDATE));
106         tableFills.add(new TableFill("CREATE_USER", FieldFill.INSERT));
107         tableFills.add(new TableFill("UPDATE_USER", FieldFill.UPDATE));
108         strategyConfig.setTableFillList(tableFills);
109
110         // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
111         // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
112         templateConfig.setController(null);
113         templateConfig.setXml("/mpTemplates/mapper.xml.vm");
114         templateConfig.setMapper("/mpTemplates/mapper.java.vm");
115
116         //如果不生成接口,就走不生成接口的模板
117         if (!this.mpContextParam.getGeneratorInterface()) {
118             templateConfig.setService(null);
119             templateConfig.setServiceImpl("/mpTemplates/NoneInterfaceServiceImpl.java");
120         } else {
121             templateConfig.setService("/mpTemplates/service.java.vm");
122             templateConfig.setServiceImpl("/mpTemplates/serviceImpl.java.vm");
123         }
124
125         // 包配置
126         packageConfig.setParent(this.contextParam.getProPackage());
127         packageConfig.setModuleName("");
128         packageConfig.setXml("mapper.mapping");
129
130         if (this.mpContextParam.getGeneratorInterface()) {
131             packageConfig.setServiceImpl("service.impl");
132             packageConfig.setService("service");
133         } else {
134             packageConfig.setServiceImpl("service");
135             packageConfig.setService("service");
136         }
137
138         //自定义specification model的生成
139         String SpecParamsTemplatePath = "/mpTemplates/specParam.java.vm";
140         String SpecResultTemplatePath = "/mpTemplates/specResult.java.vm";
141         List<FileOutConfig> focList = new ArrayList<>();
142
143         String paramsParentPackage = this.contextParam.getProPackage().replaceAll("\\.", "/") + "/model/params";
144         String resultParentPackage = this.contextParam.getProPackage().replaceAll("\\.", "/") + "/model/result";
145         File paramsDir = new File(globalConfig.getOutputDir() + "/" + paramsParentPackage);
146         File resultDir = new File(globalConfig.getOutputDir() + "/" + resultParentPackage);
147         if (!paramsDir.exists()) {
148             paramsDir.mkdirs();
149         }
150         if (!resultDir.exists()) {
151             resultDir.mkdirs();
152         }
153
154         //model的查询条件
155         focList.add(new FileOutConfig(SpecParamsTemplatePath) {
156             @Override
157             public String outputFile(TableInfo tableInfo) {
158                 return globalConfig.getOutputDir() + "/" + paramsParentPackage + "/" + tableInfo.getEntityName() + "Param.java";
159             }
160         });
161         focList.add(new FileOutConfig(SpecResultTemplatePath) {
162             @Override
163             public String outputFile(TableInfo tableInfo) {
164                 return globalConfig.getOutputDir() + "/" + resultParentPackage + "/" + tableInfo.getEntityName() + "Result.java";
165             }
166         });
167         injectionConfig.setFileOutConfigList(focList);
168
169         //自定义specification里的参数
170         HashMap<String, Object> contexMap = new HashMap<>();
171         contexMap.put("EntitySpecParams", this.contextParam.getProPackage() + ".model.params");
172         contexMap.put("EntitySpecResult", this.contextParam.getProPackage() + ".model.result");
173         injectionConfig.setMap(contexMap);
174     }
175
176 }