Reputation: 2089
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'
Upvotes: 12
Views: 34957
Reputation: 1
<resultMap id="currencyExchangeMap" type="com.sample.model.CurrencyExchange">
<result property="currency" column="currency" />
<result property="exchangeRate" column="usdtolocl" />
</resultMap>
where CurrencyExchange class has fields currency , exchangeRate
<select id="getExchangeRates" resultMap="currencyExchangeMap">
SELECT
to_curr as currency ,usdtolocl
FROM
emsg_v2.store_exchange_rate
</select>
@MapKey("currency")
public Map<String,CurrencyExchange> getExchangeRates();
Upvotes: 0
Reputation: 441
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>
Upvotes: 13
Reputation: 7569
@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
Upvotes: 1
Reputation: 2206
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 :)
Upvotes: 8