Freemark 解析工具类



 * 修改人姓名             修改时间            版本号                  描述
 */

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;



import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 
 * FreeMaker解析<br>
 * 
 * @author 12010065
 * 
 */
public class FreeMakerParser {
    private static final String DEFAULT_TEMPLATE_KEY = "default_template_key";
    private static final String DEFAULT_TEMPLATE_EXPRESSION = "default_template_expression";
    private static final Configuration CONFIGURER = new Configuration();
    static {
        CONFIGURER.setClassicCompatible(true);
        CONFIGURER.setSharedVariable("hash",new DalHash());
    }
    /**
     * 配置SQL表达式缓存
     */
    private static Map<String, Template> templateCache = new HashMap<String, Template>();
    /**
     * 分库表达式缓存
     */
    private static Map<String, Template> expressionCache = new HashMap<String, Template>();

    public static String process(String expression, Map<String, Object> root) {
        StringReader reader = null;
        StringWriter out = null;
        Template template = null;
        try {
            if (expressionCache.get(expression) != null) {
                template = expressionCache.get(expression);
            }
            if (template == null) {
                template = createTemplate(DEFAULT_TEMPLATE_EXPRESSION, new StringReader(expression));
                expressionCache.put(expression, template);
            }
            out = new StringWriter();
            template.process(root, out);
            return out.toString();
        } catch (Exception e) {
            throw new DalException(e);
        } finally {
            if (reader != null) {
                reader.close();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                return null;
            }
        }
    }

    private static Template createTemplate(String templateKey, StringReader reader) throws IOException {
        Template template = new Template(DEFAULT_TEMPLATE_KEY, reader, CONFIGURER);
        template.setNumberFormat("#");
        return template;
    }

    public static String process(Map<String, Object> root, String sql, String sqlId) {
        StringReader reader = null;
        StringWriter out = null;
        Template template = null;
        try {
            if (templateCache.get(sqlId) != null) {
                template = templateCache.get(sqlId);
            }
            if (template == null) {
                reader = new StringReader(sql);
                template = createTemplate(DEFAULT_TEMPLATE_KEY, reader);
                templateCache.put(sqlId, template);
            }
            out = new StringWriter();
            template.process(root, out);
            return out.toString();
        } catch (Exception e) {
            throw new DalException(e);
        } finally {
            if (reader != null) {
                reader.close();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                return null;
            }
        }
    }
}
  public <T> List<T> queryForList(String sqlId, Map<String, Object> paramMap,
            RowMapper<T> rowMapper, int num, DBType dbType) {
        processTableRoute(paramMap);
        SqlBean sqlBean = getSQL(sqlId);
        String sql = FreeMakerParser.process(paramMap, sqlBean.getContent(), sqlId);
        NamedParameterJdbcTemplate template = getJdbcTemplate(paramMap, sqlBean);
        return template.query(generatePaginationSqlForRandom(sql, num, dbType),
                DalUtils.mapIfNull(paramMap), rowMapper);





猜你喜欢

转载自blog.csdn.net/liuao107329/article/details/79570371
今日推荐