当用webwork或struts2时,提交过来的表单只对该实体类的部分属性进行了赋值(当然是通过get和set方法),所以有一部分属性是null的。可以通过Java反射获取不为空的值进行不为空的更新。
//将不为空的值放入Map中
public static Map describe(Object object) {
Map map = new HashMap();
Class c = object.getClass();
Field[] fields = c.getDeclaredFields();
try {
for(Field f : fields) {
f.setAccessible(true);
Object o = f.get(object);
if(o != null) {
map.put(f.getName(), o);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return map;
}
//根据实体对象生成更新语句
public static String createHql(String className, String condition, Map map) {
List<String> list = new ArrayList<String>();
map.remove("class");
StringBuffer sb = new StringBuffer();
sb.append("update ");
sb.append(className);
sb.append(" set");
for (Object key : map.keySet()) {
if (StringUtil.isNullOrSpace(map.get(key))) {
list.add(key.toString());
} else {
sb.append(" ,");
sb.append(key);
sb.append("=:");
sb.append(key);
}
}
for (String key : list) {
map.remove(key);
}
if (condition != null && !"".equals(condition)) {
sb.append(" ");
sb.append(condition);
}
return sb.toString().replaceFirst(",", "");
}
//设置参数
public static void populate(Query query, Map map) {
if (map == null) {
return;
}
for (Object key : map.keySet()) {
if (map.get(key) != null && !"".equals(map.get(key))) {
if(map.get(key) instanceof Timestamp) {
query.setTimestamp(key.toString(), (Timestamp)map.get(key));
} else if(map.get(key) instanceof Double) {
query.setDouble(key.toString(), (Double)map.get(key));
} else if(map.get(key) instanceof List){
query.setParameterList(key.toString(), (List)map.get(key));
} else {
query.setParameter(key.toString(), map.get(key));
}
}
}
}
//DAO层更新
//condition为where子句,agrMap为where字句的参数
public void update(Object object ,String condition, Map agrMap) {
Map map = null;
map = BeanUtils.describe(object);
String hql = null;
//生成sql
hql = CommonQuery.createHql(object.getClass().getSimpleName(), condition, map);
//注入查询条件参数
map.putAll(agrMap);
Query query = getSession().createQuery(hql);
CommonQuery.populate(query, map);
query.executeUpdate();
}
//将不为空的值放入Map中
public static Map describe(Object object) {
Map map = new HashMap();
Class c = object.getClass();
Field[] fields = c.getDeclaredFields();
try {
for(Field f : fields) {
f.setAccessible(true);
Object o = f.get(object);
if(o != null) {
map.put(f.getName(), o);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return map;
}
//根据实体对象生成更新语句
public static String createHql(String className, String condition, Map map) {
List<String> list = new ArrayList<String>();
map.remove("class");
StringBuffer sb = new StringBuffer();
sb.append("update ");
sb.append(className);
sb.append(" set");
for (Object key : map.keySet()) {
if (StringUtil.isNullOrSpace(map.get(key))) {
list.add(key.toString());
} else {
sb.append(" ,");
sb.append(key);
sb.append("=:");
sb.append(key);
}
}
for (String key : list) {
map.remove(key);
}
if (condition != null && !"".equals(condition)) {
sb.append(" ");
sb.append(condition);
}
return sb.toString().replaceFirst(",", "");
}
//设置参数
public static void populate(Query query, Map map) {
if (map == null) {
return;
}
for (Object key : map.keySet()) {
if (map.get(key) != null && !"".equals(map.get(key))) {
if(map.get(key) instanceof Timestamp) {
query.setTimestamp(key.toString(), (Timestamp)map.get(key));
} else if(map.get(key) instanceof Double) {
query.setDouble(key.toString(), (Double)map.get(key));
} else if(map.get(key) instanceof List){
query.setParameterList(key.toString(), (List)map.get(key));
} else {
query.setParameter(key.toString(), map.get(key));
}
}
}
}
//DAO层更新
//condition为where子句,agrMap为where字句的参数
public void update(Object object ,String condition, Map agrMap) {
Map map = null;
map = BeanUtils.describe(object);
String hql = null;
//生成sql
hql = CommonQuery.createHql(object.getClass().getSimpleName(), condition, map);
//注入查询条件参数
map.putAll(agrMap);
Query query = getSession().createQuery(hql);
CommonQuery.populate(query, map);
query.executeUpdate();
}