ORM对象关系映射原理(使用Javabean对象来实现)
使用Javabean对象实现数据库中一条记录的存储和使用List存储多条记录
这里在数据库中建立两张表,然后使用自己写的JDBCUtiles工具类来实现
数据库中emp表对应Emp类
package cn.com.testorm;
import java.sql.Date;
public class Emp { //表结构和类对应
private Integer id;
private String empname;
private Double salary;
private Date birthday;
private int age;
private Integer depId;
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 double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getDepId() {
return depId;
}
public void setDepId(Integer depId) {
this.depId = depId;
}
public Emp(String empname, Double salary) {
this.empname = empname;
this.salary = salary;
}
public Emp() {
}
}
数据库中dept表对应Dept类
package cn.com.testorm;
public class Dept {
private Integer id;
private String dname;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Dept(Integer id, String dname, String address) {
this.id = id;
this.dname = dname;
this.address = address;
}
public Dept(String dname, String address) {
this.dname = dname;
this.address = address;
}
public Dept() {
}
}
测试类
package cn.com.testorm;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("all")
public class Demo03 {
public static void test01() {
//使用Javabean对象来封装一条记录
Connection coon = null;
PreparedStatement ps = null;
ResultSet rs = null;
Emp emp = null;
try {
coon = JDBCUtils.getMySqlCoon();
ps = coon.prepareStatement("select empname,salary,age from emp where id = ?");
ps.setObject(1,1);
rs = ps.executeQuery();
while(rs.next()) {
emp = new Emp(rs.getString(1),rs.getDouble(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,ps,coon);
}
System.out.println(emp.getEmpname()+"--"+emp.getSalary());
}
public static void test02() {
//使用List<Javabean>存储多条记录
Connection coon = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Emp> list = new ArrayList<>();
try {
coon = JDBCUtils.getMySqlCoon();
ps = coon.prepareStatement("select empname,salary,age from emp where id > ?");
ps.setObject(1,1);
rs = ps.executeQuery();
while(rs.next()) {
Emp emp = new Emp(rs.getString(1),rs.getDouble(2));
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,ps,coon);
}
for (Emp emp:list) {
System.out.println(emp.getEmpname()+"--"+emp.getSalary());
}
}
public static void main(String[] args) {
// test01();
test02();
}
}