mybatis-3 icon indicating copy to clipboard operation
mybatis-3 copied to clipboard

map value

Open min1854 opened this issue 1 year ago • 6 comments

Feature request

In daily query, only the key of the map can be defined. I hope that the value of the map can also be defined, which can be returned directly Map<Integer, Integer>

public interface NoticeMapper {

  @MapKey("status")
  @MapValue("count")
  Map<Integer, Integer> groupStatus();


}
<mapper namespace="org.apache.ibatis.submitted.mapkey_value.NoticeMapper">



  <resultMap id="groupStatusMap" type="hashmap">
    <result column="count" javaType="Integer" property="count"/>
    <result column="status" javaType="Integer" property="status"/>
  </resultMap>


  <select id="groupStatus" resultMap="groupStatusMap">
  select count(*) as count , status
    from notice group by status
  </select>



</mapper>

https://github.com/mybatis/mybatis-3/pull/2863

min1854 avatar Apr 15 '23 12:04 min1854

Feature request

In daily query, only the key of the map can be defined. I hope that the value of the map can also be defined, which can be returned directly Map<Integer, Integer>

public interface NoticeMapper {

  @MapKey("status")
  Map<Integer, HashMap<Integer, Integer>> groupStatus();



}
<mapper namespace="org.apache.ibatis.submitted.mapkey_value.NoticeMapper">




  <select id="groupStatus" resultMap="hashMap">
  select count(*) as count , status
    from notice group by status
  </select>


</mapper>

#2863

Similarly, we want to support a collection of map value objects rather than a single example for jpa:

  @ElementCollection
  private Map<Long, Money> creditReservations;

isfong avatar Apr 25 '23 05:04 isfong

Hello @min1854 ,

Thank you for the idea and the PR.

I don't understand the @isfong 's request (please open a new issue if it's not exactly the same request), but the original request seems to be the same as #2672.

As I wrote in the issue, there are many similar but slightly different requests and, personally, I would expect users to use ResultHandler or Java's stream for this type of requirements. However, if other committers are interested in supporting @MapValue, I would not object (I may add a few requests on #2863 if it's green-lit, though).

harawata avatar Apr 25 '23 17:04 harawata

Feature request In daily query, only the key of the map can be defined. I hope that the value of the map can also be defined, which can be returned directly Map<Integer, Integer>

public interface NoticeMapper {

  @MapKey("status")
  Map<Integer, HashMap<Integer, Integer>> groupStatus();



}
<mapper namespace="org.apache.ibatis.submitted.mapkey_value.NoticeMapper">




  <select id="groupStatus" resultMap="hashMap">
  select count(*) as count , status
    from notice group by status
  </select>


</mapper>

#2863

Similarly, we want to support a collection of map value objects rather than a single example for jpa:

  @ElementCollection
  private Map<Long, Money> creditReservations;

I think this is difficult and requires a lot of consideration

min1854 avatar Apr 26 '23 02:04 min1854

Hello @min1854 ,

Thank you for the idea and the PR.

I don't understand the @isfong 's request (please open a new issue if it's not exactly the same request), but the original request seems to be the same as #2672.

As I wrote in the issue, there are many similar but slightly different requests and, personally, I would expect users to use ResultHandler or Java's stream for this type of requirements. However, if other committers are interested in supporting @MapValue, I would not object (I may add a few requests on #2863 if it's green-lit, though).

Yes, the two requests are the same, but since the map keys are already available and values cannot be defined, one dto needs to be defined at a time. Using Java's stream and ResultHandler is not convenient and elegant in code

Whenever there are only two columns, I need to define an additional DTO instead of directly converting the results into a map, so encoding also requires repeating the additional definition of DTO

I think most people want to directly convert to a map when querying two columns, but when they find that 'MapKey' can only support keys and cannot define values, In the end, only DTO can be defined and converted to a map using 'Java's stream'. I often encounter this problem when using it

Mybatis is already able to convert to a map, but the results still need to be processed again, which feels very troublesome

So I hope to add the function of defining map value

min1854 avatar Apr 26 '23 05:04 min1854

@min1854 ,

Sorry for the belated reply. I was writing a reply, but forgot to post it.

I just wanted to let you know that if your @MapValue satisfies your requirement, custom DTO class won't be necessary even with the current version.

@Select("select status, count(*) cnt from ...")
List<Map<String, Object>> selectMaps();
Map<Integer, Integer> statCountMap = selectMaps().stream()
  .collect(Collectors.toMap(e -> (Integer) e.get("status"), e -> (Integer) e.get("cnt")));

Also, you can put the stream processing code in a default method of the mapper.

default Map<Integer, Integer> getStatCountMap() {
  return selectMaps().stream()
    .collect(Collectors.toMap(e -> (Integer) e.get("status"), e -> (Integer) e.get("cnt")));
}

harawata avatar Jul 13 '23 19:07 harawata

@min1854 ,

Sorry for the belated reply. I was writing a reply, but forgot to post it.

I just wanted to let you know that if your @MapValue satisfies your requirement, custom DTO class won't be necessary even with the current version.

@Select("select status, count(*) cnt from ...")
List<Map<String, Object>> selectMaps();
Map<Integer, Integer> statCountMap = selectMaps().stream()
  .collect(Collectors.toMap(e -> (Integer) e.get("status"), e -> (Integer) e.get("cnt")));

Also, you can put the stream processing code in a default method of the mapper.

default Map<Integer, Integer> getStatCountMap() {
  return selectMaps().stream()
    .collect(Collectors.toMap(e -> (Integer) e.get("status"), e -> (Integer) e.get("cnt")));
}

This is not elegant, In my opinion, this should be the work done by the framework.

I think the mapping work should be addressed by the framework

Maintaining duplicate default methods is also a hassle point

min1854 avatar Jul 14 '23 09:07 min1854