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

com.base4j.mvc.base.entity.EntityUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.base4j.mvc.base.entity;

import com.base4j.mybatis.tool.NullUtil;

import javax.persistence.Id;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by [huangjun] on 2017-08-16.
 */
public class EntityUtil {

    public static final Map idFieldCache = new HashMap<>();

    public static void setPreparedValue(Object record, String targetField, Object value) {
        PropertyDescriptor idDes;
        try {
            idDes = new PropertyDescriptor(targetField, record.getClass());
        } catch (IntrospectionException e) {
            return;
        }
        Method readMethod = idDes.getReadMethod();
        Method writeMethod = idDes.getWriteMethod();
        try {
            Object invoke = readMethod.invoke(record);
            if (NullUtil.isNull(invoke)) {   //有值时不自动赋值
                writeMethod.invoke(record, value);
            }
        } catch (Exception e) {
            throw new RuntimeException("向对象:" + record.getClass().getName() + "的字段:" + targetField + "设置值时出错。" + e);
        }
    }

    public static Long getIdValule(Object record) {
        try {
            PropertyDescriptor idDes = new PropertyDescriptor("id", record.getClass());
            Method readMethod = idDes.getReadMethod();
            Object idValud = readMethod.invoke(record);
            return (Long) idValud;
        } catch (Exception e) {
            throw new RuntimeException("使用getId()方法获取主键值时出错,请确认类:" + record.getClass().getName()
                    + "中有使用getId方法返回主键值",e);
        }
    }

    /**
     * 取ID值
     *
     * @param recordClass
     * @return
     */
    private static String getIdFieldName(Class recordClass, String className) {
        if (idFieldCache.containsKey(className)) {
            return idFieldCache.get(className);
        }
        String idFieldName = null;
        Field[] declaredFields = recordClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(Id.class)) {
                idFieldName = declaredField.getName();
                idFieldCache.put(className, idFieldName);
                return idFieldName;
            }
        }
        if (idFieldName == null) {
            Class superclass = recordClass.getSuperclass();
            if (!superclass.getName().equals("java.lang.Object")) {
                return getIdFieldName(superclass, className);
            }
        }
        return idFieldName;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy