One way to do this is to use:
Prepare two ArrayLists, one with property names and one with eigenvalues. Make sure they are in the correct order, i.e. propValuesList [i] must be set to propNamesList [i].
Then put it in the HashMap and pass it as input to the displayed statement:
Map<String,Object> map = new HashMap<String,Object>(); List<String> propNamesList = new ArrayList<String>(); List<String> propValuesList = new ArrayList<String>(); propNamesList.add(0, "USER_NAME"); propNamesList.add(1, "ADDRESS"); propValuesList.add(0, "admin"); propValuesList.add(1, "hyderabad"); map.put("propNames",propNamesList); map.put("propValues",propValuesList);
Then in the displayed statement:
<select id="selectUsers" parameterType="hashmap" resultMap="UserResult"> select * from users where 1 =1 <if test="propNames != null and propValues != null"> <foreach item="propName" index="index" collection="propNames"> and #{propName} = #{propValues[${index}]} </foreach> </if> </select>
Note the use of $ {index} instead of # {index} .
source share