Foxnic-Web 代码生成 (2) —— 代码生成的配置类
概述
在上一节,我们已经讲述了代码生成的基本步骤,但是对细节部分并未展开。利用 Foxnic-Generator 包进行代码生成的方式是多种多样的,我们这里提到的配置类这是其中一种,例如 在 Foxnic-EAM 项目里,有很多的代码生成非使用配置类来完成的。
我们优先选择配置类讲解,显然配置类有其优势。首先,配置类按数据表隔离,一数据表一模块一个配置类。其次,在配置类内部,按配置对象的不同,分别在不同的方法内进行配置代码的编写。例如,配置模型时在 configModel 方法内编写配置代码,配置字段时在 configFields 方法内编写配置代码。
那么,代码生成的配置为什么要用 Java 类,而不是用 Json 、XML 或 YML 呢?首先,不管是 Json 、XML 或 YML、Java,都是在编辑器敲文本。那么哪一种方式敲文本是最方便的呢,自然是 Java 了,因为有开发工具强大的支持。
本文中的示例代码均可在 https://gitee.com/LeeFJ/foxnic-samples 项目中找到,本文对照 webfull 项目讲解。
配置类
代码生成配置类都是 ModuleCodeConfig 类的子类,在 ModuleCodeConfig 类内定义了若干方法,这些方法可以让我们分门别类的对模块代码的方方面面进行配置。在生成代码时,这些方法会按照一定的顺序进行调用,最终生成我们想要的代码。我们先来看一个示例:
1package org.github.foxnic.web.generator.module.mall; 2 3import com.github.foxnic.generator.builder.business.option.ServiceOptions; 4import com.github.foxnic.generator.builder.model.PoClassFile; 5import com.github.foxnic.generator.builder.model.VoClassFile; 6import com.github.foxnic.generator.builder.view.option.*; 7import com.github.foxnic.generator.config.WriteMode; 8import com.leefj.webfull.constants.db.WebFullTables.WEBFULL_EXAMPLE_ORDER; 9import com.leefj.webfull.domain.example.Address; 10import com.leefj.webfull.domain.example.meta.OrderMeta; 11import com.leefj.webfull.proxy.example.AddressServiceProxy; 12import org.github.foxnic.web.generator.module.BaseCodeConfig; 13 14public class ExampleOrderConfig extends BaseCodeConfig<WEBFULL_EXAMPLE_ORDER> { 15 /** 16 * 配置模型,为 po 和 vo 添加 额外的属性等 17 */ 18 @Override 19 public void configModel(PoClassFile poType, VoClassFile voType) { 20 poType.addSimpleProperty(Address.class,"address","收件地址","关联的收件地址对象"); 21 } 22 23 /** 24 * 配置字段 25 */ 26 @Override 27 public void configFields(ViewOptions view) { 28 // ID 字段通常隐藏 29 view.field(WEBFULL_EXAMPLE_ORDER.ID).basic().hidden(); 30 // NAME 字段,单行文本框 31 view.field(WEBFULL_EXAMPLE_ORDER.ORDER_NO) 32 // 搜索栏:设置模糊搜索 33 .search().fuzzySearch() 34 // 表格列:指定对齐方式 35 .table().alignLeft() 36 // 表单:指定表单编辑器为文本输入框,并指定默认值 37 .form().textInput() 38 // 表单校验:必填 39 .form().validate().required() 40 ; 41 42 // NAME 字段,单行文本框 43 view.field(WEBFULL_EXAMPLE_ORDER.ADDRESS_ID) 44 .basic().label("收件地址") 45 .form().selectBox() 46 .queryApi(AddressServiceProxy.QUERY_LIST).valueField("id").textField("address") 47 .fillWith(OrderMeta.ADDRESS) 48 // 表单校验:必填 49 .form().validate().required() 50 ; 51 52 } 53 54 /** 55 * 配置搜索 56 */ 57 @Override 58 public void configSearch(ViewOptions view, SearchAreaOptions search) { 59 60 // 搜索布局 61 search.inputLayout(new Object[]{ 62 WEBFULL_EXAMPLE_ORDER.ORDER_NO 63 }); 64 65 } 66 67 /** 68 * 配置 List 表格 69 */ 70 @Override 71 public void configList(ViewOptions view, ListOptions list) { 72 list.operationColumn().addActionButton("明细","openItems"); 73 } 74 /** 75 * 配置表单 76 */ 77 @Override 78 public void configForm(ViewOptions view, FormOptions form, FormWindowOptions formWindow) { 79 formWindow.width("800px"); 80 form.labelWidth(85); 81 } 82 /** 83 * 配置源码覆盖 84 */ 85 @Override 86 public void configOverrides() { 87 //文件生成覆盖模式 88 context.overrides() 89 .setServiceIntfAnfImpl(WriteMode.COVER_EXISTS_FILE) //服务与接口 90 .setControllerAndAgent(WriteMode.COVER_EXISTS_FILE) //Rest 91 .setPageController(WriteMode.COVER_EXISTS_FILE) //页面控制器 92 .setFormPage(WriteMode.COVER_EXISTS_FILE) //表单HTML页 93 .setListPage(WriteMode.COVER_EXISTS_FILE) //列表HTML页 94 .setExtendJsFile(WriteMode.COVER_EXISTS_FILE); 95 } 96 /** 97 * 配置服务代码 98 */ 99 @Override 100 public void configService(ServiceOptions service) { 101 102 } 103 104 public ExampleOrderConfig() { 105 super("webfull-service-example", WEBFULL_EXAMPLE_ORDER.$TABLE, "webfull_example_"); 106 } 107}
这是生成订单管理代码的配置,我们可以看到这个配置类已经实现了 configModel、configFields、configSearch、configList、configForm、configOverrides、configService 这几个方法。下面我们逐个介绍一下这些方法作用。
| 方法 | 用途 |
|---|---|
| configModel | 配置模型,配置PO、VO或增加新的模型。 |
| configFields | 配置字段,配置字段在表格、表单、搜索框内的表现形式。 |
| configView | 配置视图 |
| configSearch | 配置搜索栏 |
| configList | 配置表格、列表 |
| configForm | 配置表单 |
| configController | 配置接口控制器 |
| configService | 配置服务 |
| configBPM | 可选,如果开启流程可以配置改方法,用于流程相关的配置。 |
| configOverrides | 配置代码文件的覆盖模式。 |
模块的配置类,最终被注册到 WebFullCodeStarter ,启动 WebFullCodeStarter ,按控制台提示输入序号生成对应的模块代码。
小结
本节主要介绍了在 Foxnic-SQL 和 oxnic-Web 代码生成时配置类的原理与作用,本文明确了配置类中每个方法的作用,按需实现即可。
配置类的每个方法如何实现,我们并未在本节展开,后面的章节中我们将逐一介绍。
相关项目
https://gitee.com/LeeFJ/foxnic
https://gitee.com/LeeFJ/foxnic-web
https://gitee.com/LeeFJ/foxnic-samples
