之前想在mybatis做一个map数组,返回的类型是Map<String,List<User>>,然后处理数据的时候,就能通过这种key-value的形式获取到对应的list。
想法是这样的
//mapper接口
@MapKey("key")
public Map<String,List<User>> getUser();
//xml文件
<resultMap id="baseMap" type="hashMap">
<resule property="key" column="id" jdbcType="VARCHAR"/>
<collection property="value" oftype="com.abc.entity.User">
<resule property="userName" column="UserName" jdbcType="VARCHAR"/>
<resule property="age" column="age" jdbcType="VARCHAR"/>
</collection>
</resultMap>
<select id="getUser" resultMap="baseMap">
select id,userName,age from User
</select>
这个没什么实际意义,但是想要的东西就是这样,最终没能实现这个想法
后来大佬给了另外一种方法,就是用java8的流计算进行分组
//先普通获取数据
<select id="userList" resultMap="baseMap">
select id,userName,age from User
</select>
List<User> userList = mapper.userList();
Map<String,List<User>> list = userList.stram().collect(Collectors.groupingBy(User::getId,Collectors.toList()));
//这样就能根据id分组