一、原理讲解


jeesite代码生成用的是FreeMarker模板引擎结合xml技术来实现的,定义的模板都放在resources/templates/modules/gen下




一看就知道crud就是基本的增删改查,dao是数据库操作,treetable是有关树方面的模板,其中主要的配置文件就是config.xml,该文件中定义了生成的模板,以及java类型,查询类型,字段显示类型等一些数据。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <config>
  3. <!-- 生成分类 -->
  4. <category>
  5. <category value="curd" label="增删改查(单表)">
  6. <template>curd/controller.xml</template>
  7. <template>curd/service.xml</template>
  8. <template>category-ref:dao</template>
  9. <template>curd/viewForm.xml</template>
  10. <template>curd/viewList.xml</template>
  11. </category>
  12. <category value="curd_many" label="增删改查(一对多)">
  13. <template>curd/controller.xml</template>
  14. <template>curd/serviceMany.xml</template>
  15. <template>category-ref:dao</template>
  16. <template>curd/viewFormMany.xml</template>
  17. <template>curd/viewList.xml</template>
  18. <childTable>
  19. <template>category-ref:dao</template>
  20. </childTable>
  21. </category>
  22. <category value="dao" label="仅持久层(dao/entity/mapper)">
  23. <template>dao/dao.xml</template>
  24. <template>dao/entity.xml</template>
  25. <template>dao/mapper.xml</template>
  26. </category>
  27. <category value="treeTable" label="树结构表(一体)">
  28. <template>treetable/controller.xml</template>
  29. <template>treetable/service.xml</template>
  30. <template>treetable/dao.xml</template>
  31. <template>treetable/entity.xml</template>
  32. <template>treetable/mapper.xml</template>
  33. <template>treetable/viewForm.xml</template>
  34. <template>treetable/viewList.xml</template>
  35. </category>
  36. <category value="treeTableAndList" label="树结构表(左树右表)">
  37. <template>category-ref:dao</template>
  38. </category>
  39. </category>
  40. <!-- java类型 -->
  41. <javaType>
  42. <dict value="String" label="String"/>
  43. <dict value="Long" label="Long"/>
  44. <dict value="Integer" label="Integer"/>
  45. <dict value="Double" label="Double"/>
  46. <dict value="java.util.Date" label="Date"/>
  47. <dict value="com.thinkgem.jeesite.modules.sys.entity.User" label="User"/>
  48. <dict value="com.thinkgem.jeesite.modules.sys.entity.Office" label="Office"/>
  49. <dict value="com.thinkgem.jeesite.modules.sys.entity.Area" label="Area"/>
  50. <dict value="This" label="ThisObj" description="生成当前对象"/>
  51. <dict value="Custom" label="Custom" description="自定义对象,生成后手动设置"/>
  52. </javaType>
  53. <!-- 查询类型 -->
  54. <queryType>
  55. <dict value="=" label="="/>
  56. <dict value="!=" label="!="/>
  57. <dict value=">" label=">"/>
  58. <dict value=">=" label=">="/>
  59. <dict value="<" label="<"/>
  60. <dict value="<=" label="<="/>
  61. <dict value="between" label="Between"/>
  62. <dict value="like" label="Like"/>
  63. <dict value="left_like" label="Left Like"/>
  64. <dict value="right_like" label="Right Like"/>
  65. </queryType>
  66. <!-- 字段显示类型 -->
  67. <showType>
  68. <dict value="input" label="单行文本"/>
  69. <dict value="textarea" label="多行文本"/>
  70. <dict value="select" label="下拉选项"/>
  71. <dict value="radiobox" label="单选按钮"/>
  72. <dict value="checkbox" label="复选框"/>
  73. <dict value="dateselect" label="日期选择"/>
  74. <dict value="userselect" label="人员选择"/>
  75. <dict value="officeselect" label="部门选择"/>
  76. <dict value="areaselect" label="区域选择"/>
  77. <dict value="treeselect" label="树选择控件"/>
  78. <dict value="fileselect" label="文件上传选择"/>
  79. </showType>
  80. </config>

其中

  1. <childTable>
  2. <template>category-ref:dao</template>
  3. </childTable>

