java 为一个实体类中已赋值的属性生成sql语句

1、实体类:

package lixx.reflect;

public class Task {

private String name = null;
private String pass = null;
private String test = null;
private String test1="";
private double i = 1.0;
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public String getPass() {
   return pass;
}
public void setPass(String pass) {
   this.pass = pass;
}
public double getI() {
   return i;
}
public void setI(double i) {
   this.i = i;
}
public String getTest() {
   return test;
}
public void setTest(String test) {
   this.test = test;
}
}
2、ORM类:

package lixx.reflect;

public class ORM {

private String type = null;
private String name = null;
private String value = null;

public ORM(){
   type = "";
   name = "";
   value = "";
}
public String getType() {
   return type;
}
public void setType(String type) {
   this.type = type;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public String getValue() {
   return value;
}
public void setValue(String value) {
   this.value = value;
}


}
3、ORM操作类:

package lixx.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public class OperateORM {

private Object instance;
private String namespace;
private Class cls;
public OperateORM(Object obj,String namespace){
   this.instance = obj;
   this.namespace = namespace;
   try {
    cls = Class.forName(namespace);
   } catch (ClassNotFoundException e) {
    return ;
   }
}

public String generateSQL() throws ClassNotFoundException,
    IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
   HashMap<String, ORM> fieldsMap = getFiledsName();
   HashMap<String, Method> methodsMap = getMethodsName();
  
   Iterator iterator = methodsMap.keySet().iterator();
   while(iterator.hasNext()){
    String keyName = iterator.next().toString();
    Method method = methodsMap.get(keyName);
    Object obj = getMethodValue(method);
    if(obj == null) continue;
    String value = getMethodValue(method).toString();
    ORM orm = fieldsMap.get(keyName);
    orm.setValue(value);
   }
  
   String _sql = combineSQL(fieldsMap.values().toArray(new ORM[]{}));
   return _sql;
}

private String combineSQL(ORM[] orms){
   StringBuffer sql = new StringBuffer("");
   for(int i =0;i<orms.length;i++){
    ORM orm = orms[i];
    if(orm.getValue() != null && !orm.getValue().equals("")){
     if(!orm.getType().equals("java.lang.String")){
      if(orm.getType().equals("int")){
       if(Integer.parseInt(orm.getValue())>0)
       sql.append(orm.getName()+"="+Integer.parseInt(orm.getValue())+" and ");
      }else if(orm.getType().equals("float")){
       if(Float.parseFloat(orm.getValue())>0)
       sql.append(orm.getName()+"="+Float.parseFloat(orm.getValue())+" and ");
      }else if(orm.getType().equals("double")){
       if(Double.parseDouble(orm.getValue())>0)
       sql.append(orm.getName()+"="+Double.parseDouble(orm.getValue())+" and ");
      }
      //sql.append(" and ");
     }else{
      sql.append(orm.getName()+"='"+orm.getValue()+"' and ");
     }
    }else{
     continue;
    }
   }
   sql.append(" 1=1");
   return sql.toString();
}
private synchronized Object getMethodValue(Method method) throws IllegalArgumentException,
   IllegalAccessException, InvocationTargetException{
   Object obj = method.invoke(instance, null);
//   if(obj == null) return null;
   return obj;
}
private Field[] getFields() throws ClassNotFoundException{
   if(cls == null){
    throw new ClassNotFoundException("类没有找到");
   }
   return cls.getDeclaredFields();
}

private Method[] getMethods() throws ClassNotFoundException{
   if(cls == null){
    throw new ClassNotFoundException("类没有找到");
   }
   return cls.getDeclaredMethods();
}
private HashMap<String,ORM> getFiledsName(){
   HashMap<String,ORM> fieldsHashMap = new HashMap<String,ORM>();
   try {
    ORM orm = null;
    Field[] fields = getFields();
    for (Field field : fields) {
     orm = new ORM();
     orm.setName(field.getName());
     orm.setType(field.getType().getName());
     fieldsHashMap.put(field.getName().toUpperCase(),orm);
    }
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return fieldsHashMap;
}
private HashMap<String,Method> getMethodsName(){
   HashMap<String,Method> methodsHashSet = new HashMap<String,Method>();
   try {
    Method[] methods = getMethods();
    for(Method method : methods){
     if(method.getName().startsWith("get"))
      methodsHashSet.put(method.getName().substring(3).toUpperCase(),method);
    }
   
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return methodsHashSet;
}

}
4、测试类:

package lixx.reflect;

import java.lang.reflect.InvocationTargetException;

public class Test {

public static void main(String[] args) {
   Task task = new Task();
   task.setName("lixx");
   task.setPass("lixx1");
  
   System.out.println(task.getTest());
   OperateORM operate = new OperateORM(task,"lixx.reflect.Task");
   try {
    String sql = operate.generateSQL();
    System.out.println(sql+"");
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}
}

原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100565.html