Skip to content

Instantly share code, notes, and snippets.

@wicksome
Last active February 15, 2019 09:47
Show Gist options
  • Save wicksome/99c9fb3a928dbd577f99a59c9684095e to your computer and use it in GitHub Desktop.
Save wicksome/99c9fb3a928dbd577f99a59c9684095e to your computer and use it in GitHub Desktop.
Create result to Map or Grouping in Mybatis

Create result to Map or Grouping in Mybatis

@Slf4j
@Service
public class TestBO {
@Autowired
private Mapper mapper;
public static void main(String[] args) {
final List<Integer> ids = new ArrayList<>();
// init ids...
final Map<Integer, String> map1 = MappingHelper.toMap(mapper.get1(ids));
final Map<Long, List<Long>> map2 = MappingHelper.toMapWithListGroupedByKey(mapper.get2(ids));
final Map<Long, Set<Long>> map3 = MappingHelper.toMapWithSetGroupedByKey(mapper.get3(ids));
}
}
@Repository
public interface Mapper {
List<MappingHelper<Integer, String>> get1(@Param("ids") List<Integer> ids);
List<MappingHelper<Integer, String>> get2(@Param("ids") List<Integer> ids);
List<MappingHelper<Integer, String>> get3(@Param("ids") List<Integer> ids);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- @formatter:off -->
<mapper namespace="me.wickso.dao.DomainMapper">
<resultMap id="nameMap" type="me.wickso.model.MappingHelper">
<id property="key" column="dmn_id"/>
<result property="value" column="entp_nm"/>
</resultMap>
<resultMap id="domainTypeMap" type="me.wickso.model.MappingHelper">
<id property="key" column="dmn_id"/>
<result property="value" column="dmn_tp_cd" javaType="me.wickso.model.mapping.DomainType"/>
</resultMap>
<resultMap id="contactInfoMappingHelper" type="me.wickso.model.MappingHelper">
<association property="key" javaType="me.wickso.model.key.ObjectKey">
<constructor>
<arg column="no" javaType="_int"/>
<arg column="uuid" javaType="_int"/>
<arg column="id" javaType="java.lang.Long"/>
</constructor>
<result property="num" column="no"/>
<result property="uuid" column="uuid"/>
<result property="id" column="id"/>
</association>
<association property="value" javaType="me.wickso.model.ContactInfo">
<result property="num" column="no"/>
<result property="uuid" column="uuid"/>
<result property="id" column="id"/>
<result property="search" column="sch_attr"/>
<result property="attr1" column="attr1"/>
<result property="attr2" column="attr2"/>
<result property="attr3" column="attr3"/>
<result property="attr4" column="attr4"/>
<result property="attr5" column="attr5"/>
<result property="attr6" column="attr6"/>
<result property="attr7" column="attr7"/>
<result property="attr8" column="attr8"/>
<result property="attr9" column="attr9"/>
<result property="attr10" column="attr10"/>
</association>
</resultMap>
<select id="get1" resultMap="nameMap">
...
</select>
</mapper>
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* Helper converting list to map.
* @param <K> key
* @param <V> value
*/
@Slf4j
@Getter
@Setter
public class MappingHelper<K, V> {
private K key;
private V value;
/**
* Return map from {@link MappingHelper} list.
* @param list DTO list
* @param <K> key
* @param <V> value
* @return map
*/
public static <K, V> Map<K, V> toMap(List<MappingHelper<K, V>> list) {
if (list == null) {
return Collections.emptyMap();
}
return list.parallelStream().unordered().collect(Collectors.toMap(MappingHelper::getKey, MappingHelper::getValue));
}
/**
* Return {@link Map} with {@link List} element grouped by key.
*/
public static <K, V> Map<K, List<V>> toMapWithListGroupedByKey(List<MappingHelper<K, V>> list) {
if (list == null) {
return Collections.emptyMap();
}
return list.parallelStream()
.unordered()
.collect(Collectors.groupingBy(MappingHelper::getKey, Collectors.mapping(MappingHelper::getValue, Collectors.toList())));
}
/**
* Return {@link Map} with {@link Set} element grouped by key.
*/
public static <K, V> Map<K, Set<V>> toMapWithSetGroupedByKey(List<MappingHelper<K, V>> list) {
if (list == null) {
return Collections.emptyMap();
}
return list.parallelStream()
.unordered()
.collect(Collectors.groupingBy(MappingHelper::getKey, Collectors.mapping(MappingHelper::getValue, Collectors.toSet())));
}
}
@wicksome
Copy link
Author

wip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment