数据库字段格式处理

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.newcross.vsg.platform.dto.annotation.DtoField;
import com.newcross.vsg.platform.model.BaseModel;
import com.newcross.vsg.platform.model.DeviceGroup;

public class ReflectUtil {
 @SuppressWarnings("unchecked")
 public static void setFieldValue(Object target, String fname, Class ftype,
   Object fvalue) {
  if (target == null
    || fname == null
    || "".equals(fname)
    || (fvalue != null && !ftype
      .isAssignableFrom(fvalue.getClass()))) {
   return;
  }
  Class clazz = target.getClass();
  try {
   Method method = clazz.getDeclaredMethod(
     "get" + Character.toUpperCase(fname.charAt(0))
       + fname.substring(1), ftype);
   if (!Modifier.isPublic(method.getModifiers())) {
    method.setAccessible(true);
   }
   method.invoke(target, fvalue);
  } catch (Exception me) {
   try {
    Field field = clazz.getDeclaredField(fname);
    if (!Modifier.isPublic(field.getModifiers())) {
     field.setAccessible(true);
    }
    field.set(target, fvalue);
   } catch (Exception fe) {
    Logger.error(fe.getMessage() + fe.getMessage(), fe);
   }
  }
 }

 public static Field[] getAllFields(Class klass) {
  List<Field> fields = new ArrayList<Field>();
  fields.addAll(Arrays.asList(klass.getDeclaredFields()));
  if (klass.getSuperclass() != null) {
   fields.addAll(Arrays.asList(getAllFields(klass.getSuperclass())));
  }
  return fields.toArray(new Field[] {});
 }

 public static Map<String, Field> getAllFieldsToMap(Class clazz) {
  Field[] fields = getAllFields(clazz);
  Map<String, Field> result = new HashMap<String, Field>();
  if (fields != null) {
   for (Field field : fields) {
    result.put(field.getName(), field);
   }
  }
  return result;
 }

 public static List<Map> buildMapFromObject(Object obj,String fun_cde,String userId) {
  List<Map> result = new ArrayList<Map>();
  if(obj != null) {
   Class clazz = obj.getClass();
   Field[] fields = getAllFields(clazz);
   try {
    for(Field field : fields){
     DtoField dtoFieldAnnt = field.getAnnotation(DtoField.class);
     if (dtoFieldAnnt != null) {
      Map<String, String> row = new HashMap<String, String>();
      String key = dtoFieldAnnt.field();
      field.setAccessible(true);
      Object value = field.get(obj);
      if(value==null){
       value="";
      }
      if(value instanceof Date){
       SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       value = dateFormatter.format(value);
      };
      row.put("mod_cde","VMS");
      row.put("fun_cde",fun_cde);
      row.put("userId",userId);
      row.put("key", key);
      row.put("val", value.toString());
      result.add(row);
     }
    }
   }catch (Exception e) {
    e.printStackTrace();
   }
  }
  return result;
 }
 
 public static Object buildObjectFromMap(List<Map> list, Class clazz) {
  Object result = null;
  Field[] fields = getAllFields(clazz);
  try {
   result = clazz.newInstance();
   for(Field field : fields) {
    DtoField dtoFieldAnnt = field.getAnnotation(DtoField.class);
    Object obj = field.getType();
    String key = dtoFieldAnnt.field();
    Map map  = getRelativeField(list, key);
    if(map !=null){
     Object value = map.get("PARAM_VAL");
     if(obj.equals(Boolean.class)){
      value = Boolean.parseBoolean(value.toString());
     }
     if(obj.equals(Date.class)){
      //value = DateFormat.getDateInstance().parse(value.toString());
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      value = format.parse(value.toString());
     }
     if(obj.equals(BigDecimal.class)){
      BigDecimal bigDecimal = new BigDecimal(value.toString());
      value = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP);
     }
     if(obj.equals(Integer.class)){
      value = Integer.parseInt(value.toString());
     }
     
     field.setAccessible(true);
     field.set(result, value);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
 }
 
 public static void main(String[] args) throws Exception {
  DeviceGroup deviceGroup = new DeviceGroup();
  deviceGroup.setVersionId(1L);
  Field[] fields = getAllFields(DeviceGroup.class);
  int size = fields.length;
  for (int i = 0; i < size; i++) {
   System.out.println(fields[i].getName());
  }
  Class clazz = BaseModel.class;
  Field versionField = clazz.getDeclaredField("versionId");
  // Field versionField = clazz.getField("versionId");
  versionField.setAccessible(true);
  Object versionId = versionField.get(deviceGroup);
  System.out.println(versionId);
 }

 public static Map getRelativeField(List<Map> list, String fieldName) {
  for(Map map : list) {
   if(map.get("PARAM_KEY").toString().equalsIgnoreCase(fieldName)) {
    return map;
   }
  }
  return null;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值