import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* 操作DAO
*
* @author STILL
*
*/
@Repository
public class BaseOperateDao {
@Autowired
JdbcTemplate jt;
/**
* 按sql更新
*
* @param sql
* @param args
* @return
*/
public int updateBysql(String sql, Object... args) {
return jt.update(sql, args);
}
/**
* 按对象新增
*
* @param tableName 表名
* @param obj 对象
* @return
*/
public int saveObj(String tableName, Object obj) {
try {
StringBuilder sql = new StringBuilder(" insert into " + tableName + " (");
Map<String, Object> map = this.getProperty(obj);
Set<String> set = map.keySet();
for (String key : set) {
sql.append(key + ",");
}
sql.replace(sql.length() - 1, sql.length(), "");
sql.append(") values (");
Object object = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (String key : set) {
object = map.get(key);
// 不同类型,sql差异
if (object != null) {
if (object instanceof String) { // String
sql.append("'" + object + "',");
} else if (object instanceof Date) { // Date
sql.append("'" + format.format(object) + "',");
}
// ... 待补充
} else { // 其他
sql.append(object + ",");
}
}
sql.replace(sql.length() - 1, sql.length(), "");
sql.append(" ) ");
return jt.update(sql.toString());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 按对象更新
*
* @param tableName
* @param obj
* @return
*/
public int updateObj(String tableName, Object obj) {
try {
StringBuilder sql = new StringBuilder(" update " + tableName + " set");
Map<String, Object> map = this.getProperty(obj);
Object object = null;
int oId = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Set<String> set = map.keySet();
for (String key : set) {
object = map.get(key);
// 我们假定目前dao只支持带有id为int类型唯一约束的对象修改
if ("id".equals(key)) {
oId = (int) object;
} else {
sql.append(" " + key + "=");
// 不同类型,sql差异
if (object != null) {
if (object instanceof String) { // String
sql.append("'" + object + "'");
} else if (object instanceof Date) { // Date
sql.append("'" + format.format(object) + "'");
}
// ... 待补充
} else { // 其他
sql.append(object);
}
sql.append(" and");
}
}
// 删除 sql拼接时冗余的 and
sql.replace(sql.length() - 3, sql.length(), "");
sql.append("where id=").append(oId);
return jt.update(sql.toString());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 返回一个对象的属性和属性值
*/
@SuppressWarnings("rawtypes")
public Map<String, Object> getProperty(Object object) {
Map<String, Object> map = new HashMap<String, Object>();
try {
Class clazz = object.getClass();
// 获得对象属性
Field[] fields = clazz.getDeclaredFields();
String name;
for (Field field : fields) {
name = field.getName();
Object value = invokeMethod(object, name);
map.put(name, value);
}
} catch (Exception e) {
map = null;
e.printStackTrace();
}
return map;
}
/**
* 获得对象属性的值
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object invokeMethod(Object object, String methodName) throws Exception {
Class clazz = object.getClass();
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = null;
try {
method = clazz.getMethod("get" + methodName);
} catch (Exception e) {
e.printStackTrace();
}
Object invoke = method.invoke(object);
return invoke;
}
}