java反射对实体类取值和赋值

时间:2022-05-03 19:23:11
public static void checkDesignerEdit(Object dtos) throws Exception {
        Class  dtosClass = dtos.getClass();
//获得类的声明的字段 Field createTime
= dtosClass.getDeclaredField("createTime");
//可访问私有变量 createTime.setAccessible(
true);
//获取实体属性值 Date createDate
= (Date) createTime.get(dtos); String c = DateUtil.getFormateDateLong(createDate); String e = DateUtil.addDateMinut(c, 2880); String nowDate = DateUtil.getFormateDateLong(new Date()); int differ = DateUtil.getDatePoor(e, nowDate, 1, 0); Field workStatusF = dtosClass.getDeclaredField("workStatus"); workStatusF.setAccessible(true); int workStatus= (int) workStatusF.get(dtos); Field methodSetEdit = dtosClass.getDeclaredField("edit"); methodSetEdit.setAccessible(true); Field methodSetEditTime = dtosClass.getDeclaredField("edtiTime"); methodSetEditTime.setAccessible(true); if (differ > 0) { if (workStatus == 0 || workStatus == 10) {
//调用实体类的set方法进行赋值 methodSetEdit.set(dtos,
1); methodSetEditTime.set(dtos,(long) differ); } } else { if (workStatus == 13 || workStatus == 16) { methodSetEdit.set(dtos,1); methodSetEditTime.set(dtos,0L); } else { methodSetEdit.set(dtos,0); methodSetEditTime.set(dtos,0L); } } }
 public CommonResult worksList(String userId, int page, int pageSize) throws Exception  {
        PageHelper.startPage(page, pageSize);
        List<DesignDTO1> dtos = designMapper.worksList(userId);
        for (DesignDTO1 dto : dtos) {
//调用反射方法,实体类有公共的属性和方法可调用 checkDesignerEdit(dto); } PageInfo p
= new PageInfo(dtos); Map map = new HashMap(); map.put("rows", p.getList()); map.put("totalCount", p.getTotal()); return CommonResult.success(map); }
//实体类

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class DesignDTO1 {
    private Integer id;
    /**
     * 作品编号
     */
    private String worksId;
    /**
     * 作品名称
     */
    private String worksName;

    /**
     * 上传时间
     */
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;

    /**
     * 状态status_show
     */
    private Integer status;
    /**
     * 状态描述
     */
    private String statusMsg;
    /**
     * 作品封面图(图片名称)
     */
    private String workCoverImg;

    /**
     * 是否可修改
     */
    private Integer edit;
    /**
     * 可修改剩余时间
     */
    private  Long edtiTime;

    /**
     * 业务状态status
     */
    private Integer workStatus;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getWorksId() {
        return worksId;
    }

    public void setWorksId(String worksId) {
        this.worksId = worksId;
    }

    public String getWorksName() {
        return worksName;
    }

    public void setWorksName(String worksName) {
        this.worksName = worksName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public String getWorkCoverImg() {
        return workCoverImg;
    }

    public void setWorkCoverImg(String workCoverImg) {
        this.workCoverImg = workCoverImg;
    }

    public Integer getEdit() {
        return edit;
    }

    public void setEdit(Integer edit) {
        this.edit = edit;
    }

    public Long getEdtiTime() {
        return edtiTime;
    }

    public void setEdtiTime(Long edtiTime) {
        this.edtiTime = edtiTime;
    }

    public Integer getWorkStatus() {
        return workStatus;
    }

    public void setWorkStatus(Integer workStatus) {
        this.workStatus = workStatus;
    }
}