zip包,然后自動下載下來
創(chuàng)新互聯(lián)是專業(yè)的阿克蘇網(wǎng)站建設(shè)公司,阿克蘇接單;提供成都做網(wǎng)站、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行阿克蘇網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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)建時間: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); 控制臺打印
/*生成的全部代碼壓縮成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”);
}
}
try {
using (TransactionScope scope = new TransactionScope()) {
FileDelTest myElistment = new FileDelTest("D:\\123.test");
Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);
throw new Exception("test");//引發(fā)異常跳出using執(zhí)行回滾
scope.Complete();
MessageBox.Show("main-OK");
}
} catch (System.Transactions.TransactionException ex) {
MessageBox.Show("main-" + ex.Message);
} catch (Exception ex) {
MessageBox.Show("main-" + ex.Message);
}
自動化開發(fā)工具,我用過,比較著名的有普元,浪潮樓上平臺等。
首先,我要糾正下,ANT并不是自動生成代碼用的工具;
那些所謂的自動代碼生成器根本原理就是根據(jù)實現(xiàn)寫事先好的模板,再根據(jù)你提供的數(shù)據(jù)庫結(jié)構(gòu),生成一系列的增刪改查方法。的確是可以減少程序員的工作量,但是不能包含復(fù)雜或者特殊的業(yè)務(wù)邏輯,否則程序員全都失業(yè)了。