jeesite 代码生成原理1
Posted jlyzxye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jeesite 代码生成原理1相关的知识,希望对你有一定的参考价值。
jeeSite生成代码原理主要是:通过JAXBContext的Marshal(解组) 和 UnMarshal(编组)实现java与xml的相互转换,其中把要生成代码的文件写成一个XML模板,再通过IO流一行一行读取模板,再进行转换成相应的文件!
在进入这个页面会使用GenUtils.getConfig()方法加载config.xml 并在genTableForm中读取javaType,queryType,showType。
config.xml
<?xml version="1.0" encoding="utf-8"?> <config> <!-- 生成分类 --> <category> <category value="curd" label="增删改查(单表)"> <template>curd/controller.xml</template> <template>curd/service.xml</template> <template>category-ref:dao</template> <template>curd/viewForm.xml</template> <template>curd/viewList.xml</template> </category> <category value="curd_many" label="增删改查(一对多)"> <template>curd/controller.xml</template> <template>curd/serviceMany.xml</template> <template>category-ref:dao</template> <template>curd/viewFormMany.xml</template> <template>curd/viewList.xml</template> <childTable> <template>category-ref:dao</template> </childTable> </category> <category value="dao" label="仅持久层(dao/entity/mapper)"> <template>dao/dao.xml</template> <template>dao/entity.xml</template> <template>dao/mapper.xml</template> </category> <category value="treeTable" label="树结构表(一体)"> <template>treetable/controller.xml</template> <template>treetable/service.xml</template> <template>treetable/dao.xml</template> <template>treetable/entity.xml</template> <template>treetable/mapper.xml</template> <template>treetable/viewForm.xml</template> <template>treetable/viewList.xml</template> </category> <category value="treeTableAndList" label="树结构表(左树右表)"> <template>category-ref:dao</template> </category> </category> <!-- java类型 --> <javaType> <dict value="String" label="String"/> <dict value="Long" label="Long"/> <dict value="Integer" label="Integer"/> <dict value="Double" label="Double"/> <dict value="java.util.Date" label="Date"/> <dict value="com.thinkgem.jeesite.modules.sys.entity.User" label="User"/> <dict value="com.thinkgem.jeesite.modules.sys.entity.Office" label="Office"/> <dict value="com.thinkgem.jeesite.modules.sys.entity.Area" label="Area"/> <dict value="com.thinkgem.jeesite.modules.idorp.entity.project.IdorpProject" label="IdorpProject"/> <dict value="com.thinkgem.jeesite.modules.idorp.entity.money.IdorpProjectPayment" label="IdorpProjectPayment"/> <dict value="com.thinkgem.jeesite.modules.idorp.entity.money.IdorpContact" label="IdorpContact"/> <dict value="com.thinkgem.jeesite.modules.idorp.entity.labor.IdorpLaborCost" label="IdorpLaborCost"/> <dict value="This" label="ThisObj" description="生成当前对象"/> <dict value="Custom" label="Custom" description="自定义对象,生成后手动设置"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaProductType" label="JhOaProductType"/> <dict value="com.thinkgem.jeesite.one.modules.jhoa.entity.base.JhOaSupplier" label="JhOaSupplier"/> <dict value="com.thinkgem.jeesite.one.modules.jhoa.entity.base.JhOaStore" label="JhOaStore"/> <dict value="com.thinkgem.jeesite.one.modules.jhoa.entity.base.JhOaPost" label="JhOaPost"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaProduct" label="JhOaProduct"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaPurchaseReq" label="JhOaPurchaseReq"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaProductPrice" label="JhOaProductPrice"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaPurchaseReqDetail" label="JhOaPurchaseReqDetail"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaPurchase" label="JhOaPurchase"/> <dict value="com.thinkgem.jeesite.one.modules.jhoa.entity.purchase.JhOaPayment" label="JhOaPayment"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaPurchaseDetail" label="JhOaPurchaseDetail"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaInStorage" label="JhOaInStorage"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.purchase.JhOaBackBill" label="JhOaBackBill"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaStoreStock" label="JhOaStoreStock"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaLossApply" label="JhOaLossApply"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaStoreStaff" label="JhOaStoreStaff"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.store.JhOaSale" label="JhOaSale"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaStoreStockPrice" label="JhOaStoreStockPrice"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.store.JhOaSaleDetail" label="JhOaSaleDetail"/> <dict value="com.thinkgem.jeesite.two.modules.jhoa.entity.base.JhOaAllocate" label="JhOaAllocate"/> </javaType> <!-- 查询类型 --> <queryType> <dict value="=" label="="/> <dict value="!=" label="!="/> <dict value=">" label=">"/> <dict value=">=" label=">="/> <dict value="<" label="<"/> <dict value="<=" label="<="/> <dict value="between" label="Between"/> <dict value="like" label="Like"/> <dict value="left_like" label="Left Like"/> <dict value="right_like" label="Right Like"/> </queryType> <!-- 字段显示类型 --> <showType> <dict value="input" label="单行文本"/> <dict value="textarea" label="多行文本"/> <dict value="select" label="下拉选项"/> <dict value="radiobox" label="单选按钮"/> <dict value="checkbox" label="复选框"/> <dict value="dateselect" label="日期选择"/> <dict value="userselect" label="人员选择"/> <dict value="officeselect" label="部门选择"/> <dict value="areaselect" label="区域选择"/> <dict value="treeselect" label="树选择控件"/> <dict value="fileselect" label="文件上传选择"/> </showType> </config>
genTableForm.jsp
<td>
<select name="columnList[${vs.index}].javaType" class="required input-mini" style="width:185px;*width:175px">
<c:forEach items="${config.javaTypeList}" var="dict">
<option value="${dict.value}" ${dict.value==column.javaType?‘selected‘:‘‘} title="${dict.description}">${dict.label}</option>
</c:forEach>
</select>
</td>
各行数据保存在GenTableColumn中;
点击保存进入GenUtils.generateCode方法
1 private String generateCode(GenScheme genScheme){ 2 3 StringBuilder result = new StringBuilder(); 4 5 // 查询主表及字段列 6 GenTable genTable = genTableDao.get(genScheme.getGenTable().getId()); 7 genTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(genTable.getId())))); 8 9 // 获取所有代码模板 10 GenConfig config = GenUtils.getConfig(); 11 12 // 获取模板列表 13 List<GenTemplate> templateList = GenUtils.getTemplateList(config, genScheme.getCategory(), false); 14 List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getCategory(), true); 15 16 // 如果有子表模板,则需要获取子表列表 17 if (childTableTemplateList.size() > 0){ 18 GenTable parentTable = new GenTable(); 19 parentTable.setParentTable(genTable.getName()); 20 genTable.setChildList(genTableDao.findList(parentTable)); 21 } 22 23 // 生成子表模板代码 24 for (GenTable childTable : genTable.getChildList()){ 25 childTable.setParent(genTable); 26 childTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(childTable.getId())))); 27 genScheme.setGenTable(childTable); 28 Map<String, Object> childTableModel = GenUtils.getDataModel(genScheme); 29 for (GenTemplate tpl : childTableTemplateList){ 30 result.append(GenUtils.generateToFile(tpl, childTableModel, genScheme.getReplaceFile())); 31 } 32 } 33 34 // 生成主表模板代码 35 genScheme.setGenTable(genTable); 36 Map<String, Object> model = GenUtils.getDataModel(genScheme); 37 for (GenTemplate tpl : templateList){ 38 result.append(GenUtils.generateToFile(tpl, model, genScheme.getReplaceFile())); 39 } 40 return result.toString(); 41 }
参考链接:
https://blog.csdn.net/u011781521/article/details/79322942
https://www.cnblogs.com/26JRS/p/6084082.html
以上是关于jeesite 代码生成原理1的主要内容,如果未能解决你的问题,请参考以下文章