定义了子表,初看jeesite的代码生成,有个困惑的地方就是,一般通过FreeMarker进行代码生成定义的模板都是ftl格式的,而这里却是xml,什么鬼,难道这里不是用FreeMarker进行生成的??我们先来看下xml文件中的内容就清楚了:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <template>
  3. <name>controller</name>
  4. <filePath>src/main/java/${packageName}/${moduleName}/web/${subModuleName}</filePath>
  5. <fileName>${ClassName}Controller.java</fileName>
  6. <content><![CDATA[
  7. /**
  8. * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
  9. */
  10. package ${packageName}.${moduleName}.web<#if subModuleName != "">.${subModuleName}</#if>;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import org.apache.shiro.authz.annotation.RequiresPermissions;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.ui.Model;
  17. import org.springframework.web.bind.annotation.ModelAttribute;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  21. import com.thinkgem.jeesite.common.config.Global;
  22. import com.thinkgem.jeesite.common.persistence.Page;
  23. import com.thinkgem.jeesite.common.web.BaseController;
  24. import com.thinkgem.jeesite.common.utils.StringUtils;
  25. import ${packageName}.${moduleName}.entity<#if subModuleName != "">.${subModuleName}</#if>.${ClassName};
  26. import ${packageName}.${moduleName}.service<#if subModuleName != "">.${subModuleName}</#if>.${ClassName}Service;
  27. /**
  28. * ${functionName}Controller
  29. * @author ${functionAuthor}
  30. * @version ${functionVersion}
  31. */
  32. @Controller
  33. @RequestMapping(value = "${r"${adminPath}"}/${urlPrefix}")
  34. public class ${ClassName}Controller extends BaseController {
  35. @Autowired
  36. private ${ClassName}Service ${className}Service;
  37. @ModelAttribute
  38. public ${ClassName} get(@RequestParam(required=false) String id) {
  39. ${ClassName} entity = null;
  40. if (StringUtils.isNotBlank(id)){
  41. entity = ${className}Service.get(id);
  42. }
  43. if (entity == null){
  44. entity = new ${ClassName}();
  45. }
  46. return entity;
  47. }
  48. @RequiresPermissions("${permissionPrefix}:view")
  49. @RequestMapping(value = {"list", ""})
  50. public String list(${ClassName} ${className}, HttpServletRequest request, HttpServletResponse response, Model model) {
  51. Page<${ClassName}> page = ${className}Service.findPage(new Page<${ClassName}>(request, response), ${className});
  52. model.addAttribute("page", page);
  53. return "${lastPackageName}/${viewPrefix}List";
  54. }
  55. @RequiresPermissions("${permissionPrefix}:view")
  56. @RequestMapping(value = "form")
  57. public String form(${ClassName} ${className}, Model model) {
  58. model.addAttribute("${className}", ${className});
  59. return "${lastPackageName}/${viewPrefix}Form";
  60. }
  61. @RequiresPermissions("${permissionPrefix}:edit")
  62. @RequestMapping(value = "save")
  63. public String save(${ClassName} ${className}, Model model, RedirectAttributes redirectAttributes) {
  64. if (!beanValidator(model, ${className})){
  65. return form(${className}, model);
  66. }
  67. ${className}Service.save(${className});
  68. addMessage(redirectAttributes, "保存${functionNameSimple}成功");
  69. return "redirect:"+Global.getAdminPath()+"/${viewPrefix}/?repage";
  70. }
  71. @RequiresPermissions("${permissionPrefix}:edit")
  72. @RequestMapping(value = "delete")
  73. public String delete(${ClassName} ${className}, RedirectAttributes redirectAttributes) {
  74. ${className}Service.delete(${className});
  75. addMessage(redirectAttributes, "删除${functionNameSimple}成功");
  76. return "redirect:"+Global.getAdminPath()+"/${viewPrefix}/?repage";
  77. }
  78. }]]>
  79. </content>
  80. </template>

其中的xml格式为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <template>
  3. <name>controller</name>
  4. <filePath>src/main/java/${packageName}/${moduleName}/web/${subModuleName}</filePath>
  5. <fileName>${ClassName}Controller.java</fileName>
  6. <content><![CDATA[]]>
  7. </content>
  8. </template>

