package com.mandi.common;
|
|
import java.lang.reflect.Field;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
public class SQLautogenerateUtils {
|
|
public static String getinsertSql(Class<?> c){
|
StringBuffer t=new StringBuffer();
|
StringBuffer t1=new StringBuffer();
|
StringBuffer t2=new StringBuffer();
|
t.append("insert into "+StringUtils.lowerCase(c.getSimpleName()));
|
// 获取类中的全部定义字段
|
Field[] fields = c.getDeclaredFields( );
|
for (Field field : fields) {
|
field.setAccessible(true);
|
String fieldName=field.getName();
|
if(!"serialVersionUID".equals(fieldName)){
|
//第一次放入
|
if(StringUtils.isEmpty(t1.toString())){
|
t1.append("("+fieldName);
|
t2.append("(#{"+fieldName+"}");
|
}else{
|
t1.append(","+fieldName);
|
t2.append(",#{"+fieldName+"}");
|
}
|
}
|
}
|
return t.append(t1).append(") values ").append(t2).append(")").toString();
|
}
|
|
public static String getupdateSql(Class<?> c){
|
StringBuffer t=new StringBuffer();
|
t.append("update "+StringUtils.lowerCase(c.getSimpleName())+" set ");
|
// 获取类中的全部定义字段
|
Field[] fields = c.getDeclaredFields( );
|
boolean first=true;
|
for (Field field : fields) {
|
field.setAccessible(true);
|
String fieldName=field.getName();
|
if(!"serialVersionUID".equals(fieldName)){
|
if(first){
|
t.append(fieldName+"=").append("#{"+fieldName+"}");
|
first=false;
|
}else{
|
t.append(","+fieldName+"=").append("#{"+fieldName+"}");
|
}
|
}
|
}
|
return t.append(" where ").toString();
|
}
|
|
|
}
|