时间:2023-06-12 14:12:01 | 来源:网站运营
时间:2023-06-12 14:12:01 来源:网站运营
Java后端模板引擎对比:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="${mapperName}"> #foreach($map in $methodList) #if(${map.sqlType} == "select") <select id="${map.methodName}" resultType="${map.type}"> ${map.desc} </select> #elseif(${map.sqlType} == "insert") <insert id="${map.methodName}" resultType="${map.type}"> ${map.desc} </insert> #else #end #end</mapper>
public class VelocityTest { public static void main(String[] args) { //得到VelocityEngine VelocityEngine ve = new VelocityEngine(); //得到模板文件 ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "/Users/huhaiquan/project/database-proxy/database-proxy-server/src/test/resources"); ve.init(); Template template = ve.getTemplate("velocity.vm", "UTF-8"); VelocityContext data = new VelocityContext(); data.put("mapperName", "com.xxx.mapperName"); List<Map> methodList = DataUtils.createData(200); data.put("methodList", methodList); try { //生成xml //调用merge方法传入context int num = 1; int total=10000; for (int i=0;i<num;i++){ StringWriter stringWriter = new StringWriter(); long curr = System.currentTimeMillis(); template.merge(data, stringWriter); long end = System.currentTimeMillis();// stringWriter.flush(); total+=(end-curr); } System.out.println("total="+total+",vag="+(total*1f/num)); } catch (Exception e) { e.printStackTrace(); } }}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="${mapperName}"><#list methodList as method> <#if "${method.sqlType}" =="select"> <select id="${method.methodName}" resultType="${method.type}"> ${method.desc} </select> <#elseif "${method.sqlType}" == "insert"> <insert id="${method.methodName}" resultType="${method.type}"> ${method.desc} </insert> </#if></#list></mapper>
public class FreeMTest { public static Template getDefinedTemplate(String templateName) throws Exception{ //配置类 Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); cfg.setDirectoryForTemplateLoading(new File("/Users/huhaiquan/project/database-proxy/database-proxy-server/src/test/resources/")); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); return cfg.getTemplate(templateName); } public static void main(String[] args){ Map<String,Object> data = new HashMap<>(); data.put("mapperName", "com.xxx.mapperName"); List<Map> methodList =DataUtils.createData(200); data.put("methodList", methodList); try { Template template = getDefinedTemplate("freemarker.ftl"); long total = 0; int num = 10000; for (int i=0;i<num;i++){ StringWriter stringWriter = new StringWriter(); long curr = System.currentTimeMillis(); template.process(data,stringWriter); long end = System.currentTimeMillis(); total+=(end-curr); } System.out.println("total="+total+",vag="+(total/num)); } catch (Exception e) { e.printStackTrace(); } }}
关键词:引擎,对比,模板