发现其中的奥秘没,他把模板内容都放在了content标签的CDATA[]区。而且config.xml有相对应的bean,用来实现xml转对象:


  1. /**
  2. * 生成方案Entity
  3. * @author ThinkGem
  4. * @version 2013-10-15
  5. */
  6. @XmlRootElement(name="config")
  7. public class GenConfig implements Serializable {
  8. private static final long serialVersionUID = 1L;
  9. private List<GenCategory> categoryList; // 代码模板分类
  10. private List<Dict> javaTypeList; // Java类型
  11. private List<Dict> queryTypeList; // 查询类型
  12. private List<Dict> showTypeList; // 显示类型
  13. public GenConfig() {
  14. super();
  15. }
  16. @XmlElementWrapper(name = "category")
  17. @XmlElement(name = "category")
  18. public List<GenCategory> getCategoryList() {
  19. return categoryList;
  20. }
  21. public void setCategoryList(List<GenCategory> categoryList) {
  22. this.categoryList = categoryList;
  23. }
  24. @XmlElementWrapper(name = "javaType")
  25. @XmlElement(name = "dict")
  26. public List<Dict> getJavaTypeList() {
  27. return javaTypeList;
  28. }
  29. public void setJavaTypeList(List<Dict> javaTypeList) {
  30. this.javaTypeList = javaTypeList;
  31. }
  32. @XmlElementWrapper(name = "queryType")
  33. @XmlElement(name = "dict")
  34. public List<Dict> getQueryTypeList() {
  35. return queryTypeList;
  36. }
  37. public void setQueryTypeList(List<Dict> queryTypeList) {
  38. this.queryTypeList = queryTypeList;
  39. }
  40. @XmlElementWrapper(name = "showType")
  41. @XmlElement(name = "dict")
  42. public List<Dict> getShowTypeList() {
  43. return showTypeList;
  44. }
  45. public void setShowTypeList(List<Dict> showTypeList) {
  46. this.showTypeList = showTypeList;
  47. }
  48. }

其中的

  1. private List<GenCategory> categoryList; // 代码模板分类
  2. private List<Dict> javaTypeList; // Java类型
  3. private List<Dict> queryTypeList; // 查询类型
  4. private List<Dict> showTypeList; // 显示类型

对应的就是xml中预先定义好的

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <config>
  3. <!-- 生成分类 -->
  4. <category>
  5. <category value="curd" label="增删改查(单表)">
  6. <template>curd/controller.xml</template>
  7. ....
  8. </category>
  9. <category value="curd_many" label="增删改查(一对多)">
  10. <template>curd/controller.xml</template>
  11. ....
  12. <childTable>
  13. <template>category-ref:dao</template>
  14. </childTable>
  15. </category>
  16. <category value="dao" label="仅持久层(dao/entity/mapper)">
  17. <template>dao/dao.xml</template>
  18. ....
  19. </category>
  20. <category value="treeTable" label="树结构表(一体)">
  21. <template>treetable/controller.xml</template>
  22. ....
  23. </category>
  24. <category value="treeTableAndList" label="树结构表(左树右表)">
  25. <template>category-ref:dao</template>
  26. </category>
  27. </category>
  28. <!-- java类型 -->
  29. <javaType>
  30. <dict value="String" label="String"/>
  31. ....
  32. </javaType>
  33. <!-- 查询类型 -->
  34. <queryType>
  35. <dict value="=" label="="/>
  36. ....
  37. </queryType>
  38. <!-- 字段显示类型 -->
  39. <showType>
  40. <dict value="input" label="单行文本"/>
  41. <dict value="textarea" label="多行文本"/>
  42. ....
  43. </showType>
  44. </config>

其中的代码模板分类

通过@XmlElement(name = “category”)引用了GenCategory类,其中定义了主子模板

  1. /**
  2. * 生成方案Entity
  3. * @author ThinkGem
  4. * @version 2013-10-15
  5. */
  6. @XmlRootElement(name="category")
  7. public class GenCategory extends Dict {
  8. private static final long serialVersionUID = 1L;
  9. private List<String> template; // 主表模板
  10. private List<String> childTableTemplate;// 子表模板
  11. public static String CATEGORY_REF = "category-ref:";
  12. public GenCategory() {
  13. super();
  14. }
  15. @XmlElement(name = "template")
  16. public List<String> getTemplate() {
  17. return template;
  18. }
  19. public void setTemplate(List<String> template) {
  20. this.template = template;
  21. }
  22. @XmlElementWrapper(name = "childTable")
  23. @XmlElement(name = "template")
  24. public List<String> getChildTableTemplate() {
  25. return childTableTemplate;
  26. }
  27. public void setChildTableTemplate(List<String> childTableTemplate) {
  28. this.childTableTemplate = childTableTemplate;
  29. }
  30. }

