Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 681
Mohit Verma Avatar asked Feb 09 '12 19:02

Mohit Verma


People also ask

What is resultMap in MyBatis?

The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.

What is resultMap?

ResultMaps is the modern result navigation platform for growth-minded CEOs and their distributed teams. It gives everyone visibility + ownership. From strategy, through projects and results. OLD WAY. NEW WAY.


2 Answers

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>

like image 180
Dmitry Sergeev Avatar answered Oct 05 '22 00:10

Dmitry Sergeev


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 :)

like image 41
Moinul Hossain Avatar answered Oct 04 '22 22:10

Moinul Hossain