实体对象:
Product.java
package com.pan.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "qxgl_product")
public class Product {
private Integer id;
private String name;
private String remark;
private Double price;
private ProductType type;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "USER_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "REMARK")
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Column(name = "price")
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public void setType(ProductType type) {
this.type = type;
}
@ManyToOne
@Column(name = "type")
public ProductType getType() {
return type;
}
}
ProductType.java
package com.pan.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "productType")
public class ProductType {
private Integer id;
private String name;
private User user;
public void setUser(User user) {
this.user = user;
}
@ManyToOne
@Column(name="user_id")
public User getUser() {
return user;
}
@Id
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Test.java
package com.pan.beans;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="test")
public class Test{
private Integer id;
private String name;
private String serial;
@Id
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="test_name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="serial")
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
}
User.java
package com.pan.beans;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.dreamer.parse.json.JSONBean;
@Table(name="qxgl_user")
public class User extends JSONBean{
private Integer id;
private String name;
private String password;
private Test test;
public void setTest(Test test) {
this.test = test;
}
@ManyToOne
@Column(name="test_id")
public Test getTest() {
return test;
}
@Id
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
上面四个是实体对象,对应数据库中的表。
下面是核心的解析类,只做了一个查询的方法,不过查询还没有做完,条件部分还没有进行匹配,正在完善中...
OrmSupper.java
package org.dreamer.orm;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
public class OrmSupper {
public String select(Object object) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
StringBuffer sb = new StringBuffer();
// 获取类对象
Class<?> cls = object.getClass();
// 获取所有的方法
Method[] methods = cls.getMethods();
sb.append("select * from ");
// 获取表名
Table table = cls.getAnnotation(Table.class);
if (table == null) {
// throw new Exception("table not null");
}
sb.append("\r\n " + table.name() + "");
sb.append("\r\n where 1=1");
for (Method method : methods) {
if (method.getName().indexOf("get") == 0) {
// 取值
Id id = method.getAnnotation(Id.class);
// 列名
Column column = method.getAnnotation(Column.class);
// 外键
ManyToOne manyToOne = method.getAnnotation(ManyToOne.class);
if (manyToOne != null) {
// 获取外键的类型
Object value = method.invoke(object, null);
// sb.append(select(object));
} else if (column != null) {
Object value = method.invoke(object, null);
if (value != null) {
sb.append("\r\n and ");
sb.append(column.name());
sb.append("=");
sb.append("'" + value.toString() + "'");
}
}
}
}
return sb.toString();
}
public String update(Object object) {
return "";
}
public String insert(Object object) {
return "";
}
public String delete(Object object) {
return "";
}
/**
* 获取对象关联的所有外键表
*
* @param object
* @return
* @throws Exception
*/
public String getTables(Object object) throws Exception {
String values = "";
Class class1 = object.getClass();
Table table = (Table) class1.getAnnotation(Table.class);
if(table!=null){
values += " "+table.name() + ",\r\n";
Method[] methods = class1.getMethods();
for (Method method : methods) {
if (method.getName().indexOf("get") == 0) {
ManyToOne manyToOne = method.getAnnotation(ManyToOne.class);
OneToMany oneToMany = method.getAnnotation(OneToMany.class);
if (manyToOne != null || oneToMany != null) {
Object value = method.invoke(object, null);
if (value != null) {
values += getTables(value);
}
}
}
}
}
return values;
}
/**
* 获取对象的所有字段,包括外键关联的字段
*
* @param object
* @return
* @throws Exception
*/
public String getFields(Object object) throws Exception {
String values = "";
Class class1 = object.getClass();
Table table = (Table) class1.getAnnotation(Table.class);
Method[] methods = class1.getMethods();
for (Method method : methods) {
if (method.getName().indexOf("get") == 0) {
Column column=method.getAnnotation(Column.class);
if(column!=null){
values +=" "+table.name() + "."+column.name()+",\r\n";
}
ManyToOne manyToOne = method.getAnnotation(ManyToOne.class);
OneToMany oneToMany = method.getAnnotation(OneToMany.class);
if (manyToOne != null || oneToMany != null) {
Object value = method.invoke(object, null);
if (value != null) {
values += getFields(value)+"";
}
}
}
}
return values;
}
public String getValues(Object object) throws Exception{
String values = "";
Class class1 = object.getClass();
Table table = (Table) class1.getAnnotation(Table.class);
Method[] methods = class1.getMethods();
for (Method method : methods) {
if (method.getName().indexOf("get") == 0) {
ManyToOne manyToOne = method.getAnnotation(ManyToOne.class);
OneToMany oneToMany = method.getAnnotation(OneToMany.class);
try{
Object value = method.invoke(object, new Class[]{});
if (manyToOne != null || oneToMany != null) {
if (value != null) {
values += getValues(value)+"";
}
continue;
}
Column column=method.getAnnotation(Column.class);
if(column!=null&&value!=null){
values +="\r\n and "+table.name() + "."+column.name()+"='"+value+"'";
}
}catch (Exception e) {
}
}
}
return values;
}
private String subString(String string){
return string.substring(0, string.length()-1-"\r\n".length());
}
public String selectObject(Object object) throws Exception{
StringBuffer sBuffer=new StringBuffer();
//字段
sBuffer.append("select\r\n");
sBuffer.append(subString(getFields(object)));
//表
sBuffer.append("\r\n from \r\n");
sBuffer.append(subString(getTables(object)));
//条件
sBuffer.append("\r\n where \r\n");
sBuffer.append(" "+getValues(object).substring("\r\n and".length()));
return sBuffer.toString();
}
}
下面激动人心的时刻到了,来一个测试类...
OrmTest.java
package com.dreamer.orm.test;
import java.lang.reflect.InvocationTargetException;
import javax.mail.internet.NewsAddress;
import org.dreamer.orm.OrmSupper;
import com.github.beans.Repertory;
import com.pan.beans.Product;
import com.pan.beans.ProductType;
import com.pan.beans.Test;
import com.pan.beans.User;
public class OrmTest {
/**
* @param args
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
OrmSupper supper=new OrmSupper();
// com.github.beans.User user=new com.github.beans.User();
// user.setRepertory(new Repertory());
Product product=new Product();
ProductType type=new ProductType();
type.setId(1);
type.setName("科技数码");
User user=new User();
user.setId(1);
user.setName("张三");
Test test=new Test();
product.setType(type);
type.setUser(user);
user.setTest(test);
System.out.println("[Dreamer SQL]\r\n"+supper.selectObject(product));
}
}
执行上代码之后,输出了查询的sql语句...
[Dreamer SQL]
select
qxgl_product.USER_NAME,
qxgl_product.ID,
qxgl_product.type,
productType.name,
productType.id,
productType.user_id,
qxgl_user.name,
qxgl_user.id,
qxgl_user.test_id,
test.test_name,
test.id,
test.serial,
qxgl_user.password,
qxgl_product.REMARK,
qxgl_product.price
from
qxgl_product,
productType,
qxgl_user,
test
where
productType.name='科技数码'
and productType.id='1'
and qxgl_user.name='张三'
and qxgl_user.id='1'
虽然还不完善~但是思路是这样的~~~