mongo 对象object转Document工具类(通过反射) - 洪学林个人博客

mongo 对象object转Document工具类(通过反射)

public class DocumentUtil {

private static final Logger log = LoggerFactory.getLogger(DocumentUtil.class);

/**
 * 对象转换成document
 * @param obj 对象
 * @param clazz 对象所属类
 * @return document
 */
public static Document buildDocument(Object obj, Class clazz) {
    Document document = new Document();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        Object value = null;
        try {
            if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class) ||
                    field.getType().isAssignableFrom(Map.class)) {
                value = JSON.toJSONString(field.get(obj));
            } else {
                value = field.get(obj);
            }
        } catch (IllegalAccessException e) {
            log.error(e);
        }

        if (value != null) {
            document.append(field.getName(), value);
        }
    }
    return document;
}

/**
 * document转换对象
 * @param document document
 * @param clazz 转换对象类
 * @param <T> 泛型对象
 * @return 对象
 */
public static <T> T convertToObj(Document document, Class<T> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (Exception e) {
        log.error(e);
    }
    for (Field field : fields) {
        try {
            if (field.getType().isAssignableFrom(Integer.class)) {
                field.setAccessible(true);
                field.setInt(t, document.getInteger(field.getName()));
            } else if (field.getType().isAssignableFrom(Float.class)) {
                field.setAccessible(true);
                field.setFloat(t, document.getDouble(field.getName()).floatValue());
            } else if (field.getType().isAssignableFrom(Double.class)) {
                field.setAccessible(true);
                field.setDouble(t, document.getDouble(field.getName()));
            } else if (field.getType().isAssignableFrom(Long.class)) {
                field.setAccessible(true);
                field.setLong(t, document.getLong(field.getName()));
            } else if (field.getType().isAssignableFrom(String.class)) {
                field.setAccessible(true);
                field.set(t, document.getString(field.getName()));
            } else if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseArray(document.getString(field.getName())));
            } else if (field.getType().isAssignableFrom(Map.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName())));
            } else {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName()), field.getType()));
            }
        } catch (Exception e) {
            log.error(e);
        }
    }

    return t;
}

}

添加新评论