Java反射 - 简单的给Bean赋值和取值、利用注解标注Bean属性字段根据顺序赋值

由于项目的实际需要,所以利用java反射原理写了一个简单给bean赋值和取值通用的类,项目中用到了读取excel、然后存到数据库表里面。但是根据excel的列序号读取出来然后在一个一个对应实体里面的字段、特别是一个excel有一百多个字段后期用户在中间添加删除字段真的把人搞晕了。然后我就想了一下能不能用自定义注解给实体的每个属性注解上序号然后读取excel里面的数据之后根据实体属性注解序号与excel的列序号一一对应、然后在根据Java反射直接给Bean实体赋值。如果用户改了excel的列我们只需要修改实体的index列号就好了,不需要我们再去修改代码的对应关系了。记录一下给需要的兄弟提供一个参考例子。

1、新建一个自定义注解类

package cn.com.xxx.config;


import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 实体model属性字段的位置顺序 ,从0开始
 */
@Documented
@Retention(RUNTIME)
@Target(FIELD)
public @interface BeanFieldIndex {
    public int index();
}

2、新建一个实体类我没映射表简单测试的进行注解

package cn.com.xxx.domain;

import cn.com.xxx.config.BeanFieldIndex;

public class TestUser {

    @BeanFieldIndex(index = 0)
    private String userName;

    @BeanFieldIndex(index = 1)
    private String password;

    @BeanFieldIndex(index = 2)
    private String address;


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

3、Java反射处理类工具

package cn.com.xxx.config;

import java.lang.reflect.Field;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class BeanRefUtils {


    /**
     * 通过{@BeanFieldIndex}来进行排序,调用实体的get方法取值
     * @param beanObj	实体
     * @return  returnList		returnList返回的数据
     * @throws Exception
     */
    public static List<Object> getProperty(Object beanObj) throws Exception{
        Class<? extends Object> clazz = beanObj.getClass();
        Field[] fields = clazz.getDeclaredFields();//获得属性
        //记录BeanFieldIndex的长度,必须从0开始
        int index = 0;
        ArrayList<Object> returnList = new ArrayList<Object>();

            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                BeanFieldIndex beanFieldIndex = field.getAnnotation(BeanFieldIndex.class);
                if(beanFieldIndex != null && index == beanFieldIndex.index()){
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                    Method getMethod = pd.getReadMethod();
                    if (getMethod != null) {
                        String fieldType = field.getType().getSimpleName();
                        Object value = getMethod.invoke(beanObj);
                        String result = "";
                        if (value != null) {
                            if ("Date".equals(fieldType)) {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                result = sdf.format(value);
                            } else {
                                result = String.valueOf(value);
                            }
                        }
                        returnList.add(result);
                        index++;
                    }
                }
            }
        return returnList;
    }


    /**
     * 简单测试
     * @param beanObj
     * @param value
     * @throws Exception
     */
    public static void setProperty(Object beanObj, Object value) throws Exception{
        Class<? extends Object> clazz = beanObj.getClass();
        Field[] fields = clazz.getDeclaredFields();//获得属性
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            // 此处应该判断beanObj,property不为null
            PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
            Method setMethod = pd.getWriteMethod();
            if (setMethod != null) {
                //这里注意实体类中set方法中的参数类型,如果不是String类型则进行相对应的转换
                setMethod.invoke(beanObj, value);//invoke是执行set方法
            }
        }
    }


    /**
     * 通过{@BeanFieldIndex}来进行排序,调用实体的set方法赋值
     * @param beanObj	实体
     * @param list
     * @throws Exception
     */
    public static void setProperty(Object beanObj, List<String> list) throws Exception{
        Class<? extends Object> clazz = beanObj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
            BeanFieldIndex radiusIndex = field.getAnnotation(BeanFieldIndex.class);
            if(radiusIndex != null){
                int index = radiusIndex.index();
                String value = list.get(index);
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method setMethod = pd.getWriteMethod();
                if(setMethod != null){
                    if (null != value && !"".equals(value)) {
                        String fieldType = field.getType().getSimpleName();
                        if ("String".equals(fieldType)) {
                            setMethod.invoke(beanObj, value);
                        } else if ("Date".equals(fieldType)) {
                            Date temp = parseDate(value);
                            setMethod.invoke(beanObj, temp);
                        } else if ("Integer".equals(fieldType)
                                || "int".equals(fieldType)) {
                            Integer intval = Integer.parseInt(value);
                            setMethod.invoke(beanObj, intval);
                        } else if ("Long".equalsIgnoreCase(fieldType)) {
                            Long temp = Long.parseLong(value);
                            setMethod.invoke(beanObj, temp);
                        } else if ("Double".equalsIgnoreCase(fieldType)) {
                            Double temp = Double.parseDouble(value);
                            setMethod.invoke(beanObj, temp);
                        } else if ("Boolean".equalsIgnoreCase(fieldType)) {
                            Boolean temp = parseBoolean(value);
                            setMethod.invoke(beanObj, temp);
                        } else {
                            System.out.println("不支持的类型" + fieldType);
                        }
                    }
                }
            }
        }
    }




    /**
     * 将String转化为boolean
     */
    private static boolean parseBoolean(String value) {
        if("1".equals(value) || "true".equalsIgnoreCase(value)){
            return true;
        }
        return false;
    }

    /**
     * 将String转化为Date
     */
    private static Date parseDate(String value) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(value);
        } catch (Exception e) {
        }
        return date;
    }




}

4、在项目中直接调用这个方法进行格式化读取的excel数据直接格式化为实体了。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值