@RequestMapping(value="/Table/createCode/{tableName}/{tablePrefix}", method=RequestMethod.GET) public void createCode(@PathVariable String tableName, @PathVariable String tablePrefix) throws TemplateException, IOException { String tableShortName = tableName.substring(tableName.indexOf(tablePrefix) + tablePrefix.length() + 1); cfg = new Configuration(Configuration.VERSION_2_3_32); ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), "/templates"); cfg.setTemplateLoader(ctl); cfg.setDefaultEncoding("UTF-8"); root = new HashMap<String, Object>(); List<TableEntity> varList = seService.getTableByName(tableName); Iterator<TableEntity> itr = varList.iterator(); while(itr.hasNext()) { TableEntity item = itr.next(); if(item.getType().equals("bigint")) item.setType("Integer"); else if(item.getType().equals("character varying")) item.setType("String"); } root.put("tableNameUpperCamel", CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableShortName)); root.put("tableNameLowerCamel", CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableShortName)); root.put("tableName", tableName); root.put("tableList", varList); List<String> fileList = new ArrayList<String>(); fileList.add("entity"); fileList.add("controller"); fileList.add("mapper"); fileList.add("service"); try { response.reset(); response.setContentType("application/zip;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=test.zip"); ZipOutputStream zip = new ZipOutputStream(response.getOutputStream()); for(String file : fileList) { WriteHttpStream(zip, file, tableShortName); } zip.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //format return data //return ReturnUtils.CreateReturn(ReturnUtils.CreateReturnData(varList, varList.size())); }
private void WriteHttpStream(ZipOutputStream zip, String templateName, String tableName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException { String fileName = tableName + "_" + templateName; fileName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, fileName); zip.putNextEntry(new ZipEntry(templateName + "/")); zip.closeEntry(); zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + ".java")); Template temp = cfg.getTemplate(templateName + ".ftl"); Writer out = new OutputStreamWriter(zip); temp.process(root, out); zip.flush(); zip.closeEntry(); if(templateName == "mapper") { zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + ".xml")); temp = cfg.getTemplate(templateName + ".xml.ftl"); out = new OutputStreamWriter(zip); temp.process(root, out); zip.flush(); zip.closeEntry(); } if(templateName == "service") { zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + "Impl.java")); temp = cfg.getTemplate(templateName + ".impl.ftl"); out = new OutputStreamWriter(zip); temp.process(root, out); zip.flush(); zip.closeEntry(); } }
package com.accel.UamConfig.entity; import java.util.LinkedList; import java.util.List; public class ${tableNameUpperCamel}Entity { <#list tableList as item> <#assign newName>${item.name?replace("_"," ")?capitalize?uncap_first?replace(" ","")}</#assign> private ${item.type} ${newName}; </#list> <#list tableList as item> <#assign newName>${item.name?replace("_"," ")?capitalize?uncap_first?replace(" ","")}</#assign> public ${item.type} get${newName?capitalize}() { return ${newName}; } public void set${newName?capitalize}(${item.type} ${newName}) { this.${newName} = ${newName}; } </#list> }
标签:java,String,zip,templateName,tableName,item,new From: https://www.cnblogs.com/tju1895/p/17188835.html