BeanUtil

来源:互联网 发布:linux route del 编辑:程序博客网 时间:2024/05/18 01:26

这边分享一个BeanUtil,一般我们现实在前端的对象称之为VO,而VO和你从数据库取出来的Bean对象是有区别的,因此我们经常会从对象中选择其中某几个字段充当VO,那么你就可以使用我这个工具类,直接可以将List的实体对象转换成List的Vo,减少了很多麻烦。其中一个注意点就是需要引入commons包

import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.beanutils.PropertyUtils;/** * 转换vo的公共方法 * @author yiyong_wu */public class BeanUtil {public static Method makeGetMethod(Class classType, String fieldName)throws Exception {String firstLetter = fieldName.substring(0, 1).toUpperCase();String getMethodName = "get" + firstLetter + fieldName.substring(1);Method getMethod = classType.getMethod(getMethodName, new Class[] {});return getMethod;}public static Method makeSetMethod(Class classType, String fieldName)throws Exception {Field field = classType.getDeclaredField(fieldName);String firstLetter = fieldName.substring(0, 1).toUpperCase();String setMethodName = "set" + firstLetter + fieldName.substring(1);Method setMethod = classType.getMethod(setMethodName,new Class[] { field.getType() });return setMethod;}/** * 当需要对返回的vo进行处理时调用 * List<MessageVO> voList = (List<MessageVO>) BeanUtil.poToVo(MessageVO.class, result,null); * @param classType vo类名 * @param po        entity类 * @param fieldMap  po到vo名称不相同的字段映射 * @return vo实例 */public static Object poToVo(Class<?> classType, Object po,Map<String, String> fieldMap) {Object entity = null;try {entity = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});Field fields[] = classType.getDeclaredFields();String className = classType.getSimpleName();for (int i = 0, length = fields.length; i < length; i++) {try {String fieldName = fields[i].getName();Class fieldType = fields[i].getType();Method setMethod = makeSetMethod(classType, fieldName);Method getMethod = null;if (setMethod == null) {continue;}if (fieldMap == null || !fieldMap.containsKey(fieldName)) {getMethod = makeGetMethod(po.getClass(), fieldName);} else {// 如果在map中,则说明po和vo字段名称不一致getMethod = makeGetMethod(po.getClass(),fieldMap.get(fieldName));}Object value = getMethod.invoke(po, new Object[] {});setMethod.invoke(entity, value);} catch (Exception e) {continue;}}} catch (Exception e) {return null;}return entity;}public static List poToVo(Class<?> classType, List poList,Map<String, String> fieldMap) {List list = new ArrayList();Object vo = null;try {for (int i = 0; i < poList.size(); i++) {vo = poToVo(classType, poList.get(i), fieldMap);list.add(vo);}} catch (Exception e) {return null;}return list;}/** * 复制po的对象属性到vo *  * @param vo *            .classType * @param po * @return vo */public static Object poToVo(Class<?> classType, Object po) {Object entity = null;try {entity = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});PropertyUtils.copyProperties(entity,po);} catch (Exception e) {return null;}return entity;}/** * 直接在控制层返回voList时调用 * 比如:jsonResult.success(poToVo(classType, poList)); * @param vo *            .classType * @param poList * @return voList */public static List poToVo(Class<?> classType, List poList) {List list = new ArrayList();Object entity = null;try {for (int i = 0; i < poList.size(); i++) {entity = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});PropertyUtils.copyProperties(entity,poList.get(i));list.add(entity);}} catch (Exception e) {return null;}return list;}}


1 0
原创粉丝点击