Is that possible to generate a list of maps from myBatis

lwp*_*ro2 3 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)
  }

}
Run Code Online (Sandbox Code Playgroud)

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

Zhu*_*kov 8

对的,这是可能的.您可以设置resultTypeHashmap你的XML映射文件,它会返回地图的列表.

<select id="selectFromStaffs" resultType="Hashmap">
    select id, name, age, address from staffs
</select>
Run Code Online (Sandbox Code Playgroud)

而你得到它同样的方式:

...
List listOfMaps = sqlSession.selectList("selectFromStaffs");
...
Run Code Online (Sandbox Code Playgroud)

您可以在" 用户指南"中阅读有关它的更多信息.


nik*_*kli 5

在映射器 xml 类型resultType"map"or "Hashmap",映射器接口中,您必须将 reutrn 类型写为"List"

StaffMapper.xml

<select id="getStaffList" resultType="map">
    select id, name, age, address from staffs
</select>
Run Code Online (Sandbox Code Playgroud)

StaffMapper.java

<select id="getStaffList" resultType="map">
    select id, name, age, address from staffs
</select>
Run Code Online (Sandbox Code Playgroud)