对Bean进行操作以用于把入参对象解析成Map参数传递到Mybatis的工具类

32 篇文章 0 订阅

工具类如下:

package com.cdkj.frame.core.utils;


import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;


import com.cdkj.frame.core.refrect.DeepParameter;




/**
 * 
 * @ClassName: BeanUtils
 * @Description: 对Bean进行操作的相关工具方法
 *
 */
public class BeanUtils {

/**
* 将Bean对象转换成Map对象,将忽略掉值为null或size=0的属性
* 没有WriteMethod的属性不进行读取,尤其是类中有专门用于显示的get方法
* 当查询条件和查询结果中有同样字段,显示的字段中有再有数据库访问的时候,PageHelper,拦截就不正确
* 如在矫正档案的更多搜索中有查询条件状态,显示列表中也有状态
* 用com.cdkj.frame.core.utils.BeanUtils.toMap(Object)就拦截出错
* 不适合用于显示类
* @param bean
* @return The set of properties for the bean
* @see  org.apache.commons.beanutils.PropertyUtils.describe
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static Map<String, Object> describe(Object bean)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {


if (bean == null) {
throw new IllegalArgumentException("No bean specified");
}
Map<String, Object> description = new HashMap<String, Object>();
if (bean instanceof DynaBean) {
DynaProperty[] descriptors = ((DynaBean) bean).getDynaClass().getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
Object value = PropertyUtils.getProperty(bean, name);
if(value != null){
description.put(name, PropertyUtils.getProperty(bean, name));
}
}
} else {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (descriptors[i].getReadMethod() != null && descriptors[i].getWriteMethod() != null) {
Object value = PropertyUtils.getProperty(bean, name);
if(value != null){
if(value instanceof String){
if(StringUtils.isNotEmpty((String)value)){
description.put(name, value);
}
}else{
//需要深度转换的类
DeepParameter deep = value.getClass().getAnnotation(DeepParameter.class);
if(deep != null){
description.putAll(describe(value));
}else{
description.put(name, value);
}
}
}
}
}
}
return (description);


}


/**
* 将Bean对象转换成Map对象,将忽略掉值为null或size=0的属性

* @param obj
*            对象
* @return 若给定对象为null则返回size=0的map对象
*/
public static Map<String, Object> toMap(Object obj) {
Map<String, Object> map = new HashMap<String, Object>();
if (obj == null) {
return map;
}
BeanMap beanMap = new BeanMap(obj);
Iterator<String> it = beanMap.keyIterator();
while (it.hasNext()) {
String name = it.next();
Object value = beanMap.get(name);
// 转换时会将类名也转换成属性,此处去掉
if (value != null && !name.equals("class")) {
map.put(name, value);
}
}
return map;
}


/**
* 该方法将给定的所有对象参数列表转换合并生成一个Map,对于同名属性,依次由后面替换前面的对象属性

* @param objs
*            对象列表
* @return 对于值为null的对象将忽略掉
*/
public static Map<String, Object> toMap(Object... objs) {
Map<String, Object> map = new HashMap<String, Object>();
for (Object object : objs) {
if (object != null) {
map.putAll(toMap(object));
}
}
return map;
}


/**
* 获取接口的泛型类型,如果不存在则返回null

* @param clazz
* @return
*/
public static Class<?> getGenericClass(Class<?> clazz) {
Type t = clazz.getGenericSuperclass();
if (t instanceof ParameterizedType) {
Type[] p = ((ParameterizedType) t).getActualTypeArguments();
return ((Class<?>) p[0]);
}
return null;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值