摘要:reflector組新增與更新sql語法
//第一支主程式=================================================
public static void main(String[] args) {
emp mp=new emp();
mp.setName("tt");
mp.setTotal(new BigDecimal("2"));
mp.setNo(99);
mp.setDate(new Date());
try {
System.out.println(vo.getInsertSQL(mp));
List<String> fields=new ArrayList<String>();
List<String> params=new ArrayList<String>();
fields.add("name");
params.add("11");
fields.add("no");
params.add("2");
System.out.println(vo.getUpdateSQL(mp, fields,params));
System.out.println(fields.size());
for (int i = 0; i < fields.size(); i++) {
System.out.println(i);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//第二支程式====================組sql的程式reflector===============================
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class vo {
public static String getInsertSQL(Object vo) throws Exception {
StringBuffer table = new StringBuffer(vo.getClass().getSimpleName());
StringBuffer keys = new StringBuffer();
StringBuffer vals = new StringBuffer();
for (Field field: vo.getClass().getDeclaredFields()) {
field.setAccessible(true);
if(field.get(vo)!=null) {
keys.append(keys.length() > 0 ? ", " : "");
keys.append("`").append(field.getName()).append("`");
}
if(field.get(vo)!=null) {
vals.append(vals.length() > 0 ? ", " : "");
vals.append("'").append(field.get(vo)).append("'");
}
}
StringBuffer sql = new StringBuffer();
sql.append("INSERT INTO `").append(table).append("` (");
sql.append(keys);
sql.append(") VALUES (");
sql.append(vals);
sql.append(")");
return sql.toString();
}
public static String getUpdateSQL(Object vo,List<String> fields,List<String> params) throws Exception {
StringBuffer table = new StringBuffer(vo.getClass().getSimpleName());
StringBuffer keys = new StringBuffer();
StringBuffer vals = new StringBuffer();
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd");
Date current = new Date();
StringBuffer sql = new StringBuffer();
sql.append("update ").append(table).append(" set ");
sql.append(" ");
for (Field field: vo.getClass().getDeclaredFields()) {
System.out.println("type="+field.getType().getSimpleName());
field.setAccessible(true);
if(field.get(vo)!=null) {
sql.append(keys.length() > 0 ? ", " : "");
keys.append("'have many fields'");
sql.append(" ").append(field.getName()).append(" =");
}
if(field.get(vo)!=null) {
if("class java.lang.String".equals(field.getType())) {
sql.append("'").append(field.get(vo)).append("'");
}else {
sql.append("").append(field.get(vo)).append("");
}
}
}
sql.append(" where 1=1 ");
for(int i=0;i<fields.size();i++) {
sql.append("and "+ fields.get(i) +"='"+ params.get(i) +"' ");
}
return sql.toString();
}
}
//第三支程式===============bean==========================================
import java.math.BigDecimal;
import java.util.Date;
public class emp {
private String name;
private int no;
private BigDecimal total;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public BigDecimal getTotal() {
return total;
}
public void setTotal(BigDecimal total) {
this.total = total;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}