001package cn.mybatis.mp.generator.config;
002
003import cn.mybatis.mp.generator.template.*;
004import cn.mybatis.mp.generator.template.engine.TemplateEngine;
005import db.sql.api.DbType;
006import lombok.Getter;
007
008import javax.sql.DataSource;
009import java.util.ArrayList;
010import java.util.List;
011import java.util.function.Consumer;
012
013@Getter
014public class GeneratorConfig {
015
016    private final DataBaseConfig dataBaseConfig;
017    /**
018     * 表配置
019     */
020    private final TableConfig tableConfig = new TableConfig();
021    /**
022     * mapper 配置
023     */
024    private final MapperConfig mapperConfig = new MapperConfig();
025    /**
026     * mapper xml 配置
027     */
028    private final MapperXmlConfig mapperXmlConfig = new MapperXmlConfig();
029    /**
030     * 忽略表
031     */
032    private final boolean ignoreTable = false;
033    private final List<Class<? extends ITemplateBuilder>> templateBuilders = new ArrayList<>();
034    /**
035     * 列配置
036     */
037    private final ColumnConfig columnConfig = new ColumnConfig();
038    /**
039     * 实体类配置
040     */
041    private final EntityConfig entityConfig = new EntityConfig();
042    /**
043     * Dao 配置
044     */
045    private final DaoConfig daoConfig = new DaoConfig();
046    /**
047     * Dao 实现类配置
048     */
049    private final DaoImplConfig daoImplConfig = new DaoImplConfig();
050    /**
051     * Service 配置
052     */
053    private final ServiceConfig serviceConfig = new ServiceConfig();
054    /**
055     * Service 实现类配置
056     */
057    private final ServiceImplConfig serviceImplConfig = new ServiceImplConfig();
058    /**
059     * Action 实现类配置
060     */
061    private final ActionConfig actionConfig = new ActionConfig();
062    /**
063     * 是否忽略试图
064     */
065    private boolean ignoreView = false;
066    /**
067     * 完成后是否打开目录
068     */
069    private boolean finishOpen = false;
070    /**
071     * 根文件路径 默认取 System.getProperty("user.dir") +"/generate"
072     */
073    private String baseFilePath = System.getProperty("user.dir") + "/generate";
074    /**
075     * 根包路径
076     */
077    private String basePackage = "";
078    /**
079     * 模板根目录
080     */
081    private String templateRootPath = "templates";
082    /**
083     * 作者
084     */
085    private String author;
086    /**
087     * 模板引擎
088     */
089    private TemplateEngine templateEngine;
090
091    {
092        templateBuilders.add(EntityTemplateBuilder.class);
093        templateBuilders.add(MapperTemplateBuilder.class);
094        templateBuilders.add(MapperXmlTemplateBuilder.class);
095        templateBuilders.add(DaoTemplateBuilder.class);
096        templateBuilders.add(DaoImplTemplateBuilder.class);
097        templateBuilders.add(ServiceTemplateBuilder.class);
098        templateBuilders.add(ServiceImplTemplateBuilder.class);
099        templateBuilders.add(ActionTemplateBuilder.class);
100    }
101
102    public GeneratorConfig(String jdbcUrl, String username, String password) {
103        this.dataBaseConfig = new DataBaseConfig(jdbcUrl, username, password);
104    }
105
106    public GeneratorConfig(DbType dbType, DataSource dataSource) {
107        this.dataBaseConfig = new DataBaseConfig(dbType, dataSource);
108    }
109
110    /**
111     * 数据库配置
112     *
113     * @param consumer
114     * @return
115     */
116    public GeneratorConfig dataBaseConfig(Consumer<DataBaseConfig> consumer) {
117        consumer.accept(this.dataBaseConfig);
118        return this;
119    }
120
121    /**
122     * 设置模板引擎
123     *
124     * @param templateEngine
125     * @return
126     */
127    public GeneratorConfig templateEngine(TemplateEngine templateEngine) {
128        this.templateEngine = templateEngine;
129        return this;
130    }
131
132    /**
133     * 设置模板的根目录 默认:templates
134     *
135     * @param templateRootPath
136     * @return
137     */
138    public GeneratorConfig templateRootPath(String templateRootPath) {
139        this.templateRootPath = templateRootPath;
140        return this;
141    }
142
143    /**
144     * 设置 模板生成器
145     *
146     * @param consumer
147     * @return
148     */
149    public GeneratorConfig templateBuilders(Consumer<List<Class<? extends ITemplateBuilder>>> consumer) {
150        consumer.accept(this.templateBuilders);
151        return this;
152    }
153
154    /**
155     * 设置文件生成目标目录 默认: System.getProperty("user.dir") +"/generate"
156     *
157     * @param baseFilePath
158     * @return
159     */
160    public GeneratorConfig baseFilePath(String baseFilePath) {
161        this.baseFilePath = baseFilePath;
162        return this;
163    }
164
165    /**
166     * 设置 基础包路径
167     *
168     * @param basePackage
169     * @return
170     */
171    public GeneratorConfig basePackage(String basePackage) {
172        this.basePackage = basePackage;
173        return this;
174    }
175
176    /**
177     * 设置作者
178     *
179     * @param author
180     * @return
181     */
182    public GeneratorConfig author(String author) {
183        this.author = author;
184        return this;
185    }
186
187    /**
188     * 设置是否忽略试图
189     *
190     * @param ignoreView
191     * @return
192     */
193    public GeneratorConfig ignoreView(boolean ignoreView) {
194        this.ignoreView = ignoreView;
195        return this;
196    }
197
198
199    /**
200     * 设置表的配置
201     *
202     * @param consumer
203     * @return
204     */
205    public GeneratorConfig tableConfig(Consumer<TableConfig> consumer) {
206        consumer.accept(this.tableConfig);
207        return this;
208    }
209
210    /**
211     * 设置列的配置
212     *
213     * @param consumer
214     * @return
215     */
216    public GeneratorConfig columnConfig(Consumer<ColumnConfig> consumer) {
217        consumer.accept(this.columnConfig);
218        return this;
219    }
220
221    /**
222     * 设置实体类的配置
223     *
224     * @param consumer
225     * @return
226     */
227    public GeneratorConfig entityConfig(Consumer<EntityConfig> consumer) {
228        consumer.accept(this.entityConfig);
229        return this;
230    }
231
232
233    /**
234     * 设置mapper配置
235     *
236     * @param consumer
237     * @return
238     */
239    public GeneratorConfig mapperConfig(Consumer<MapperConfig> consumer) {
240        consumer.accept(this.mapperConfig);
241        return this;
242    }
243
244    /**
245     * 设置mapper配置
246     *
247     * @param consumer
248     * @return
249     */
250    public GeneratorConfig mapperXmlConfig(Consumer<MapperXmlConfig> consumer) {
251        consumer.accept(this.mapperXmlConfig);
252        return this;
253    }
254
255    /**
256     * 设置dao配置
257     *
258     * @param consumer
259     * @return
260     */
261    public GeneratorConfig daoConfig(Consumer<DaoConfig> consumer) {
262        consumer.accept(this.daoConfig);
263        return this;
264    }
265
266    /**
267     * 设置dao 实现类的配置
268     *
269     * @param consumer
270     * @return
271     */
272    public GeneratorConfig daoImplConfig(Consumer<DaoImplConfig> consumer) {
273        consumer.accept(this.daoImplConfig);
274        return this;
275    }
276
277    /**
278     * 设置service配置
279     *
280     * @param consumer
281     * @return
282     */
283    public GeneratorConfig serviceConfig(Consumer<ServiceConfig> consumer) {
284        consumer.accept(this.serviceConfig);
285        return this;
286    }
287
288    /**
289     * 设置 service 实现类配置
290     *
291     * @param consumer
292     * @return
293     */
294    public GeneratorConfig serviceImplConfig(Consumer<ServiceImplConfig> consumer) {
295        consumer.accept(this.serviceImplConfig);
296        return this;
297    }
298
299    /**
300     * 设置 控制器的配置
301     *
302     * @param consumer
303     * @return
304     */
305    public GeneratorConfig actionConfig(Consumer<ActionConfig> consumer) {
306        consumer.accept(this.actionConfig);
307        return this;
308    }
309
310    /**
311     * 完成后打开
312     */
313    public GeneratorConfig finishOpen(boolean finishOpen) {
314        this.finishOpen = finishOpen;
315        return this;
316    }
317}