IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 利用反射获取属性值和属性名称,并给属性赋值 -> 正文阅读

[Java知识库]利用反射获取属性值和属性名称,并给属性赋值

创建实体类

package com.wapwag.woss.modules.sys.entity;

import com.wapwag.woss.common.persistence.DataEntity;

public class PumpReportData extends DataEntity<PumpReportData> {

    private String  LvQianYaLi;  //市政滤前压力
    private String  LvHouYaLi;  //市政滤后压力
    private String ChuShuiYaLi1;//分区1_出水压力
    private String ChuShuiYaLi2;//分区2_出水压力
    private String ChuShuiYaLi3;//分区3_出水压力
    private String ChuShuiYaLi4;//分区4_出水压力
    private String ChuShuiYaLi5;//分区5_出水压力
    private String JinShuiLiuLiang;//水箱进水瞬时流量
    private String YeWei1;//水箱1液位
    private String YeWei2;//水箱2液位
    private String ZhuoDu;//浊度
    private String YuLv;//余氯
    private String ZaoYin;//噪音
    private String ZongGuanJinShuiLiuLiang;//总管累计流量
    private Integer  LvQianYaLiException;  //市政滤前压力
    private Integer  LvHouYaLiException;  //市政滤后压力
    private Integer ChuShuiYaLi1Exception;//分区1_出水压力
    private Integer ChuShuiYaLi2Exception;//分区2_出水压力
    private Integer ChuShuiYaLi3Exception;//分区3_出水压力
    private Integer ChuShuiYaLi4Exception;//分区4_出水压力
    private Integer ChuShuiYaLi5Exception;//分区5_出水压力
    private Integer JinShuiLiuLiangException;//水箱进水瞬时流量
    private Integer YeWei1Exception;//水箱1液位
    private Integer YeWei2Exception;//水箱2液位
    private Integer ZhuoDuException;//浊度
    private Integer YuLvException;//余氯
    private Integer ZaoYinException;//噪音
    private Integer ZongGuanJinShuiLiuLiangException;//总管累计流量
    private String dateTime;
    private String value;
    private String pinyin;
    private String tagName;
    private String minvalue;
    private String bigvalue;
    private String idDevice;
    private String thAvgTotalValue;//3小时夜间总管进水平均流量
    private String wholeAvgTotalValue;//24h 总管进水平均流量
    private String tankAvgValue;//水箱进水阀打开时水箱进水平均流量
    private String pumpName;


    public String getLvQianYaLi() {
        return LvQianYaLi;
    }

    public void setLvQianYaLi(String lvQianYaLi) {
        LvQianYaLi = lvQianYaLi;
    }

    public String getLvHouYaLi() {
        return LvHouYaLi;
    }

    public void setLvHouYaLi(String lvHouYaLi) {
        LvHouYaLi = lvHouYaLi;
    }

    public String getChuShuiYaLi1() {
        return ChuShuiYaLi1;
    }

    public void setChuShuiYaLi1(String chuShuiYaLi1) {
        ChuShuiYaLi1 = chuShuiYaLi1;
    }

    public String getChuShuiYaLi2() {
        return ChuShuiYaLi2;
    }

    public void setChuShuiYaLi2(String chuShuiYaLi2) {
        ChuShuiYaLi2 = chuShuiYaLi2;
    }

    public String getChuShuiYaLi3() {
        return ChuShuiYaLi3;
    }

    public void setChuShuiYaLi3(String chuShuiYaLi3) {
        ChuShuiYaLi3 = chuShuiYaLi3;
    }

    public String getChuShuiYaLi4() {
        return ChuShuiYaLi4;
    }

    public void setChuShuiYaLi4(String chuShuiYaLi4) {
        ChuShuiYaLi4 = chuShuiYaLi4;
    }

    public String getChuShuiYaLi5() {
        return ChuShuiYaLi5;
    }

