本文介绍了是否有可能从myBatis生成地图列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有查询select id, name, age, address from staffs,而不是有Staff对象的列表.我希望有一张地图列表,例如

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.

推荐答案

是可以的.您可以在mapper xml文件中将resultType设置为Hashmap,它将返回地图列表.

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.

这篇关于是否有可能从myBatis生成地图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:23