mybatis多表查询返回map类型
程序员文章站
2022-07-01 07:56:00
...
mybatis进行多表查询时,返回的结果为map而不是某个实体类的时候,如下:
<!-- 查询多表返回类型为map -->
<select id="list" parameterType="User" resultType="map">
SELECT
u.uid,
u.role_id,
m.mid,
m.mname,
r.role_id,
r.mid,
r.iden
FROM
user u
LEFT JOIN role r ON u.role_id = r.role_id
LEFT JOIN module m ON r.mid = m.mid
WHERE
u.uid = #{uid}
</select>
DAO中的成员方法:
public List<Map<String, Object>> list(User user, "findListMap");
对返回的List
@Test
public void listMap() {
User user = new User();
user.setUid(1);
List<Map<String, Object>> listMap = testService.listMap(user, "findListMap");
for (Map<String, Object> map : listMap) {
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(value);
}
}
}
参考:
http://blog.csdn.net/cydbetter/article/details/79074064
https://blog.csdn.net/lxxxzzl/article/details/43604481
https://blog.csdn.net/GensingU/article/details/74856351
https://blog.csdn.net/chenluyao_it/article/details/78747859