@MapValue suggest

152 views
Skip to first unread message

宋梓立

unread,
Dec 9, 2021, 10:16:01 AM12/9/21
to mybatis-user

When I want to get some statistics(with gourp by) or idToSth(eg. userIdToDeptName).

I want to get a map whose key is the first column, value is the second column.

I found there is not a good way to do it.

This is my way to do it now by MybatisKV.

https://paste.ubuntu.com/p/xfTzbHf8FJ/

@Data
public class MybatisKV<K, V> {
   private K key;
   private V val;

   public static <K, V> Map<K, V> toMap(List<? extends MybatisKV<K, V>> list) {
       Map<K, V> map = new HashMap<>(list.size());
       list.forEach(p -> map.put(p.getKey(), p.getVal()));
       return map;
  }
}

I noticed there is an annotation @Mapkey.

Why not craete an annotation @MapValue.

With it, I could write code like this:

https://paste.ubuntu.com/p/9nQGQhHyvc/

public interface UserMapper {
 @MapKey("dept_id")
 @MapValue("user_count")
 @Select("SELECT dept_id, count(*) user_ount FROM users gourp by dept_id)
 Map<Integer, User> selectDeptIdToUserCount(String name);
}

宋梓立

unread,
Dec 9, 2021, 10:16:01 AM12/9/21
to mybatis-user

When I want to get some statistics(with gourp by) or idToSth(eg. userIdToDeptName).

I want to get a map whose key is the first column, value is the second column.

I found there is not a good way to do it.

This is my way to do it now by MybatisKV.

https://paste.ubuntu.com/p/p9z5bvztkv/

@Data
public class MybatisKV<K, V> {
   private K key;
   private V val;

   public static <K, V> Map<K, V> toMap(List<? extends MybatisKV<K, V>> list) {
       Map<K, V> map = new HashMap<>(list.size());
       list.forEach(p -> map.put(p.getKey(), p.getVal()));
       return map;
  }
}

I noticed there is an annotation @Mapkey.

Why not craete an annotation @MapValue.

With it, I could write code like this:

https://paste.ubuntu.com/p/9nQGQhHyvc/

public interface UserMapper {
 @MapKey("dept_id")
 @MapValue("user_count")
 @Select("SELECT dept_id, count(*) user_ount FROM users gourp by dept_id)
 Map<Integer, Integer> selectDeptIdToUserCount(String name);
}

Guy Rouillier

unread,
Dec 9, 2021, 11:50:43 PM12/9/21
to mybatis-user
Have you tried this?  I did not before responding, but according to the MyBatis User's Guide, what you desire is how MyBatis works when you specify a return type of Map.  The @MapKey annotation identifies the column for the key, so the remaining data should automatically become the value.  Let me know if you find that is not the case, and I'll try a quick sample.

--
Guy Rouillier
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/0de14ae1-1fc4-46b1-ac43-dee53a677d07n%40googlegroups.com.

gsoriee

unread,
Dec 10, 2021, 9:00:34 AM12/10/21
to mybati...@googlegroups.com, mybati...@googlegroups.com
Thanks for your reply. I have tried @Mapkey like this:                       

    @MapKey("id")
@Select("SELECT id, name FROM test_data")
Map<Integer, String> testMap();


There is a ReflectionException: "There is no getter for property named 'id' in 'class java.lang.String".
I read mybatis code(org.apache.ibatis.executor.result.DefaultMapResultHandler line .
When we call SqlSession.selectMap,  mybatis will get 'id' from Map's value.
It's seems that my @MapValue suggest is difficult to realize.





gsoriee
gso...@gmail.com

gsoriee

unread,
Dec 11, 2021, 8:28:59 AM12/11/21
to mybati...@googlegroups.com
I found it possible to implement this requirement with interceptors.
On 12/10/2021 12:50Guy Rouillier<guy.ro...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages