All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mybatiseasy.core.utils.EntityMapUtil Maven / Gradle / Ivy

There is a newer version: 0.8.10
Show newest version
package com.mybatiseasy.core.utils;


import com.mybatiseasy.core.session.EntityField;
import com.mybatiseasy.core.session.Entity;
import com.mybatiseasy.core.session.EntityKids;
import org.apache.ibatis.reflection.MetaObject;

import java.util.*;


public class EntityMapUtil { 
     
    /**
     * map转对象
     * @param map Map
     * @param entityClass 对象类
     * @return 对象
     * @param  对象类型
     * @throws Exception ex
     */
    public static  T mapToEntity(Map map, Class entityClass) throws Exception {

        T object = entityClass.getDeclaredConstructor().newInstance();
        MetaObject metaObject = MetaObjectUtil.forObject(object);
        Entity entity = EntityKids.getEntityMap(entityClass.getName());
        assert entity != null;
        for (EntityField field : entity.getEntityFieldMapList()
        ) {
            // 数据库字段
            String columnName = SqlUtil.removeBackquote(field.getColumn());
            // entity字段
            String fieldName = field.getName();
            Object value = map.get(columnName);

            if (value != null && metaObject.hasGetter(fieldName)) {
                Object convertedValue = ConversionUtil.convertValue(value, field.getJavaType());
                metaObject.setValue(fieldName, convertedValue);
            }
        }

        return object;

    }

    public static  List mapToEntityList(List> mapList, Class entityClass) throws Exception {
        List objList = new ArrayList<>();
        for (Map item: mapList
        ) {
            objList.add(mapToEntity(item, entityClass));
        }
        return objList;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy