本文介绍了在mybatis中返回HashMap,在spring MVC中作为ModelAttribute使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 spring mvc @modelAttribute 在我的 Jsp 页面中显示类别列表.

I want to display list of categories in my Jsp page using spring mvc @modelAttribute.

在我的 mapper.xml 文件中是

In my mapper.xml file is

<select id="selectAllCategories" resultMap="BaseResultMap">
  select id, name from categories
</select>

在我的 Mapper.java 类中,我有方法

In my Mapper.java class I have method

List<Map<String, String>> selectAllCategories();

我想要一个这样的方法:

I want to have a method like this:

Map<Integer, String>`selectAllCategories();

代替List>,这可能吗?

推荐答案

你想得到一个 Map 其中 Integer 是 id 和字符串是 name.如果您的表中有 200 个类别,您的地图中需要 200 个条目,而不是 200 个地图的列表.

You want to get a Map<Integer,String> where the Integer is the id and the String is the name. If there were 200 categories in your table, you would want 200 entries in your map, rather than a list of 200 maps.

MyBatis 不能开箱即用地做到这一点,但您可以使用它的设施来做到这一点.我看到两个选项.

MyBatis can't quite do that out of the box, but you can use its facilities to do that. I see two options.

选项 1:

第一个不是您所要求的,但值得展示.它为您提供了一个 Map,其中 Category 是类别表的域对象,它具有 id、名称(以及类别表中可能的其他字段).创建 Category 域对象后,在 MyBatis 中使用 @MapKey 注释很容易做到这一点:

The first isn't quite what you asked for but is worth showing. It gives you a Map<Integer,Category> where Category is a domain object for the categories table that has id, name (and possibly other fields from the categories table). After you've created the Category domain object, this is quite easy to do in MyBatis using the @MapKey annotation:

@Select("SELECT id, name FROM categories")
@MapKey("id")
Map<Integer,Category> getAllCategories();

在您的代码中,您将执行以下操作:

In your code you would then do:

MyMapper mapper = session.getMapper(MyMapper.class);
Map<Integer,Category> m = mapper.getAllCategories();

这可能适用于您的用例,也可能不起作用,具体取决于您是否可以将名称提取为 Category 对象的属性.

That may or may not work for your use case depending on whether whether you can extract the name as a property of the Category object.


选项 2:

要获得您要求的 Map,我所知道的最简单的方法是创建一个实现 MyBatis ResultHandler 接口.

To get the Map<Integer,String> you asked for, the easiest way I know is to create a class that implements the MyBatis ResultHandler interface.

您的 ResultHandler 将使用 MyBatis 创建的 column-name => column-value 的默认 hashmap 并创建单个 master Map.代码如下:

Your ResultHandler will use the default hashmap of column-name => column-value that MyBatis creates and create a single master Map. Here's the code:

public class CategoryResultHandler implements ResultHandler {

  Map<Integer,String> inMap = new HashMap<Integer,String>();

  public Map<Integer, String> getIdNameMap() {
    return inMap;
  }

  @Override
  public void handleResult(ResultContext rc) {
    @SuppressWarnings("unchecked")
    Map<String,Object> m = (Map<String,Object>)rc.getResultObject();
    inMap.put((Integer)getFromMap(m, "id"),
              (String)getFromMap(m, "name"));
  }

  // see note at bottom of answer as to why I include this method
  private Object getFromMap(Map<String, Object> map, String key) {
    if (map.containsKey(key.toLowerCase())) {
      return map.get(key.toLowerCase());
    } else {
      return map.get(key.toUpperCase());
    }
  }
}

handleResult 方法在类别表中的每一行被调用一次.你告诉 MyBatis 使用 ResultHandler 然后像这样提取你的主地图:

The handleResult method gets called once per row in the category table. You tell MyBatis to use the ResultHandler and then extract your master map like this:

CategoryResultHandler rh = new CategoryResultHandler();
session.select("getAllCategories", rh);
Map<Integer,String> m = rh.getIdNameMap();

这两个中的一个应该适合你.

One of those two should work for you.

最后几点:

  1. 为什么要包含 getFromMap() 辅助方法?因为你不能总是控制 MyBatis 返回的 hashmap 中列名的大小写.更多详情:mybatis- 3.1.1.如何覆盖mybatis返回的resultmap

  1. Why did I include the getFromMap() helper method? Because you can't always control the case of the column name in the hashmap that MyBatis returns. More details here: mybatis- 3.1.1. how to override the resultmap returned from mybatis

我在 mybatis-koans 的 Koan26 中有这些解决方案的工作示例(我根据你的问题添加了它):https://github.com/midpeter444/mybatis-koans

I have working examples of these solutions in Koan26 of the mybatis-koans (which I added based on your question): https://github.com/midpeter444/mybatis-koans

这篇关于在mybatis中返回HashMap,在spring MVC中作为ModelAttribute使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 19:40