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

com.yuyenews.easy.util.entity.EntityUtil Maven / Gradle / Ivy

The newest version!
package com.yuyenews.easy.util.entity;

import java.lang.reflect.Field;
import java.sql.ResultSet;

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

/**
 * 组装实体工具类
 * 
 * @author yuye
 *
 */
public class EntityUtil {

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

	/**
	 * 获取实体对象
	 * 
	 * @param cls lei
	 * @param resultSet shuj
	 * 
	 * @return duix
	 */
	public static Object getEntity(Class cls, ResultSet resultSet) {

		try {

			if (cls.getName().equals(Integer.class.getName())
					|| cls.getName().equals(Long.class.getName())
					|| cls.getName().equals(String.class.getName())
					|| cls.getName().equals(Double.class.getName())) {
				return resultSet.getObject(1);
			} else {

				Object obj = cls.getDeclaredConstructor().newInstance();

				Field[] fields = cls.getDeclaredFields();

				for (Field field : fields) {
					field.setAccessible(true);
					
					Object objValue = resultSet.getObject(field.getName());
					if (objValue != null) {
						field.set(obj, objValue);
					}
				}

				return obj;
			}
		} catch (Exception e) {
			logger.error("组装实体对象报错", e);
		}

		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy