[19/05/08-星期三] JDBC(Java DataBase Connectivity)_ORM(Object Relationship Mapping, 对象关系映射)
一、概念
基本思想:
– 表结构跟类对应; 表中字段和类的属性对应;表中记录和对象对应;
– 让javabean的属性名和类型尽量和数据库保持一致!
– 一条记录对应一个对象。将这些查询到的对象放到容器中(List,Set,Map)
• 将表中的一条记录封装到Object数组中
• 将表中的一条记录封装到map中
• 将表中一条记录封装到javabean对象中
/*** * 测试object数组来封装一条对象,有3种 * 1)将表中的一条记录封装到Object数组中 2)将表中的一条记录封装到map中 3)将表中一条记录封装到javabean对象中 */ package com.sxt.orm; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class Demo01 { public static void main(String[] args) throws Exception { Connection coon=JDBCUtil.getMysqlConnection(); PreparedStatement ps=coon.prepareStatement("select empname,salary from emp where id>?"); ps.setObject(1, 1); ResultSet rs=ps.executeQuery(); List<Object[]> list=new ArrayList<Object[]>();// 如果要放多条记录可以采用容器 while (rs.next()) {//getString("empname")等价于getString(1), 1就是上文中要查询的2个信息(empname,salary)之一 //System.out.println(rs.getString("empname")+" "+rs.getDouble("salary")); Object obj[]=new Object[2]; obj[0]=rs.getString(1);//把接收到的2列信息存到Object类的数组中,通过数组封装了1条记录. obj[1]=rs.getDouble(2); list.add(obj);//把每次查到到的1条记录插到容器中 } JDBCUtil.close(ps, coon, rs);//即使在这里把数据库的引用关了之后,下边任然可以使用,因为已经存到数组中去了 for (Object[] objects : list) { System.out.println(objects[0]+" "+objects[1]);//前边不加个字符串连接会报错 } } }
/*** * 2)使用map封装数据库的记录 */ package com.sxt.orm; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Demo02 { public static void main(String[] args) throws Exception { Connection coon=JDBCUtil.getMysqlConnection(); PreparedStatement ps=coon.prepareStatement("select id, empname,salary from emp where id>?"); ps.setObject(1, 1); ResultSet rs=ps.executeQuery(); /* //存储1条信息 Map<String, Object> row=new HashMap<String, Object>(); while (rs.next()) { row.put("id", rs.getObject("id")); row.put("empname", rs.getObject("empname")); row.put("salary", rs.getObject("salary")); //用键值对存储1条信息 } while (rs.next()) { Map<String, Object> row=new HashMap<String, Object>(); row.put("id", rs.getObject("id")); row.put("empname", rs.getObject("empname")); row.put("salary", rs.getObject("salary")); //用键值对存储1条信息 } //遍历map for (String key : row.keySet()) {// key就是row.put("id", rs.getInt("id"));中前边的关键字 System.out.print(key+":"+row.get(key)+" "); } } */ //借助list 存储多条信息 容器套容器 List< Map<String, Object> > list=new ArrayList< Map<String,Object> >(); while (rs.next()) { Map<String, Object> row=new HashMap<String, Object>(); row.put("id", rs.getObject("id")); row.put("empname", rs.getObject("empname")); row.put("salary", rs.getObject("salary")); //用键值对存储1条信息 list.add(row); //row是1条记录 } //遍历list for (Map<String, Object> row : list) { for (String key : row.keySet()) {// key就是row.put("id", rs.getInt("id"));中前边的关键字 System.out.print(key+":"+row.get(key)+" "); } System.out.println(); } } }
3)使用Javabean
【雇员类】
/*** * 雇员类,一个表对应一个类。一个列属性对应类数属性,一条记录对应一个对象 */ package com.sxt.orm; import java.sql.Date; public class Emp { private Integer id;//使用的是包装类 相当于int型 private String empname; private Integer age; private Date brithday; private Double salary; private Integer deptId; //什么都有的的构造方法 public Emp(Integer id, String empname, Integer age, Date brithday, Double salary, Integer deptId) { this.id = id; this.empname = empname; this.age = age; this.brithday = brithday; this.salary = salary; this.deptId = deptId; } //只有 id empname salary的构造方法 为测试而生 public Emp(Integer id, String empname, Double salary) { this.id = id; this.empname = empname; this.salary = salary; } //空构造器 public Emp() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBrithday() { return brithday; } public void setBrithday(Date brithday) { this.brithday = brithday; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } }
【测试】
/*** * 2)使用Javabean封装 用的最多 */ package com.sxt.orm; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Demo03 { public static void main(String[] args) throws Exception { Connection coon=JDBCUtil.getMysqlConnection(); PreparedStatement ps=coon.prepareStatement("select id, empname,salary from emp where id>?"); ps.setObject(1, 1); List<Emp> list=new ArrayList<Emp>(); ResultSet rs=ps.executeQuery(); Emp emp=null; while (rs.next()) { emp=new Emp(rs.getInt(1),rs.getString(2),rs.getDouble(3)); list.add(emp);//如果查询多条记录加上这句话 } for (Emp tempemp : list) { System.out.println(tempemp.getId()+" "+tempemp.getEmpname()+" "+tempemp.getSalary()); } } }