1.準備資料庫
drop database if exists orm_test;
create database if not exists orm_test character set gbk;
use orm_test;
create table person_table
(
id int auto_increment primary key,
name varchar(255) not null,
age int,
height int,
weight float
);
insert into person_table values(null, 'zhangsan', 21, 175, 60);
insert into person_table values(null, 'lisi', 30, 180, 70);
insert into person_table values(null, 'wangwu', 21, 178, 59);
insert into person_table values(null, 'leeyohn', 21, 176, 60);
insert into person_table values(null, 'yun', 22, 168, 50);
insert into person_table values(null, 'zhaoliu', 21, 170, 80);
insert into person_table values(null, 'king', 25, 168, 68);
2.編寫實體類
public class Person
{
private int id;
private String name;
private int age;
private int height;
private float weight;
//無參數構造器
public Person()
{
}
//初始化全部屬性的構造器
public Person(int id, String name, int age, int height
, float weight)
{
this.id = id;
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
//id屬性的setter和getter方法
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
//name屬性的setter和getter方法
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
//age屬性的setter和getter方法
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
//height屬性的setter和getter方法
public void setHeight(int height)
{
this.height = height;
}
public int getHeight()
{
return height;
}
//weight屬性的setter和getter方法
public void setWeight(float weight)
{
this.weight = weight;
}
public float getWeight()
{
return weight;
}
public String toString()
{
return "name=" + name + " age=" + age
+ " height=" + height + " weight=" + weight;
}
}
SimpleORM.java
import java.lang.reflect.*;
import java.sql.*;
/*
* Copyright 2009-2012 the original author or authors.
* Description:
* <br/>網站: <a href="http://www.crazyit.org" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" mce_href="http://www.crazyit.org" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >瘋狂Java聯盟</a>
* <br/>Program Name: SimpleORM.java
* <br/>Project Name:
* <br/>Date: 2010-2-11
* @author : leeyohn
* @e-mail : [email protected]
* @QQ : 1417515725
* @version : 1.0
*/
public class SimpleORM
{
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private void getConnection() throws Exception
{
if (conn == null)
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/orm_test", "root", "12345");
}
}
public Object get(String table, int id, Class<?> clazz) throws Exception
{
getConnection();
pstmt = conn.prepareStatement("select * from " + table + " where id=?");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
Object target = clazz.newInstance();
Method[] methods = clazz.getDeclaredMethods();
//擷取結果集中繼資料
ResultSetMetaData meta = pstmt.getMetaData();
//擷取表中列數
int cols = meta.getColumnCount();
for (int i = 0; i < cols; i++)
{
String colName = meta.getColumnName(i + 1);
for (int j = 0; j < methods.length; j++)
{
//是否比對setter方法
if (methods[j].getName().equalsIgnoreCase("set" + colName))
{
methods[j].invoke(target, rs.getObject(i + 1));
}
}
}
return target;
}
return null;
}
public void closeRs() throws Exception
{
if (rs != null)
{
rs.close();
}
if (pstmt != null)
{
pstmt.close();
}
if (conn != null)
{
conn.close();
}
}
public static void main(String[] args) throws Exception
{
SimpleORM orm = new SimpleORM();
Person p = (Person)orm.get("person_table", 1, Person.class);
System.out.println(p);
orm.closeRs();
}
}