首页 > 解决方案 > 仅当字符串中有另一个元素时,如何才能使元素出现?以及如何使键显示为值?StringSubstitutor

问题描述

我有一个 HashMap 字符串,它以列名作为键,以列值作为值。

Map<String, String> columnValue = new HashMap<>();
            columnValue.put("userId", "1");
            columnValue.put("orderId", "2");
            columnValue.put("carId", "3");
            columnValue.put("orderDate", "2021-05-21 15:23");
            StringSubstitutor sub = new StringSubstitutor(columnValue);
            String query = "SELECT*FROM user_order Where ${userId} = ? AND  ${orderId}=? AND  ${carId}=?" +
                    "AND ${userAddress}=? AND  ${userDestination}=? AND ${orderCost}=? AND date_format(${orderDate},'%Y-%m-%d %H:%i')=?";

首先,我需要如果具有列名的键存在于 HashMap 中,它将出现在行中,而不是值中。其次,我需要确保如果列名是字符串,那么它后面会出现“and”这个词,除了最后一个元素。如何使用 Apache 的 StringSubstitutor 来做到这一点?

标签: javastringtextapache-commonssubstitution

解决方案


推荐阅读