设置返回值类型 resultType="java.util.Map"
NowjavaMapper.xml
<select id="selectMap" resultType="java.util.Map">
SELECT id, title, author FROM nowjava LIMIT 1
</select>
NowjavaMapper.java
以Map为最外层容器时就要加 @MapKey("id") 指定提取 id 作为 key。
@MapKey("id")
Map<Long, Object> selectMap();
NowjavaMapperTest.java
@Test
public void selectMap() {
Map<Long, Object> map = nowjavaMapper.selectMap();
System.out.println(JSON.toJSONString(map));
}
输出结果
注意:Map是无序的,所以这里的字段并没有按SQL中的顺序来显示。
{"1":{"author":"nowjava.com","id":1,"title":"时代Java"}}
查询结果能保持 SQL 语句中 select 字段1, 字段2... 的顺序。
NowjavaMapper.xml
设置返回值类型 resultType="java.util.LinkedHashMap" 。
<select id="selectLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM nowjava LIMIT 1
</select>
NowjavaMapper.java
LinkedHashMap<String, Object> selectLinkedHashMap();
NowjavaMapperTest.java
LinkedHashMap<String, Object> linkedHashMap = nowjavaMapper.selectLinkedHashMap();
输出结果
注意:LinkedHashMap中字段按SQL中的顺序显示。
{"id":1,"title":"时代Java","author":"nowjava.com"}
NowjavaMapper.xml
<select id="selectMapList" resultType="java.util.Map">
SELECT id, title, author FROM nowjava
</select>
NowjavaMapper.java
List<Map<String, Object>> selectMapList();
NowjavaMapperTest.java
List<Map<String, Object>> maps = nowjavaMapper.selectMapList();
输出结果
[
{"author":"nowjava.com","id":1,"title":"时代Java"},
{"author":"nowjava.com","id":2,"title":"Java实例"},
{"author":"nowjava.com","id":3,"title":"Java手册"},
...
]
NowjavaMapper.xml
<select id="selectMapMap" resultType="java.util.Map">
SELECT id, title, author FROM nowjava
</select>
NowjavaMapper.java
@MapKey("id")
Map<String, Map<String, Object>> selectMapMap();
NowjavaMapperTest.java
Map<String, Map<String, Object>> stringMapMap = nowjavaMapper.selectMapMap();
输出结果
{
"1":{"author":"nowjava.com","id":1,"title":"时代Java"},
"2":{"author":"nowjava.com","id":2,"title":"Java实例"},
"3":{"author":"nowjava.com","id":3,"title":"Java手册"},
...
}
NowjavaMapper.xml
<select id="selectListLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM nowjava
</select>
NowjavaMapper.java
List<LinkedHashMap<String, Object>> selectListLinkedHashMap();
NowjavaMapperTest.java
List<LinkedHashMap<String, Object>> linkedHashMap = nowjavaMapper.selectListLinkedHashMap();
输出结果
[
{"id":1,"title":"时代Java","author":"nowjava.com"},
{"id":2,"title":"Java实例","author":"nowjava.com"},
{"id":3,"title":"Java手册","author":"nowjava.com"},
...
]
NowjavaMapper.xml
<select id="selectMapLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM nowjava
</select>
NowjavaMapper.java
@MapKey("id")
Map<String, LinkedHashMap<String, Object>> selectMapLinkedHashMap();
NowjavaMapperTest.java
Map<String, LinkedHashMap<String, Object>> mapLinkedHashMaps = nowjavaMapper.selectMapLinkedHashMap();
输出结果
{
"1":{"id":1,"title":"时代Java","author":"nowjava.com"},
"2":{"id":2,"title":"Java实例","author":"nowjava.com"},
"3":{"id":3,"title":"Java手册","author":"nowjava.com"},
...
}
NowjavaMapper.xml
本文系作者在时代Java发表,未经许可,不得转载。
如有侵权,请联系nowjava@qq.com删除。