    public void setChuShuiYaLi5(String chuShuiYaLi5) {
        ChuShuiYaLi5 = chuShuiYaLi5;
    }

    public String getJinShuiLiuLiang() {
        return JinShuiLiuLiang;
    }

    public void setJinShuiLiuLiang(String jinShuiLiuLiang) {
        JinShuiLiuLiang = jinShuiLiuLiang;
    }

    public String getYeWei1() {
        return YeWei1;
    }

    public void setYeWei1(String yeWei1) {
        YeWei1 = yeWei1;
    }

    public String getYeWei2() {
        return YeWei2;
    }

    public void setYeWei2(String yeWei2) {
        YeWei2 = yeWei2;
    }

    public String getZhuoDu() {
        return ZhuoDu;
    }

    public void setZhuoDu(String zhuoDu) {
        ZhuoDu = zhuoDu;
    }

    public String getYuLv() {
        return YuLv;
    }

    public void setYuLv(String yuLv) {
        YuLv = yuLv;
    }

    public String getZaoYin() {
        return ZaoYin;
    }

    public void setZaoYin(String zaoYin) {
        ZaoYin = zaoYin;
    }

    public String getZongGuanJinShuiLiuLiang() {
        return ZongGuanJinShuiLiuLiang;
    }

    public void setZongGuanJinShuiLiuLiang(String zongGuanJinShuiLiuLiang) {
        ZongGuanJinShuiLiuLiang = zongGuanJinShuiLiuLiang;
    }

    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    public String getMinvalue() {
        return minvalue;
    }

    public void setMinvalue(String minvalue) {
        this.minvalue = minvalue;
    }

    public String getBigvalue() {
        return bigvalue;
    }

    public void setBigvalue(String bigvalue) {
        this.bigvalue = bigvalue;
    }

    public String getIdDevice() {
        return idDevice;
    }

    public void setIdDevice(String idDevice) {
        this.idDevice = idDevice;
    }

    public String getThAvgTotalValue() {
        return thAvgTotalValue;
    }

    public void setThAvgTotalValue(String thAvgTotalValue) {
        this.thAvgTotalValue = thAvgTotalValue;
    }

    public String getWholeAvgTotalValue() {
        return wholeAvgTotalValue;
    }

    public void setWholeAvgTotalValue(String wholeAvgTotalValue) {
        this.wholeAvgTotalValue = wholeAvgTotalValue;
    }

    public String getTankAvgValue() {
        return tankAvgValue;
    }

    public void setTankAvgValue(String tankAvgValue) {
        this.tankAvgValue = tankAvgValue;
    }

    public String getPumpName() {
        return pumpName;
    }

    public void setPumpName(String pumpName) {
        this.pumpName = pumpName;
    }

