Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is that possible to generate a list of maps from myBatis

Tags:

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.

like image 213
engineer Avatar asked Dec 13 '22 17:12

engineer


1 Answers

Yes it is possible. You can set resultType as Hashmap in your mapper xml file, and it will return a list of maps.

<select id="selectFromStaffs" resultType="Hashmap">
    select id, name, age, address from staffs
</select>

And you get it the same way:

...
List listOfMaps = sqlSession.selectList("selectFromStaffs");
...

You can read more about it in the User Guide.

like image 80
Zhukikov Avatar answered Jan 31 '23 05:01

Zhukikov