前台传的字符串是A,B 后台要把字符串写到sql里,所以字符串要转为'A','B' sql写成in('A','B'),怎么转?查询用的JdbcTemplate,或者还有什么比较好的方案嘛,小弟感激不尽
4条回答 默认 最新
- tkzc_shark 2019-06-05 16:05关注
google的CharMatcher和Strings工具类
public List<TypePriceInfo> listTypePriceByProductIds(List<Long> productIds) { String sqlInParam = CharMatcher.is(',').trimFrom(Strings.repeat("?,", productIds.size())); String sql = BASE_TYPE_PRICE_INFO_SQL + "where p.product_id in(" + sqlInParam + ")"; List<TypePriceInfo> priceInfos = jdbcTemplate.query(sql, productIds.toArray(), typePriceInfoRowMapper); if (priceInfos == null) { return Collections.emptyList(); } else { return priceInfos; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报