    public Integer getLvQianYaLiException() {
        if("".equals(LvQianYaLiException)||LvQianYaLiException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return LvQianYaLiException;
    }

    public void setLvQianYaLiException(Integer lvQianYaLiException) {
        LvQianYaLiException = lvQianYaLiException;
    }

    public Integer getLvHouYaLiException() {
        if("".equals(LvHouYaLiException)||LvHouYaLiException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return LvHouYaLiException;
    }

    public void setLvHouYaLiException(Integer lvHouYaLiException) {
        LvHouYaLiException = lvHouYaLiException;
    }

    public Integer getChuShuiYaLi1Exception() {
        if("".equals(ChuShuiYaLi1Exception)||ChuShuiYaLi1Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ChuShuiYaLi1Exception;
    }

    public void setChuShuiYaLi1Exception(Integer chuShuiYaLi1Exception) {
        ChuShuiYaLi1Exception = chuShuiYaLi1Exception;
    }

    public Integer getChuShuiYaLi2Exception() {
        if("".equals(ChuShuiYaLi2Exception)||ChuShuiYaLi2Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ChuShuiYaLi2Exception;
    }

    public void setChuShuiYaLi2Exception(Integer chuShuiYaLi2Exception) {
        ChuShuiYaLi2Exception = chuShuiYaLi2Exception;
    }

    public Integer getChuShuiYaLi3Exception() {
        if("".equals(ChuShuiYaLi3Exception)||ChuShuiYaLi3Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ChuShuiYaLi3Exception;
    }

    public void setChuShuiYaLi3Exception(Integer chuShuiYaLi3Exception) {
        ChuShuiYaLi3Exception = chuShuiYaLi3Exception;
    }

    public Integer getChuShuiYaLi4Exception() {
        if("".equals(ChuShuiYaLi4Exception)||ChuShuiYaLi4Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ChuShuiYaLi4Exception;
    }

    public void setChuShuiYaLi4Exception(Integer chuShuiYaLi4Exception) {
        ChuShuiYaLi4Exception = chuShuiYaLi4Exception;
    }

    public Integer getChuShuiYaLi5Exception() {
        if("".equals(ChuShuiYaLi5Exception)||ChuShuiYaLi5Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ChuShuiYaLi5Exception;
    }

    public void setChuShuiYaLi5Exception(Integer chuShuiYaLi5Exception) {
        ChuShuiYaLi5Exception = chuShuiYaLi5Exception;
    }

    public Integer getJinShuiLiuLiangException() {
        if("".equals(JinShuiLiuLiangException)||JinShuiLiuLiangException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return JinShuiLiuLiangException;
    }

    public void setJinShuiLiuLiangException(Integer jinShuiLiuLiangException) {
        JinShuiLiuLiangException = jinShuiLiuLiangException;
    }

    public Integer getYeWei1Exception() {
        if("".equals(YeWei1Exception)||YeWei1Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return YeWei1Exception;
    }

    public void setYeWei1Exception(Integer yeWei1Exception) {
        YeWei1Exception = yeWei1Exception;
    }

    public Integer getYeWei2Exception() {
        if("".equals(YeWei2Exception)||YeWei2Exception==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return YeWei2Exception;
    }

    public void setYeWei2Exception(Integer yeWei2Exception) {
        YeWei2Exception = yeWei2Exception;
    }

    public Integer getZhuoDuException() {
        if("".equals(ZhuoDuException)||ZhuoDuException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ZhuoDuException;
    }

    public void setZhuoDuException(Integer zhuoDuException) {
        ZhuoDuException = zhuoDuException;
    }

    public Integer getYuLvException() {
        if("".equals(YuLvException)||YuLvException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return YuLvException;
    }

    public void setYuLvException(Integer yuLvException) {
        YuLvException = yuLvException;
    }

    public Integer getZaoYinException() {
        if("".equals(ZaoYinException)||ZaoYinException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ZaoYinException;
    }

    public void setZaoYinException(Integer zaoYinException) {
        ZaoYinException = zaoYinException;
    }

    public Integer getZongGuanJinShuiLiuLiangException() {
        if("".equals(ZongGuanJinShuiLiuLiangException)||ZongGuanJinShuiLiuLiangException==null){
            return 0;//去除该属性的前后空格并进行非空非null判断
        }
        return ZongGuanJinShuiLiuLiangException;
    }

    public void setZongGuanJinShuiLiuLiangException(Integer zongGuanJinShuiLiuLiangException) {
        ZongGuanJinShuiLiuLiangException = zongGuanJinShuiLiuLiangException;
    }
}

查询数据,对查询的数据做处理

private static final Map<String,String> nameRelation = new HashMap<String,String>(){{
        put("LvQianYaLi","市政滤前压力");
        put("LvHouYaLi","市政滤后压力");
        put("ChuShuiYaLi1","分区1_出水压力");
        put("ChuShuiYaLi2","分区2_出水压力");
        put("ChuShuiYaLi3","分区3_出水压力");
        put("ChuShuiYaLi4","分区4_出水压力");
        put("ChuShuiYaLi5","分区5_出水压力");
        put("JinShuiLiuLiang","水箱进水瞬时流量");
        put("YeWei1","水箱1液位");
        put("YeWei2","水箱2液位");
        put("ZhuoDu","浊度");
        put("YuLv","余氯");
        put("ZaoYin","噪音");
        put("ZongGuanJinShuiLiuLiang","总管累计流量");
    }};

    private static final Map<String,String> reflectRelation = new HashMap<String,String>(){{
        put("LvQianYaLi","LvQianYaLiException");
        put("LvHouYaLi","LvHouYaLiException");
        put("ChuShuiYaLi1","ChuShuiYaLi1Exception");
        put("ChuShuiYaLi2","ChuShuiYaLi2Exception");
        put("ChuShuiYaLi3","ChuShuiYaLi3Exception");
        put("ChuShuiYaLi4","ChuShuiYaLi4Exception");
        put("ChuShuiYaLi5","ChuShuiYaLi5Exception");
        put("JinShuiLiuLiang","JinShuiLiuLiangException");
        put("YeWei1","YeWei1Exception");
        put("YeWei2","YeWei2Exception");
        put("ZhuoDu","ZhuoDuException");
        put("YuLv","YuLvException");
        put("ZaoYin","ZaoYinException");
        put("ZongGuanJinShuiLiuLiang","ZongGuanJinShuiLiuLiangException");
    }};

public List<PumpReportData> queryPumpReportDayData(Integer detailedModel,String startDate,String endDate,String pumpHouseId){
        List<Services> servicesList = reportDao.queryAlarmPoint(pumpHouseId);
        Map<String, Services> collect = servicesList.stream().collect(Collectors.toMap(Services::getName, a -> a, (e1, e2) -> e1));
        List<PumpReportData> pumpReportDataList = reportDao.queryReportDetailData(pumpHouseId, startDate, endDate, detailedModel);
        pumpReportDataList.stream().forEach(data->{
            Services services = null;
            BigDecimal max = new BigDecimal(0.0f);
            BigDecimal min = new BigDecimal(0.0f);
            BigDecimal real = new BigDecimal(0.0f);
            String temp = null;
            for (Field field : data.getClass().getDeclaredFields()) {//遍历实体所有属性
                field.setAccessible(true);//设置私有属性可见
                String name = field.getName();//获取属性名称
                if(null != nameRelation.get(name) && null != (services=collect.get(nameRelation.get(name)))){
                    try {
                        Field declaredField;
                        if(null == field.get(data)){//获取属性值
                            try {
                                declaredField = data.getClass().getDeclaredField(reflectRelation.get(name));
                                declaredField.setAccessible(true);
                                declaredField.set(data,1);
                                continue;
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                        }else{
                            temp = (String) field.get(data);//获取属性值
                        }
                        if(temp.equals("-") || temp.equals("/")){
                            continue;
                        }
                        real = new BigDecimal(temp);
                        max = null == services.getRangemaximum()?null:new BigDecimal(services.getRangemaximum());
                        min = null == services.getRangeminimum()?null:new BigDecimal(services.getRangeminimum());
                        try {
                            declaredField = data.getClass().getDeclaredField(reflectRelation.get(name));//根据属性名称获取实体属性
                            declaredField.setAccessible(true);
                            if((null != min && real.compareTo(min) <0) || (null != max && real.compareTo(max) >0)){
                                declaredField.set(data,1);//给属性设置值
                            }else{
                                declaredField.set(data,0);
                            }
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        }

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        return pumpReportDataList;
    }

参考文章:java中遍历实体类,获取属性名和属性值_Mrgray的博客-CSDN博客_java中遍历实体类获取属性值

java反射给实体类赋值_Edison_kon的博客-CSDN博客_java反射给实体类赋值

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-04-01 23:11:18  更:2022-04-01 23:15:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:48:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码