【问题标题】:How should I store Map<String, LinkedList<Object>> data into database table?我应该如何将 Map<String, LinkedList<Object>> 数据存储到数据库表中?
【发布时间】:2018-09-03 06:11:19
【问题描述】:

我的地图将字符串存储为键,将对象的 LinkedList 存储为值。现在,我需要将这些数据存储在数据库表中。 我的地图数据类似于 -

键:值

id : [2, 3, 4, 5]

name : [Rohit, Iravati, Uttam, Sushil]

jobrole : [Software Engineer, Software Engineer, Manager, Director]

salary : [100, 100, 100, 100]

而我的表结构是-

[ID、姓名、工作、工资]

我不知道我应该如何从地图中读取数据以创建以下给定的查询以将此地图数据存储在表中 -

  • insert into DBROLTA.Employee(id, name, jobrole, salary) values(2, 'Rohit', 'Software Engineer','100');

  • insert into DBROLTA.Employee(id, name, jobrole, salary) values(3, 'Iravati', 'Software Engineer','100');

  • insert into DBROLTA.Employee(id, name, jobrole, salary) values(4, 'Uttam', 'Manager','100');

谁能帮帮我。

【问题讨论】:

  • 通常使用数据结构List&lt;Map&lt;Object, Object&gt;&gt; 代替您的方法。这样,我们可以使用映射轻松地为列表中的每个元素创建插入语句。我的建议是修复你的数据结构,而不是试图回答这个问题。
  • @TimBiegeleisen 理想情况下应该是List&lt;Employee&gt; :)

标签: java mysql linked-list hashmap


【解决方案1】:

不再将键和相应的值数组存储在 map 中,而是将键存储为 id,将自定义对象存储为值。

其中自定义对象是具有 id、name、jobrole 和 Salary 属性的对象。

创建 Map 并将自定义对象存储到 ID。因此您可以轻松检索对象并创建 sql 语句并执行它们。

 Map<Interger, CustomObject> = new HashMap<Integer, CustomObject>

对于多个插入查询执行使用批量插入和批量执行方法,这是为了避免多次数据库命中。

批量更新操作请参考:How to execute multiple SQL statements from java

【讨论】:

    【解决方案2】:
    public static void main(String[] args) {
        Map<String, LinkedList<Object>> map = new HashMap<>();
        LinkedList<Object> data = new LinkedList<>();
        data.add(2);
        data.add(3);
        map.put("id", data);
    
        data = new LinkedList<>();
        data.add("Rohit");
        data.add("Iravati");
        map.put("name", data);
    
        data = new LinkedList<>();
        data.add("SE");
        data.add("SSE");
        map.put("jobrole", data);
    
        data = new LinkedList<>();
        data.add(100);
        data.add(200);
        map.put("salary", data);
    
        for (int i = 0; i < map.get("id").size(); i++) {
            Object id = map.get("id").get(i);
            Object name = map.get("name").get(i);
            Object jobrole = map.get("jobrole").get(i);
            Object salary = map.get("salary").get(i);
            // TODO Use above values to create your query.
            System.out.println(id + "-" + name + "-" + jobrole + "-" + salary);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 for 循环遍历所有数据并一次构建一个查询字符串

      可能是这样的

      for(int j = 0; j < mapOfData.get("ID").length(); j++){
           strID = mapOfData.get("ID").get(j);
           strName = mapOfData.get("Name").get(j);
      
           query = "INSERT INTO TABLE VALUES (" + strID + ", " + strName + ")";
      }
      

      您遍历地图的整个长度(假设地图中没有丢失数据),访问每个链表并从那里获取值。

      【讨论】:

        【解决方案4】:

        这是一种不好的方法,但是您当前的数据结构并没有给我太多选择。

        public static void main(String[] args) {
        
            //GIVEN
            Map<String, LinkedList<Object>> data = new HashMap<>();
            data.put("id", Stream.of(1, 2, 3)
                    .collect(Collectors.toCollection(LinkedList::new)));
            data.put("name", Stream.of("a", "b", "c")
                    .collect(Collectors.toCollection(LinkedList::new)));
            data.put("jobrole", Stream.of("x", "y", "z")
                    .collect(Collectors.toCollection(LinkedList::new)));
            data.put("salary", Stream.of(10.0, 20.0, 30.0)
                    .collect(Collectors.toCollection(LinkedList::new)));
        
            //Let's fix your data structure first
            Employee[] employeesArr = null;
            for (Map.Entry<String, LinkedList<Object>> entry : data.entrySet()) {
                int count = 0;
                if (employeesArr == null) {
                    employeesArr = new Employee[entry.getValue().size()];
                    for (int i = 0; i < employeesArr.length; i++) {
                        employeesArr[i] = new Employee();
                    }
                }
                switch (entry.getKey()) {
                    case "id":
                        for (Object o : entry.getValue()) {
                            employeesArr[count++].setId((Integer) o);
                        }
                        break;
                    case "name":
                        for (Object o : entry.getValue()) {
                            employeesArr[count++].setName((String) o);
                        }
                        break;
                    case "jobrole":
                        for (Object o : entry.getValue()) {
                            employeesArr[count++].setRole((String) o);
                        }
                        break;
                    case "salary":
                        for (Object o : entry.getValue()) {
                            employeesArr[count++].setSalary((Double) o);
                        }
                        break;
                }
            }
        
            //employeesArr is a much better data structure
            for (int i = 0; i < employeesArr.length; i++) {
                //use PreparedStatement or an ORM
                System.out.println("insert into DBROLTA.Employee(id, name, jobrole, salary) values ("
                        + employeesArr[i].getId() + ", '"
                        + employeesArr[i].getName() + "', '"
                        + employeesArr[i].getRole() + "', "
                        + employeesArr[i].getSalary()
                        + ");");
            }
        
        }
        

        Employee.java

        public class Employee {
            private int id;
            private String name;
            private String role;
            private double salary;
        
            public int getId() {
                return id;
            }
        
            public void setId(int id) {
                this.id = id;
            }
        
            //other getters/setters
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-16
          • 2015-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-27
          相关资源
          最近更新 更多