mybatis / old-google-code-issues

Automatically exported from code.google.com/p/mybatis
2 stars 4 forks source link

queryForMap does not use a column as map #195

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
3.0.3

Please describe the problem.  Unit tests are best!
if i use 

"select id as key, name as value from table" 

in a queryForMap Method
on the following table:

id | name
1  | John

in ibatis 2.x it produces a Map with id as key and name as value.

Map (1 = John)

Mybatis 3 (using @MapKey annotation) produces a Map with id as key and a 
Hashmap (key = column Name, value = value) as value:

Map (1=Map(id= 1, name = John))

after searching the net, i did not find any sollution to this.

I would like to have a sort of @MapValue annotaion so i can create a mapper the 
following way

@MapKey("id")
@MapValue("name")
Map <Integer, String> getNames();

that produces my desired map.

What is the expected output? What do you see instead?

see obove.

Please provide any additional information below.
referes to issue http://code.google.com/p/mybatis/issues/detail?id=155

Original issue reported on code.google.com by Juergen....@gmail.com on 2 Dec 2010 at 11:48

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Issue 413 has been merged into this issue.

Original comment by jeffgbut...@gmail.com on 14 Oct 2011 at 4:07

GoogleCodeExporter commented 9 years ago

Original comment by jeffgbut...@gmail.com on 14 Oct 2011 at 4:09

GoogleCodeExporter commented 9 years ago
We are facing the same issue while migrating from iBatis to myBatis. Since 
queryForMap is being used mostly while loading metadata, it requires 
significant effort while migrating from iBatis to myBatis.

It would be good if this could be taken up soon.

Original comment by vivek1...@gmail.com on 30 Nov 2011 at 12:53

GoogleCodeExporter commented 9 years ago
Jeff I was having a look at 443 and found this bug by chance. Sorry if you had 
already started with it.

Fixed in r4625

Original comment by eduardo.macarron on 31 Jan 2012 at 9:35

GoogleCodeExporter commented 9 years ago
Should it be better to have

    } else if (method.isAnnotationPresent(MapKey.class) && Map.class.isAssignableFrom(returnType)) {
      Type returnTypeParameter = method.getGenericReturnType();

rather than

    } else {
      final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);
      if (mapKeyAnnotation != null && Map.class.isAssignableFrom(returnType)) {
        Type returnTypeParameter = method.getGenericReturnType();

Original comment by wong...@gmail.com on 1 Feb 2012 at 4:08

GoogleCodeExporter commented 9 years ago
Maybe I missed something.  I only managed to get this form

  @Select("select id, name from users where id=1")
  Map<Integer, Object> getUsersMap2();

to work when the result set has not more than 1 row or a 
TooManyResultsException is thrown.  How do I get a bunch of name value pairs 
into a map directly?

Original comment by wong...@gmail.com on 1 Feb 2012 at 4:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hi Wongkwl, yep getAnnottion is better.

And yes, that can only work with one record. Note that the record is returned 
as a Map. In that case

ID=1
NAME=Pocoyo

So just one record can be retrieved.

To get more than one result the mapper should look like this:

List<Map<Integer, Object>> getAListOfMaps();

Have a look at the updated test in r4627

Original comment by eduardo.macarron on 1 Feb 2012 at 7:47

GoogleCodeExporter commented 9 years ago
I think the test case should be

    @Select("select id, name from users where id=1")
    Map<**String**, Object> getUserAsAMap();

because the result is a mapping of field name to field value, but not id to 
name.  By the way, the id to name mapping is, I think, what the reporter wants, 
i.e.

    1=Pocoyo

but not what you said

    ID=1
    NAME=Pocoyo

I know this is quite difficult to achieve but it makes things nice.

Original comment by wong...@gmail.com on 1 Feb 2012 at 7:53

GoogleCodeExporter commented 9 years ago
Right, that should a String instead of an Integer, that was a bad copy & paste.

And you are right also, with the issue, I fixed my own bug but not the original 
one that I think it is not supported by MyBatis 3 so that will be a feature 
request.

Will keep this open and fill a new one to keep track of the solved bug.

Thanks for your help!

Original comment by eduardo.macarron on 1 Feb 2012 at 8:04