jeesite快速開發平臺(七)----程式碼生成原理
阿新 • • 發佈:2018-11-20
一、原理講解
jeesite程式碼生成用的是FreeMarker模板引擎結合xml技術來實現的,定義的模板都放在resources/templates/modules/gen下
一看就知道crud就是基本的增刪改查,dao是資料庫操作,treetable是有關樹方面的模板,其中主要的配置檔案就是config.xml,該檔案中定義了生成的模板,以及java型別,查詢型別,欄位顯示型別等一些資料。
-
<?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="This" label="ThisObj" description="生成當前物件"/>
-
<dict value="Custom" label="Custom" description="自定義物件,生成後手動設定"/>
-
</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>
其中
-
<childTable>
-
<template>category-ref:dao
</template>
-
</childTable>
定義了子表,初看jeesite的程式碼生成,有個困惑的地方就是,一般通過FreeMarker進行程式碼生成定義的模板都是ftl格式的,而這裡卻是xml,什麼鬼,難道這裡不是用FreeMarker進行生成的??我們先來看下xml檔案中的內容就清楚了:
-
<?xml version="1.0" encoding="utf-8"?>
-
<template>
-
<name>controller
</name>
-
<filePath>src/main/java/${packageName}/${moduleName}/web/${subModuleName}
</filePath>
-
<fileName>${ClassName}Controller.java
</fileName>
-
<content><![CDATA[
-
/**
-
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
-
*/
-
package ${packageName}.${moduleName}.web<#if subModuleName != "">.${subModuleName}</#if>;
-
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.shiro.authz.annotation.RequiresPermissions;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.ModelAttribute;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
-
-
import com.thinkgem.jeesite.common.config.Global;
-
import com.thinkgem.jeesite.common.persistence.Page;
-
import com.thinkgem.jeesite.common.web.BaseController;
-
import com.thinkgem.jeesite.common.utils.StringUtils;
-
import ${packageName}.${moduleName}.entity<#if subModuleName != "">.${subModuleName}</#if>.${ClassName};
-
import ${packageName}.${moduleName}.service<#if subModuleName != "">.${subModuleName}</#if>.${ClassName}Service;
-
-
/**
-
* ${functionName}Controller
-
* @author ${functionAuthor}
-
* @version ${functionVersion}
-
*/
-
@Controller
-
@RequestMapping(value = "${r"${adminPath}"}/${urlPrefix}")
-
public class ${ClassName}Controller extends BaseController {
-
-
@Autowired
-
private ${ClassName}Service ${className}Service;
-
-
@ModelAttribute
-
public ${ClassName} get(@RequestParam(required=false) String id) {
-
${ClassName} entity = null;
-
if (StringUtils.isNotBlank(id)){
-
entity = ${className}Service.get(id);
-
}
-
if (entity == null){
-
entity = new ${ClassName}();
-
}
-
return entity;
-
}
-
-
@RequiresPermissions("${permissionPrefix}:view")
-
@RequestMapping(value = {"list", ""})
-
public String list(${ClassName} ${className}, HttpServletRequest request, HttpServletResponse response, Model model) {
-
Page<${ClassName}> page = ${className}Service.findPage(new Page<${ClassName}>(request, response), ${className});
-
model.addAttribute("page", page);
-
return "${lastPackageName}/${viewPrefix}List";
-
}
-
-
@RequiresPermissions("${permissionPrefix}:view")
-
@RequestMapping(value = "form")
-
public String form(${ClassName} ${className}, Model model) {
-
model.addAttribute("${className}", ${className});
-
return "${lastPackageName}/${viewPrefix}Form";
-
}
-
-
@RequiresPermissions("${permissionPrefix}:edit")
-
@RequestMapping(value = "save")
-
public String save(${ClassName} ${className}, Model model, RedirectAttributes redirectAttributes) {
-
if (!beanValidator(model, ${className})){
-
return form(${className}, model);
-
}
-
${className}Service.save(${className});
-
addMessage(redirectAttributes, "儲存${functionNameSimple}成功");
-
return "redirect:"+Global.getAdminPath()+"/${viewPrefix}/?repage";
-
}
-
-
@RequiresPermissions("${permissionPrefix}:edit")
-
@RequestMapping(value = "delete")
-
public String delete(${ClassName} ${className}, RedirectAttributes redirectAttributes) {
-
${className}Service.delete(${className});
-
addMessage(redirectAttributes, "刪除${functionNameSimple}成功");
-
return "redirect:"+Global.getAdminPath()+"/${viewPrefix}/?repage";
-
}
-
-
}]]>
-
</content>
-
</template>
其中的xml格式為:
-
<?xml version="1.0" encoding="utf-8"?>
-
<template>
-
<name>controller
</name>
-
<filePath>src/main/java/${packageName}/${moduleName}/web/${subModuleName}
</filePath>
-
<fileName>${ClassName}Controller.java
</fileName>
-
<content><![CDATA[]]>
-
</content>
-
</template>
發現其中的奧祕沒,他把模板內容都放在了content標籤的CDATA[]區。而且config.xml有相對應的bean,用來實現xml轉物件:
-
/**
-