mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-11-22 18:36:52 +08:00
adding coupon script
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package net.lab1024.smartadmin.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
public class MapRemoveNullUtil {
|
||||
@@ -99,4 +101,42 @@ public class MapRemoveNullUtil {
|
||||
removeNullEntry(map);
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个类查询方式加入map(属性值为int型时,0时不加入,
|
||||
* 属性值为String型或Long时为null和“”不加入)
|
||||
*注:需要转换的必须是对象,即有属性
|
||||
*/
|
||||
public static Map<String, String> setConditionMap(Object obj){
|
||||
Map<String, String> map = new HashMap<>();
|
||||
if(obj==null){
|
||||
return null;
|
||||
}
|
||||
Field[] fields = obj.getClass().getDeclaredFields();//获取类的各个属性值
|
||||
for(Field field : fields){
|
||||
String fieldName = field.getName();//获取类的属性名称
|
||||
if(getValueByFieldName(fieldName,obj)!=null)//获取类的属性名称对应的值
|
||||
{
|
||||
map.put(fieldName, getValueByFieldName(fieldName,obj));
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
* 根据属性名获取该类此属性的值
|
||||
* @param fieldName
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
private static String getValueByFieldName(String fieldName, Object object){
|
||||
String firstLetter=fieldName.substring(0,1).toUpperCase();
|
||||
String getter = "get"+firstLetter+fieldName.substring(1);
|
||||
try {
|
||||
Method method = object.getClass().getMethod(getter, new Class[]{});
|
||||
Object value = method.invoke(object, new Object[] {});
|
||||
return value.toString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user