Is that possible to generate a list of maps from myBatis

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Is that possible to generate a list of maps from myBatis

Jackie Li
for example, I have queries "select id, name, age, address from
staffs", instead of having a list of Staff object. I'd prefer to have
a list of maps, as
list{
  map{
    ("id", 123),
    ("name","jackie"),
    ("address", "canada"),
    ("age",26)
  }
  map{
    ("id", 126),
    ("name","james"),
    ("address", "canada"),
    ("age",27)
  }

}

is that possible, and how to do that, if possible? Thanks.
Reply | Threaded
Open this post in threaded view
|

RE: Is that possible to generate a list of maps from myBatis

Poitras Christian
Hi,

I think it's possible but support for map in a resultMap is limited.
You can use this kind of code :
<resultMap id="HashMapResult" type="java.util.HashMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
You will get the result kind of map you are showing in your example.

Be aware that's the only kind of map you can get this way. You will not be able to use the value of a column as a key in your map...
So, for example, you cannot expect to receive a list of map like
List{
  Map {123, "jackie"},
  Map {126, "james"}
}

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de lwpro2
Envoyé : December-03-10 4:46 AM
À : mybatis-user
Objet : Is that possible to generate a list of maps from myBatis

for example, I have queries "select id, name, age, address from
staffs", instead of having a list of Staff object. I'd prefer to have
a list of maps, as
list{
  map{
    ("id", 123),
    ("name","jackie"),
    ("address", "canada"),
    ("age",26)
  }
  map{
    ("id", 126),
    ("name","james"),
    ("address", "canada"),
    ("age",27)
  }

}

is that possible, and how to do that, if possible? Thanks.