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