模板字符串解析(占位符)

本文介绍了如何利用MyBatis的内置解析包org.apache.ibatis.parsing来简化字符串模板和占位符的处理,使得在消息拼装等场景下操作变得更加便捷。
摘要由CSDN通过智能技术生成

在进行消息拼装或者其他字符串拼装的时候很麻烦,可使用mybatis现有的解析包来处理“占位符模板”就会变得极为简单,引用包org.apache.ibatis.parsing

  1. 默认
    Properties props = new Properties();
    props.put("otherParam.a", "123");
    props.put("otherParam.b", "456");
    props.put("otherParam.c", "789");
    String str = PropertyParser.parse("123=${otherParam.a},456=${otherParam.c},789=${otherParam.b}", props);

     

  2. 自定义
    //使用Properties
    public static String genericTokenParser(Properties properties, String str) {
        TokenHandler  handler = new TokenHandler() {
            @Override
            public String handleToken(String content) {
                return (String) properties.get(content);
            }
        };
        GenericTokenParser parser = new GenericTokenParser("${", "}", handler);
        return parser.parse(str);
    }
    //使
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值