Map中的数据封装到实体或者数据类型转换

前提:

实体字段的设计为小驼峰类型,数据库字段的设计遵循下划线分割设计对应字段设计的小驼峰原则。

例如实体中的字段为taskType,则数据库的字段为TASK_TYPE。

工具类代码如下:

package com.company.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  Map数据转实体或者其他
 * @param <T>
 */
public class MapUtil<T> {

    private static final Logger logger = LoggerFactory.getLogger(MapUtil.class);

    private static final SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
    private static final SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat sf3 = new SimpleDateFormat("yyyyMMddHHmmss");

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public T mapToEntity(Class clazz, Map<String, Object> map) {
        T t = null;
        if (map == null)
            return null;
        try {
            t = (T) clazz.newInstance();

            List<String> list = new ArrayList<String>();
            list.add("id");
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                list.add(f.getName());
            }

            Set<String> set = map.keySet();
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                try {
                    String name = it.next();
                    if (!list.contains(name)) {
                        continue;
                    }
                    if (map.get(name) == null) {
                        continue;
                    }
                    PropertyDescriptor pd = new PropertyDescriptor(name, clazz);
                    Method method = pd.getWriteMethod();

                    if (pd.getPropertyType() == String.class) {
                        method.invoke(t, map.get(name).toString());
                    } else if (pd.getPropertyType() == Long.class || pd.getPropertyType() == long.class) {
                        method.invoke(t, Long.valueOf(map.get(name).toString()));
                    } else if (pd.getPropertyType() == Integer.class || pd.getPropertyType() == int.class) {
                        method.invoke(t, Integer.valueOf(map.get(name).toString()));
                    } else if (pd.getPropertyType() == Boolean.class || pd.getPropertyType() == boolean.class) {
                        method.invoke(t, Boolean.valueOf(map.get(name).toString()));
                    } else if (pd.getPropertyType() == BigDecimal.class ) {
                        method.invoke(t, BigDecimal.valueOf(Double.valueOf(map.get(name).toString())));
                    } else if (pd.getPropertyType() == Double.class || pd.getPropertyType() == double.class) {
                        method.invoke(t, Double.valueOf(map.get(name).toString()));
                    } else if (pd.getPropertyType() == Float.class || pd.getPropertyType() == float.class) {
                        method.invoke(t, Float.valueOf(map.get(name).toString()));
                    } else if (pd.getPropertyType() == Byte.class || pd.getPropertyType() == byte.class) {
                        method.invoke(t, Byte
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若依-咬一口甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值