了解ORM思想,为以后操作表数据提前打下基础
表格模板
|---表格数据存储_map和list结合存储整张表
|------表格数据存储_Map和_List结合
|--------每一行数使用一个Map;整个表格使用一个List
ORM思想:对象关系映射
1 package boom.collection; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 11 /** 12 * 测试用容器表格数据的存储 13 * @author Administrator 14 * 15 */ 16 public class TestStoreData { 17 18 public static void main(String[] args) { 19 20 Map<String, Object> row1 = new HashMap<>(); 21 22 row1.put("id", "1001"); 23 row1.put("姓名", "张三"); 24 row1.put("薪水", "20000"); 25 row1.put("入职日期", "2018.9.6"); 26 27 Map<String, Object> row2 = new HashMap<>(); 28 row2.put("id", "1002"); 29 row2.put("姓名", "李四"); 30 row2.put("薪水", "5000"); 31 row2.put("入职日期", "2019.5.9"); 32 33 Map<String, Object> row3 = new HashMap<>(); 34 row3.put("id", "1003"); 35 row3.put("姓名", "王五"); 36 row3.put("薪水", "30000"); 37 row3.put("入职日期", "2010.8.3"); 38 39 List<Map<String, Object>> table1 = new ArrayList<>(); 40 table1.add(row1); 41 table1.add(row2); 42 table1.add(row3); 43 for (Map<String, Object> row : table1) { 44 Set<String> keyset = row.keySet(); 45 for (String key : keyset) { 46 System.out.print(key + ":" + row.get(key) + " "); 47 } 48 System.out.println(); 49 } 50 } 51 52 }
|----表格数据存储_javabean和list结合存储整张表
|------每一行数据使用一个:javabean对象;整个表格用一个Map / List
用户类:javabean
1 /** 2 * 完整的javabean[setter和getter方法,无参构造器] 3 */ 4 class User{ 5 private int id; 6 private String name; 7 private double salary; 8 private String data; 9 10 public User() { 11 } 12 13 public User(int id, String name, double salary, String data) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.salary = salary; 18 this.data = data; 19 } 20 21 public int getId() { 22 return id; 23 } 24 25 public void setId(int id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public double getSalary() { 38 return salary; 39 } 40 41 public void setSalary(double salary) { 42 this.salary = salary; 43 } 44 45 public String getData() { 46 return data; 47 } 48 49 public void setData(String data) { 50 this.data = data; 51 } 52 @Override 53 public String toString() { 54 return "id:" + id + " name:" + name + " salary:" + salary + " data:" + data; 55 } 56 }
测试类:
1 User user1 = new User(1001, "张三", 50000, "2018.2.5"); 2 User user2 = new User(1002, "李四", 18000, "2018.9.5"); 3 User user3 = new User(1003, "王五", 15000, "2018.5.5"); 4 5 List<User> list = new ArrayList<>(); 6 list.add(user1); 7 list.add(user2); 8 list.add(user3); 9 10 for (User u : list) { 11 System.out.println(u); 12 } 13 14 System.out.println("------------------------------------------------"); 15 16 Map<Integer, User> map = new HashMap<>(); 17 map.put(1001, user1); 18 map.put(1002, user2); 19 map.put(1003, user3); 20 Set<Integer> keyset = map.keySet(); 21 for (Integer key : keyset) { 22 System.out.println(key + ":" + map.get(key)); 23 }
效果图: