@MapValue suggest

145 vues
Accéder directement au premier message non lu

宋梓立

non lue,
9 déc. 2021, 10:16:0109/12/2021
à 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);
}

宋梓立

non lue,
9 déc. 2021, 10:16:0109/12/2021
à 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

non lue,
9 déc. 2021, 23:50:4309/12/2021
à 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

non lue,
10 déc. 2021, 09:00:3410/12/2021
à 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

non lue,
11 déc. 2021, 08:28:5911/12/2021
à 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:
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message