com.mybatiseasy.core.utils.EntityMapUtil Maven / Gradle / Ivy
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy