Use Cases
The following is a code example on how to use Lambda express to build dynamic query the where clause. Here is the use cases:
- client provide a set of query parameters in JSON format or XML format
- Mule flow will transform the request payload to java LinkedHashMap
- We need to write custom transformer to build the database query's where clause
- In the custom transformer, we need to filter the entries with null or empty string value
- The transform should return the string in the format of:
ID='1243124' and Name='John Smith'
Code Example
package com.ggl.util; import java.util.HashMap; import java.util.LinkedHashMap; public class DynamicQueryBuilder { public static void main(String[] argv) { System.out.println("--> Build HashMap Now! <--"); HashMapsource = new LinkedHashMap<>(); source.put("ID", "123456789"); source.put("NAME", "Gary Liu"); source.put("Address", null); source.put("Sex", ""); StringBuilder builder = new StringBuilder(); source.entrySet() .parallelStream() .filter(e->e.getValue() != null) .filter(e->e.getValue().length() != 0) .forEachOrdered(e -> append(builder, e.getKey(), e.getValue())); String tmpResult = builder.toString(); System.out.println(tmpResult); System.out.println (tmpResult.replaceFirst(" and " + "$", "")); } private static void append(StringBuilder builder, String key, String value) { builder.append(key + "='" + value + "' and " ); } }
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing
ReplyDeleteMule 4 Training
Best Mulesoft Online Training