利用反射生成JDBC增删改查Sql

package intellif.mining.util;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


import com.google.common.base.CaseFormat;


import intellif.mining.hover.entity.HoverEvent;


public class SqlFactory {


private static void load(Object obj, Map<String, Object> paramMap)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
/**
* 获得属性名称和值的集合

*/
Class c = obj.getClass();
Method[] methods = c.getMethods();


for (Method m : methods) {


String mName = m.getName();
if (mName.startsWith("get") && !mName.startsWith("getClass")) {
String fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,
mName.substring(3, 4).toLowerCase() + mName.substring(4, mName.length()));
Object value = m.invoke(obj, null);
if (value != null) {


if (value instanceof String) {
paramMap.put(fieldName, "\"" + value + "\"");
} else if (value instanceof Date) {
paramMap.put(fieldName, "\"" + DateUtil.getformatDate((Date) value) + "\"");
} else if (value instanceof Long) {
if ((long) value != 0) {
paramMap.put(fieldName, value);
}
} else {
paramMap.put(fieldName, value);
}
}
}
}
}


/**
* @param type
* @param tableName
* @param obj
* @return 对象为空则返回为null
*/
public static String createUpdateSql(String type, String tableName, Object obj) {
Map<String, Object> paramMap = new HashMap<String, Object>();
try {
load(obj, paramMap);
} catch (IllegalArgumentException 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();
}
StringBuffer strb = new StringBuffer();
Set<String> set = paramMap.keySet();
Object[] keys = set.toArray();
int len = keys.length;
if ("insert".compareToIgnoreCase(type) == 0) {
strb.append("insert into " + tableName + "(");
for (int i = 0; i < len; i++) {
if (i < len - 1) {
strb.append(keys[i]);
strb.append(",");
} else {
strb.append(keys[i]);
strb.append(") values(");
}
}
for (int i = 0; i < len; i++) {
if (i < len - 1) {
strb.append(paramMap.get(keys[i]) + ",");
} else {
strb.append(paramMap.get(keys[i]) + ")");
}
}
}
if ("delete".compareToIgnoreCase(type) == 0) {
strb.append("delete from " + tableName);
for (int i = 0; i < len; i++) {
if (((String) keys[i]).contains("id") || ((String) keys[i]).contains("Id")) {
strb.append(" where " + keys[i] + "=" + paramMap.get(keys[i]));
}
}
}
if ("update".compareToIgnoreCase(type) == 0) {
strb.append("update " + tableName + " ");
for (int i = 0; i < len; i++) {
if (i < len - 1) {
strb.append("set" + keys[i] + "=" + paramMap.get(keys[i]));
strb.append(",");
} else {
strb.append("set" + keys[i] + "=" + paramMap.get(keys[i]));


}
}
for (int i = 0; i < len; i++) {
if (((String) keys[i]).contains("id") || ((String) keys[i]).contains("Id")) {
strb.append(" where " + keys[i] + "=" + paramMap.get(keys[i]));
}
}
}
if (paramMap.isEmpty()) {
return null;
}
return strb.toString();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值