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);





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值