这两个主子其实对应的就是

在GenUtils中通过getConfig()方法可获取转换成bean的对象


而其中的数据就是在配置业务表是初始化的一些数据,比如当我们选择某一张表时,然后点击下一步




就会调用GenTableController中的form方法,把一些配置参数设置进去



然后在genTableForm.jsp页面进行渲染

  1. <%@ page contentType="text/html;charset=UTF-8" %>
  2. <%@ include file="/WEB-INF/views/include/taglib.jsp"%>
  3. <html>
  4. <head>
  5. <title>业务表管理</title>
  6. <meta name="decorator" content="default"/>
  7. <script type="text/javascript">
  8. $(document).ready(function() {
  9. $("#comments").focus();
  10. $("#inputForm").validate({
  11. submitHandler: function(form){
  12. loading(\'正在提交,请稍等...\');
  13. $("input[type=checkbox]").each(function(){
  14. $(this).after("<input type=\"hidden\" name=\""+$(this).attr("name")+"\" value=\""
  15. +($(this).attr("checked")?"1":"0")+"\"/>");
  16. $(this).attr("name", "_"+$(this).attr("name"));
  17. });
  18. form.submit();
  19. },
  20. errorContainer: "#messageBox",
  21. errorPlacement: function(error, element) {
  22. $("#messageBox").text("输入有误,请先更正。");
  23. if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
  24. error.appendTo(element.parent().parent());
  25. } else {
  26. error.insertAfter(element);
  27. }
  28. }
  29. });
  30. });
  31. </script>
  32. </head>
  33. <body>
  34. <ul class="nav nav-tabs">
  35. <li><a href="${ctx}/gen/genTable/">业务表列表</a></li>
  36. <li class="active"><a href="${ctx}/gen/genTable/form?id=${genTable.id}&name=${genTable.name}">业务表<shiro:hasPermission name="gen:genTable:edit">${not empty genTable.id?\'修改\':\'添加\'}</shiro:hasPermission><shiro:lacksPermission name="gen:genTable:edit">查看</shiro:lacksPermission></a></li>
  37. </ul>
  38. <c:choose>
  39. <c:when test="${empty genTable.name}">
  40. <form:form id="inputForm" modelAttribute="genTable" action="${ctx}/gen/genTable/form" method="post" class="form-horizontal">
  41. <form:hidden path="id"/>
  42. <sys:message content="${message}"/>
  43. <br/>
  44. <div class="control-group">
  45. <label class="control-label">表名:</label>
  46. <div class="controls">
  47. <form:select path="name" class="input-xxlarge">
  48. <form:options items="${tableList}" itemLabel="nameAndComments" itemValue="name" htmlEscape="false"/>
  49. </form:select>
  50. </div>
  51. <div class="form-actions">
  52. <input id="btnSubmit" class="btn btn-primary" type="submit" value="下一步"/> 
  53. <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
  54. </div>
  55. </div>
  56. </form:form>
  57. </c:when>
  58. <c:otherwise>
  59. <form:form id="inputForm" modelAttribute="genTable" action="${ctx}/gen/genTable/save" method="post" class="form-horizontal">
  60. <form:hidden path="id"/>
  61. <sys:message content="${message}"/>
  62. <fieldset>
  63. <legend>基本信息</legend>
  64. <div class="control-group">
  65. <label class="control-label">表名:</label>
  66. <div class="controls">
  67. <form:input path="name" htmlEscape="false" maxlength="200" class="required" readonly="true"/>
  68. </div>
  69. </div>
  70. <div class="control-group">
  71. <label class="control-label">说明:</label>
  72. <div class="controls">
  73. <form:input path="comments" htmlEscape="false" maxlength="200" class="required"/>
  74. </div>
  75. </div>
  76. <div class="control-group">
  77. <label class="control-label">类名:</label>
  78. <div class="controls">
  79. <form:input path="className" htmlEscape="false" maxlength="200" class="required"/>
  80. </div>
  81. </div>
  82. <div class="control-group">
  83. <label class="control-label">父表表名:</label>
  84. <div class="controls">
  85. <form:select path="parentTable" cssClass="input-xlarge">
  86. <form:option value=""></form:option>
  87. <form:options items="${tableList}" itemLabel="nameAndComments" itemValue="name" htmlEscape="false"/>
  88. </form:select>
  89.  当前表外键:
  90. <form:select path="parentTableFk" cssClass="input-xlarge">
  91. <form:option value=""></form:option>
  92. <form:options items="${genTable.columnList}" itemLabel="nameAndComments" itemValue="name" htmlEscape="false"/>
  93. </form:select>
  94. <span class="help-inline">如果有父表,请指定父表表名和外键</span>
  95. </div>
  96. </div>
  97. <div class="control-group hide">
  98. <label class="control-label">备注:</label>
  99. <div class="controls">
  100. <form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="200" class="input-xxlarge"/>
  101. </div>
  102. </div>
  103. <legend>字段列表</legend>
  104. <div class="control-group">
  105. <table id="contentTable" class="table table-striped table-bordered table-condensed">
  106. <thead><tr>
  107. <th title="数据库字段名">列名</th>
  108. <th title="默认读取数据库字段备注">说明</th>
  109. <th title="数据库中设置的字段类型及长度">物理类型</th>
  110. <th title="实体对象的属性字段类型">Java类型</th>
  111. <th title="实体对象的属性字段(对象名.属性名|属性名2|属性名3,例如:用户user.id|name|loginName,属性名2和属性名3为Join时关联查询的字段)">Java属性名称 <i class="icon-question-sign"></i></th>
  112. <th title="是否是数据库主键">主键</th><th title="字段是否可为空值,不可为空字段自动进行空值验证">可空</th><th title="选中后该字段被加入到insert语句里">插入</th>
  113. <th title="选中后该字段被加入到update语句里">编辑</th><th title="选中后该字段被加入到查询列表里">列表</th>
  114. <th title="选中后该字段被加入到查询条件里">查询</th><th title="该字段为查询字段时的查询匹配放松">查询匹配方式</th>
  115. <th title="字段在表单中显示的类型">显示表单类型</th><th title="显示表单类型设置为“下拉框、复选框、点选框”时,需设置字典的类型">字典类型</th><th>排序</th></tr></thead>
  116. <tbody>
  117. <c:forEach items="${genTable.columnList}" var="column" varStatus="vs">
  118. <tr${column.delFlag eq \'1\'?\' class="error" title="已删除的列,保存之后消失!"\':\'\'}>
  119. <td nowrap>
  120. <input type="hidden" name="columnList[${vs.index}].id" value="${column.id}"/>
  121. <input type="hidden" name="columnList[${vs.index}].delFlag" value="${column.delFlag}"/>
  122. <input type="hidden" name="columnList[${vs.index}].genTable.id" value="${column.genTable.id}"/>
  123. <input type="hidden" name="columnList[${vs.index}].name" value="${column.name}"/>${column.name}
  124. </td>
  125. <td>
  126. <input type="text" name="columnList[${vs.index}].comments" value="${column.comments}" maxlength="200" class="required" style="width:100px;"/>
  127. </td>
  128. <td nowrap>
  129. <input type="hidden" name="columnList[${vs.index}].jdbcType" value="${column.jdbcType}"/>${column.jdbcType}
  130. </td>
  131. <td>
  132. <select name="columnList[${vs.index}].javaType" class="required input-mini" style="width:85px;*width:75px">
  133. <c:forEach items="${config.javaTypeList}" var="dict">
  134. <option value="${dict.value}" ${dict.value==column.javaType?\'selected\':\'\'} title="${dict.description}">${dict.label}</option>
  135. </c:forEach>
  136. </select>
  137. </td>
  138. <td>
  139. <input type="text" name="columnList[${vs.index}].javaField" value="${column.javaField}" maxlength="200" class="required input-small"/>
  140. </td>
  141. <td>
  142. <input type="checkbox" name="columnList[${vs.index}].isPk" value="1" ${column.isPk eq \'1\' ? \'checked\' : \'\'}/>
  143. </td>
  144. <td>
  145. <input type="checkbox" name="columnList[${vs.index}].isNull" value="1" ${column.isNull eq \'1\' ? \'checked\' : \'\'}/>
  146. </td>
  147. <td>
  148. <input type="checkbox" name="columnList[${vs.index}].isInsert" value="1" ${column.isInsert eq \'1\' ? \'checked\' : \'\'}/>
  149. </td>
  150. <td>
  151. <input type="checkbox" name="columnList[${vs.index}].isEdit" value="1" ${column.isEdit eq \'1\' ? \'checked\' : \'\'}/>
  152. </td>
  153. <td>
  154. <input type="checkbox" name="columnList[${vs.index}].isList" value="1" ${column.isList eq \'1\' ? \'checked\' : \'\'}/>
  155. </td>
  156. <td>
  157. <input type="checkbox" name="columnList[${vs.index}].isQuery" value="1" ${column.isQuery eq \'1\' ? \'checked\' : \'\'}/>
  158. </td>
  159. <td>
  160. <select name="columnList[${vs.index}].queryType" class="required input-mini">
  161. <c:forEach items="${config.queryTypeList}" var="dict">
  162. <option value="${fns:escapeHtml(dict.value)}" ${fns:escapeHtml(dict.value)==column.queryType?\'selected\':\'\'} title="${dict.description}">${fns:escapeHtml(dict.label)}</option>
  163. </c:forEach>
  164. </select>
  165. </td>
  166. <td>
  167. <select name="columnList[${vs.index}].showType" class="required" style="width:100px;">
  168. <c:forEach items="${config.showTypeList}" var="dict">
  169. <option value="${dict.value}" ${dict.value==column.showType?\'selected\':\'\'} title="${dict.description}">${dict.label}</option>
  170. </c:forEach>
  171. </select>
  172. </td>
  173. <td>
  174. <input type="text" name="columnList[${vs.index}].dictType" value="${column.dictType}" maxlength="200" class="input-mini"/>
  175. </td>
  176. <td>
  177. <input type="text" name="columnList[${vs.index}].sort" value="${column.sort}" maxlength="200" class="required input-min digits"/>
  178. </td>
  179. </tr>
  180. </c:forEach>
  181. </tbody>
  182. </table>
  183. </div>
  184. </fieldset>
  185. <div class="form-actions">
  186. <shiro:hasPermission name="gen:genTable:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/> </shiro:hasPermission>
  187. <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
  188. </div>
  189. </form:form>
  190. </c:otherwise>
  191. </c:choose>
  192. </body>
  193. </html>

注意其中的name属性

name="columnList[${vs.index}].xxx"

在GenTable中是用list来装的


所以在spring mvc数据绑定的时候可以通过下标的形式来传值




业务表配置好之后,就是生成代码,新建生成方案配置,然后输入一些必须的信息




点击保存并生成代码,会调用GenSchemeController中的save()方法,传入一些参数




然后调用genSchemeService.save()方法进行生成




如果是1的话就生成代码




generateCode方法如下:

  1. private String generateCode(GenScheme genScheme){
  2. StringBuilder result = new StringBuilder();
  3. // 查询主表及字段列
  4. GenTable genTable = genTableDao.get(genScheme.getGenTable().getId());
  5. genTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(genTable.getId()))));
  6. // 获取所有代码模板
  7. GenConfig config = GenUtils.getConfig();
  8. // 获取模板列表
  9. List<GenTemplate> templateList = GenUtils.getTemplateList(config, genScheme.getCategory(), false); //获取所有不包含childTableTemplateList的数据
  10. List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getCategory(), true);
  11. // 如果有子表模板,则需要获取子表列表
  12. if (childTableTemplateList.size() > 0){
  13. GenTable parentTable = new GenTable();
  14. parentTable.setParentTable(genTable.getName());
  15. genTable.setChildList(genTableDao.findList(parentTable));
  16. }
  17. // 生成子表模板代码
  18. for (GenTable childTable : genTable.getChildList()){
  19. childTable.setParent(genTable);
  20. childTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(childTable.getId()))));
  21. genScheme.setGenTable(childTable);
  22. Map<String, Object> childTableModel = GenUtils.getDataModel(genScheme);
  23. for (GenTemplate tpl : childTableTemplateList){
  24. result.append(GenUtils.generateToFile(tpl, childTableModel, genScheme.getReplaceFile()));
  25. }
  26. }
  27. // 生成主表模板代码
  28. genScheme.setGenTable(genTable);
  29. Map<String, Object> model = GenUtils.getDataModel(genScheme);
  30. for (GenTemplate tpl : templateList){
  31. result.append(GenUtils.generateToFile(tpl, model, genScheme.getReplaceFile()));
  32. }
  33. return result.toString();
  34. }

该方法主要是做一些,获取模板,然后进行数据绑定等一些工作。其中的

  1. // 获取模板列表
  2. List<GenTemplate> templateList = GenUtils.getTemplateList(config, genScheme.getCategory(), false); //获取所有不包含childTableTemplateList的数据
  3. List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getCategory(), true);

getTemplateList方法为

  1. /**
  2. * 根据分类获取模板列表
  3. * @param config
  4. * @param category
  5. * @param isChildTable 是否是子表
  6. * @return
  7. */
  8. public static List<GenTemplate> getTemplateList(GenConfig config, String category, boolean isChildTable){
  9. List<GenTemplate> templateList = Lists.newArrayList();
  10. if (config !=null && config.getCategoryList() != null && category != null){
  11. for (GenCategory e : config.getCategoryList()){
  12. if (category.equals(e.getValue())){
  13. List<String> list = null;
  14. if (!isChildTable){
  15. list = e.getTemplate();
  16. }else{
  17. list = e.getChildTableTemplate();
  18. }
  19. if (list != null){
  20. for (String s : list){
  21. if (StringUtils.startsWith(s, GenCategory.CATEGORY_REF)){
  22. templateList.addAll(getTemplateList(config, StringUtils.replace(s, GenCategory.CATEGORY_REF, ""), false));
  23. }else{
  24. GenTemplate template = fileToObject(s, GenTemplate.class);
  25. if (template != null){
  26. templateList.add(template);
  27. }
  28. }
  29. }
  30. }
  31. break;
  32. }
  33. }
  34. }
  35. return templateList;
  36. }

通过标志位来获取

  1. if (!isChildTable){
  2. list = e.getTemplate();
  3. }else{
  4. list = e.getChildTableTemplate();
  5. }

主表,子表模板,生成文件的方法是GenUtils.generateToFile(tpl, model, genScheme.getReplaceFile())

  1. /**
  2. * 生成到文件
  3. * @param tpl
  4. * @param model
  5. * @param isReplaceFile
  6. * @return
  7. */
  8. public static String generateToFile(GenTemplate tpl, Map<String, Object> model, boolean isReplaceFile){
  9. // 获取生成文件
  10. String fileName = Global.getProjectPath() + File.separator
  11. + StringUtils.replaceEach(FreeMarkers.renderString(tpl.getFilePath() + "/", model),
  12. new String[]{"//", "/", "."}, new String[]{File.separator, File.separator, File.separator})
  13. + FreeMarkers.renderString(tpl.getFileName(), model);
  14. logger.debug(" fileName === " + fileName);
  15. // 获取生成文件内容
  16. String content = FreeMarkers.renderString(StringUtils.trimToEmpty(tpl.getContent()), model);
  17. logger.debug(" content === \r\n" + content);
  18. // 如果选择替换文件,则删除原文件
  19. if (isReplaceFile){
  20. FileUtils.deleteFile(fileName);
  21. }
  22. // 创建并写入文件
  23. if (FileUtils.createFile(fileName)){
  24. FileUtils.writeToFile(fileName, content, true);
  25. logger.debug(" file create === " + fileName);
  26. return "生成成功:"+fileName+"<br/>";
  27. }else{
  28. logger.debug(" file extents === " + fileName);
  29. return "文件已存在:"+fileName+"<br/>";
  30. }
  31. }

生成成功之后,会有如下提示:




版权声明:本文为jpfss原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jpfss/p/9989979.html