欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java模板代碼生成 java模板代碼生成器

如何使用Freemarker生成java代碼

Freemarker是一個(gè)模板框架。我們可以通過Freemarker進(jìn)行代碼生成或頁面的靜態(tài)生成。 現(xiàn)在簡單的說一下怎樣使用Freemarker Freemarker的主要生成類

創(chuàng)新互聯(lián)專注于武清網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供武清營銷型網(wǎng)站建設(shè),武清網(wǎng)站制作、武清網(wǎng)頁設(shè)計(jì)、武清網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造武清網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供武清網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

public boolean generate(String templateFileName, Map data,

String fileName) {

try {

//取得模板的位置

String templateFileDir=templateFileName.substring(0, templateFileName.lastIndexOf("/"));

//取得模板的名字

String templateFile=templateFileName.substring(templateFileName.lastIndexOf("/"), templateFileName.length());

//取得生成文件的路徑

String genFileDir=fileName.substring(0, fileName.lastIndexOf("/"));

Template template = ConfigurationHelper.getConfiguration(templateFileDir).getTemplate(templateFile);

File fileDir=new File(genFileDir);

org.apache.commons.io.FileUtils.forceMkdir(fileDir);

File output = new File(fileName);

if(output.exists()){

//如何代碼已存在不重復(fù)生成

return false;

}

Writer writer = new FileWriter(output);

template.process(data, writer);

writer.close();

} catch (TemplateException e) {

e.printStackTrace();

return false;

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

代碼中的Map 是模板所需要的數(shù)據(jù),我們可以通過面向?qū)ο竦姆椒ò褦?shù)據(jù)存在模板中public boolean genDaoInterface(String fileName){

DaoModel daoModel=new DaoModel();

//設(shè)置Dao實(shí)現(xiàn)類的包名

daoModel.setPackageName(DaoConstant.PACKAGE);

//取得接口名

String className=StringUtils.substringBefore(fileName,".");

//設(shè)置接口名

daoModel.setClassName(className);

MapString, Object data = new HashMapString, Object();

data.put("model", daoModel);

//設(shè)置生成的位置

String filePath=new String("src/"+package2path(DaoConstant.PACKAGE)+"/"+fileName);

//代碼生成

return super.generate(DaoConstant.INTERFACE_TEMPLATE, data, filePath);

}

data.put("model", daoModel);由這句代碼可看出我們將可以在模板中直接調(diào)用這些數(shù)據(jù)package ${model.packageName};

public interface ${model.className} extends BaseHibernateDao {

}

Java動(dòng)態(tài)生成代碼

可以的,我說說大概思路,很簡單,你自己具體實(shí)現(xiàn)吧,把代碼寫給你沒意義的:

1.將你這段字符串輸出到一個(gè)文件里,用Java類文件的方式命名。

2.調(diào)用外部javac命令將該文件編譯。

3.用類加載器(ClassLoad)動(dòng)態(tài)加載新的class文件并用Class.forName()注冊該類,然后就可以正常使用了。

上面的每一步都能在baidu中找到實(shí)現(xiàn)方法,自己發(fā)揮吧。

java代碼生成器怎么用

zip包,然后自動(dòng)下載下來

1.預(yù)先定義好模板

2.界面輸入相關(guān)參數(shù)

3.解析模板生成代碼并下載

最后放出源代碼:

package com.et.controller.system.createcode;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.et.controller.base.BaseController;

import com.et.util.DelAllFile;

import com.et.util.FileDownload;

import com.et.util.FileZip;

import com.et.util.Freemarker;

import com.et.util.PageData;

import com.et.util.PathUtil;

/**

* 類名稱:FreemarkerController

* 創(chuàng)建人:Harries

* 創(chuàng)建時(shí)間:2015年1月12日

* @version

*/

@Controller

@RequestMapping(value=”/createCode”)

public class CreateCodeController extends BaseController {

/**

* 生成代碼

*/

@RequestMapping(value=”/proCode”)

public void proCode(HttpServletResponse response) throws Exception{

PageData pd = new PageData();

pd = this.getPageData();

/* ============================================================================================= */

String packageName = pd.getString(“packageName”); //包名 ========1

String objectName = pd.getString(“objectName”); //類名 ========2

String tabletop = pd.getString(“tabletop”); //表前綴 ========3

tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前綴轉(zhuǎn)大寫

String zindext = pd.getString(“zindex”); //屬性總數(shù)

int zindex = 0;

if(null != zindext !””.equals(zindext)){

zindex = Integer.parseInt(zindext);

}

ListString[] fieldList = new ArrayListString[](); //屬性集合 ========4

for(int i=0; i zindex; i++){

fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //屬性放到集合里面

}

MapString,Object root = new HashMapString,Object(); //創(chuàng)建數(shù)據(jù)模型

root.put(“fieldList”, fieldList);

root.put(“packageName”, packageName); //包名

root.put(“objectName”, objectName); //類名

root.put(“objectNameLower”, objectName.toLowerCase()); //類名(全小寫)

root.put(“objectNameUpper”, objectName.toUpperCase()); //類名(全大寫)

root.put(“tabletop”, tabletop); //表前綴

root.put(“nowDate”, new Date()); //當(dāng)前日期

DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代碼前,先清空之前生成的代碼

/* ============================================================================================= */

String filePath = “admin/ftl/code/”; //存放路徑

String ftlPath = “createCode”; //ftl路徑

/*生成controller*/

Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);

/*生成service*/

Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);

/*生成mybatis xml*/

Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

/*生成SQL腳本*/

Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

/*生成jsp頁面*/

Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);

Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);

/*生成說明文檔*/

Freemarker.printFile(“docTemplate.ftl”, root, “說明.doc”, filePath, ftlPath);

//this.print(“oracle_SQL_Template.ftl”, root); 控制臺(tái)打印

/*生成的全部代碼壓縮成zip文件*/

FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);

/*下載代碼*/

FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);

}

}

java代碼生成器用途

主要功能: 你只要設(shè)計(jì)好數(shù)據(jù)庫 就可以生成java vo

java DAO jsp

servlet

struts-config配置信息

oracle 建表語句 查詢語句 等

可生成java struts 架構(gòu)的完整的源碼 包括 增加 刪除 修改 查詢等功能的源碼

1.不同的架構(gòu),需要不同的生成器

2.生成器一般需要模板技術(shù),如freeMarker、velocity等

3.生成器也是Java項(xiàng)目,可以自己修改、設(shè)計(jì)、開發(fā)

4.生成器能節(jié)省一定的工作量

學(xué)這個(gè)? 網(wǎng)上都有現(xiàn)成的軟件,會(huì)用就行,使用很簡單。 如果是想學(xué)開發(fā)一個(gè)代碼生成器的話,我覺得沒必要啊。

你把JAVA 語言學(xué)會(huì)了,真正在開發(fā)的時(shí)候自然會(huì)遇見這個(gè)軟件,自然就會(huì)了

本文標(biāo)題:java模板代碼生成 java模板代碼生成器
本文地址:http://www.chinadenli.net/article28/ddoisjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈面包屑導(dǎo)航標(biāo)簽優(yōu)化網(wǎng)站改版虛擬主機(jī)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管