我以一个简单的实例去实现,具体实现看service层以及去看util类
entity:
public class WwDept { private Integer wwDeptId; private String wwDeptName; private Integer wwDeptParentid; }
service:
public Map addDept(WwDept dept) { //查询出对象所有的属性 Field[] fields = dept.getClass().getDeclaredFields(); //用于判断所有属性是否为空,如果参数为空则不查询 boolean flag = false; for (Field field : fields) { //不检查 直接取值 field.setAccessible(true); try { if (StringUtils.isNotNull(field.get(dept))) { //不为空 flag = true; //当有任何一个参数不为空的时候则跳出判断直接查询 break; } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } if (flag == true) { return MapUtils.getMap("fail"); } else { if (deptMapper.addDept(dept) > 0) return MapUtils.getMap("success"); return MapUtils.getMap("fail"); } }
util:
public class PropertyUtils { public static boolean isNull(Object[] objs) { if (objs == null || objs.length == 0) return true; return false; } public static boolean isNull(Object obj) { if (obj == null || isNull(obj.toString())){ return true; } return false; } public static boolean isNull(Integer integer) { if (integer == null || integer == 0) return true; return false; } public static boolean isNull(Collection collection) { if (collection == null || collection.size() == 0) return true; return false; } public static boolean isNull(Map map) { if (map == null || map.size() == 0) return true; return false; } public static boolean isNull(String str) { return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase()); } public static boolean isNull(Long longs) { if (longs == null || longs == 0) return true; return false; } public static boolean isNotNull(Long longs) { return !isNull(longs); } public static boolean isNotNull(String str) { return !isNull(str); } public static boolean isNotNull(Collection collection) { return !isNull(collection); } public static boolean isNotNull(Map map) { return !isNull(map); } public static boolean isNotNull(Integer integer) { return !isNull(integer); } public static boolean isNotNull(Object[] objs) { return !isNull(objs); } public static boolean isNotNull(Object obj) { return !isNull(obj); } }