1

mapper interface:

List<Map<String, String>> selectXXXList(BeanA a);

BeanA:

private Map<String, String> map;

I need iterate this map in xxxMapper.xml. I tried in this way, but it can not work.

<select id = "selectXXXList" resultType="hashMap">
   SELECT * FROM tableA
   WHERE
      1=1 
      <foreach collection="a.map.keys" item="item" index="index" open="" seperator="" close="">
      AND ${item} = #{a.map[${item}]}
      </foreach>
<select>

It is able to iterator the key in this way. But the value of hashMap #{a.map[${item}]} does not work. Any idea? I can not change the interface BTW.

2
  • @VinayHegde it is a dynamic query , I want to use key as column value as condition in WHERE Commented Sep 7, 2019 at 9:48
  • @VinayHegde it is like select * from tableA where HASHMAP_KEY_A = HASH_VALUE_A and HASHMAP_KEY_B = HASHMAP_VALUE_B Commented Sep 7, 2019 at 9:57

1 Answer 1

1

Try this :

<select id="selectXXXList" parameterType="beanA" resultType="hashmap">    
    select * from TABL where 
    <foreach  collection="map"  index="key" item="value"  open=""  separator=" and "  close="">
        ${key}=#{value}
    </foreach>
</select>
Sign up to request clarification or add additional context in comments.

6 Comments

My parameterType has to be beanA, which is not a HASHMAP.
Thanks , I would try it. But I don’t think it work.
I think mybaits can not obtain the flied in a bean in this way. I am not sure,XD.
Ah, I know this way work. But I have to use a bean as parameter.
Replace #{key} with ${key} and this should work. Here is a portable demo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.