0

For example I have the following MySql table:

A    B     C

1    aaa   2017

1    bbb   2018

2    ccc   2016

2    ddd   2015

I want to put all rows into a multimap structure, using column A as key (the key is not unique, so why multimap). I searched and it seems org.apache.commons.dbutils ResultSetHandler only has BeanMapHandler (but not something like BeanMultiMapHandler).

Is there anyway to put rows into a multimap using column A as key? Thanks.

azro
  • 53,056
  • 7
  • 34
  • 70
  • Have you tried any ORM based solution? Google hibernate – nabster Sep 10 '18 at 02:21
  • What is ORM? Thanks. – Carial ZHAO Sep 10 '18 at 03:06
  • Object relation mapping. Popular one in Java ecosystem is Hibernate. However, it might be an overkill for you if your objective is very simple here. Then you can simply use raw SQL query to read tables from database and map them manually to some other data structure, which I believe you are trying to do. I suggest reading this blog and related questions on Hibernate usage on stack overflow https://blog.jooq.org/2016/07/07/what-java-orm-do-you-prefer-and-why-sql-of-course/ – nabster Sep 10 '18 at 03:14

1 Answers1

0

Google Guava can be a possible soulution. Following code can serve as a guideline:

Multimap<Integer, Map<String, Integer> myMultimap = ArrayListMultimap.create();
// create and inflate nested maps here. Code is not shown 
myMultimap.put(1, nestedMap1);
myMultimap.put(1, nestedMap2);
myMultimap.put(2, nestedMap3);
myMultimap.put(2, nestedMap4);
nabster
  • 1,561
  • 2
  • 20
  • 32
Xu Zhenxue
  • 15
  • 4
  • Thank you. the problem is that I do not know if there are some library that can do the query and put row data into multimap. Or I need to write one myself. I know multimap can hold key to many values mapping. – Carial ZHAO Sep 10 '18 at 03:20
  • Mybatis is also a ORM based solution. This related question may help you. [mybatis-with-guava-multimap](https://stackoverflow.com/questions/43321454/mybatis-with-guava-multimap) – Xu Zhenxue Sep 10 '18 at 07:59