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:
1
ID=
'1243124'
and Name=
'John Smith'
Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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! <--" ); HashMap<string,string> source = 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 " ); } } </string,string> |
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