How to get Map<String, String> as return type using mybatis annotations

28,844

Solution 1

Here is how i do this, without extra method for converting List to Map:

Here is Message class

public class Message {
   private String code;
   private String message;
   GETTERS/SETTERS
}

Mapper

@Select("SELECT code, message FROM MESSAGES")
@MapKey("code")
public Map<String, Message> selectAllMessages();

Unfortunatly it's impossible to create Map<String, String>

Solution 2

the annotation @Select("select a, b from tableA") will return a List of Map where Each map will contain a single entry. You might write a converter for it.

public Map<Object,Object> mapFromListOfMap (List<Map> listOfMap ) {
  Map<Object,Object> map = new HashMap<Object,Object>();
  for(int i = 0; i < listOfMap.size(); i++) {
    Object key = (Object) listOfMap.get(i).get("a");
    Object value = (Object)listOfMap.get(i).get("b");
    map.put(key, value);
  }
  return map;
}

@Select("select a, b from tableA") will return something like this

List[0] -> Map ((key=>'a',value=>1),((key=>'b',value=>'a')))
List[1] -> Map ((key=>'a',value=>2),((key=>'b',value=>'b')))
List[2] -> Map ((key=>'a',value=>3),((key=>'b',value=>'c')))

and the function mapFromListOfMap will make it something like this

Map ((key=>'1',value=>'a'),(key=>'2',value=>'b'),(key=>'3',value=>'c'))

hope this helps :)

Solution 3

@MapKey(a) will return a map with your results keyed by a

EDIT: Interesting result. Haven't tried using annotations (use mappers instead) but AFAIK it looks like it expects the map being HashMap<someA, someB> where someA has a getter and a setter for "a" (like getA, setA)... you can even use the same class (HashMap

Share:
28,844
Mohit Verma
Author by

Mohit Verma

Updated on March 03, 2020

Comments

  • Mohit Verma
    Mohit Verma about 4 years

    Using annotations in mybatis, can we have return type as normal map ?

    Basically, I want something like this

    @Select("select a, b from tableA")
    public Map<String, String> getItems();
    

    Where

    mysql> select * from tableA;
    +------+------+
    | a    | b    |
    +------+------+
    | 1    | a    |
    | 2    | b    |
    | 3    | c    |
    +------+------+
    
    mysql> desc tableA;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | a     | varchar(10) | YES  |     | NULL    |       |
    | b     | varchar(10) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    

    Tried this

    @Select("select a, b from tableA")
    @MapKey("a)
    public Map<String, String> getItems();
    

    but it's giving below exception

    ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'a' in 'class java.lang.String'
    
  • Mohit Verma
    Mohit Verma about 12 years
    Thanks. This is exactly what I am doing right now. Was wondering if mybatis can itself return the map, instead of we doing it.