问题描述
我想使用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<>>
吗?
推荐答案
您想获得一个Map<Integer,String>
,其中Integer是id
,而String是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<Integer,Category>
,其中Category是具有ID,名称(以及类别表中的其他字段)的类别表的域对象.创建Category域对象之后,使用@MapKey
批注在MyBatis中很容易做到这一点:
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<Integer,String>
,我知道的最简单的方法是创建一个实现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哈希表,并创建单个主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.
最后几点注意事项:
-
为什么要包含
getFromMap()
辅助方法?因为您无法始终控制MyBatis返回的哈希图中列名的大小写.此处有更多详细信息: mybatis- 3.1.1.如何覆盖从mybatis返回的结果图
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!