两表联级更新

一、案例描述

数据库中有仪器设备表data_apparatus和仪器设备检校表data_apparatus_check_scheme。
实现联级更新,当修改检校表中的“检校结果”和“检校时间”时,设备表的这两个数据也同步更新。

二、实现步骤

1.创建vo类

在原有检校表实体类DataApparatus的基础上,创建新的实体类DataApparatusVo,该实体类中加入了更新需要的属性。

DataApparatusVo:

package com.ruoyi.system.domain.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Date;

public class DataApparatusVo {
    private static final long serialVersionUID = 1L;

    /** 主键 */
    private Long id;



    /** 检校表中的仪器id,与设备表组件对应 */

    private Long apparatusId;

    /** 出厂编号 */
    @Excel(name = "出厂编号")
    private String factoryNum;

    /** 技术指标 */
    @Excel(name = "技术指标")
    private String technicalIndex;

    /** 使用部门(字典) */
    @Excel(name = "使用部门(字典)")
    private String userDepartment;

    /** 责任人 */
    @Excel(name = "责任人")
    private String responsiblePer;

    /** 存放地点 */
    @Excel(name = "存放地点")
    private String storageSite;

    /** 检/校日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "检/校日期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date checkDate;

    /** 检校表中的检校日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "检/校日期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date checkYear;

    /** 检/校周期 */
    @Excel(name = "检/校周期")
    private String checkCycle;

    /** 计划检/校日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "计划检/校日期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date planCheckDate;

    /** 是否限用(字典) */
    @Excel(name = "是否限用", readConverterExp = "字=典")
    private String isRestricted;

    /** 限用说明 */
    @Excel(name = "限用说明")
    private String restrictState;

    /** 备注 */
    @Excel(name = "备注")
    private String rmk;

    /** 有效期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "有效期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date validDate;

    /** 检/校结果(字典) */
    @Excel(name = "检/校结果", readConverterExp = "字=典")
    private String checkResult;

    /** 检校表中的检/校结果(字典) */
    @Excel(name = "检/校结果")
    private Integer checkResultVo;

    /** 实验室编号(字典) */
    @Excel(name = "实验室编号(字典)")
    private String labId;

    /** 仪器设备名称 */
    @Excel(name = "仪器设备名称")
    private String apparatusName;

    /** 仪器设备型号 */
    @Excel(name = "仪器设备型号")
    private String apparatusModel;

    /** 建档时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "建档时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date bookbuildingTime;

    /** 核查结果(字典) */
    @Excel(name = "核查结果(字典)")
    private String inspectResult;

    /** 是否需要校验 */
    @Excel(name = "是否需要校验")
    private Integer isNeedCheck;

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

    public Long getId()
    {
        return id;
    }
    public void setFactoryNum(String factoryNum)
    {
        this.factoryNum = factoryNum;
    }

    public String getFactoryNum()
    {
        return factoryNum;
    }
    public void setTechnicalIndex(String technicalIndex)
    {
        this.technicalIndex = technicalIndex;
    }

    public String getTechnicalIndex()
    {
        return technicalIndex;
    }
    public void setUserDepartment(String userDepartment)
    {
        this.userDepartment = userDepartment;
    }

    public String getUserDepartment()
    {
        return userDepartment;
    }
    public void setResponsiblePer(String responsiblePer)
    {
        this.responsiblePer = responsiblePer;
    }

    public String getResponsiblePer()
    {
        return responsiblePer;
    }
    public void setStorageSite(String storageSite)
    {
        this.storageSite = storageSite;
    }

    public String getStorageSite()
    {
        return storageSite;
    }
    public void setCheckDate(Date checkDate)
    {
        this.checkDate = checkDate;
    }

    public Date getCheckDate()
    {
        return checkDate;
    }
    public void setCheckCycle(String checkCycle)
    {
        this.checkCycle = checkCycle;
    }

    public String getCheckCycle()
    {
        return checkCycle;
    }
    public void setPlanCheckDate(Date planCheckDate)
    {
        this.planCheckDate = planCheckDate;
    }

    public Date getPlanCheckDate()
    {
        return planCheckDate;
    }
    public void setIsRestricted(String isRestricted)
    {
        this.isRestricted = isRestricted;
    }

    public String getIsRestricted()
    {
        return isRestricted;
    }
    public void setRestrictState(String restrictState)
    {
        this.restrictState = restrictState;
    }

    public String getRestrictState()
    {
        return restrictState;
    }
    public void setRmk(String rmk)
    {
        this.rmk = rmk;
    }

    public String getRmk()
    {
        return rmk;
    }
    public void setValidDate(Date validDate)
    {
        this.validDate = validDate;
    }

    public Date getValidDate()
    {
        return validDate;
    }
    public void setCheckResult(String checkResult)
    {
        this.checkResult = checkResult;
    }

    public String getCheckResult()
    {
        return checkResult;
    }
    public void setLabId(String labId)
    {
        this.labId = labId;
    }

    public String getLabId()
    {
        return labId;
    }
    public void setApparatusName(String apparatusName)
    {
        this.apparatusName = apparatusName;
    }

    public String getApparatusName()
    {
        return apparatusName;
    }
    public void setApparatusModel(String apparatusModel)
    {
        this.apparatusModel = apparatusModel;
    }

    public String getApparatusModel()
    {
        return apparatusModel;
    }
    public void setBookbuildingTime(Date bookbuildingTime)
    {
        this.bookbuildingTime = bookbuildingTime;
    }

    public Date getBookbuildingTime()
    {
        return bookbuildingTime;
    }
    public void setInspectResult(String inspectResult)
    {
        this.inspectResult = inspectResult;
    }

    public String getInspectResult()
    {
        return inspectResult;
    }
    public void setIsNeedCheck(Integer isNeedCheck)
    {
        this.isNeedCheck = isNeedCheck;
    }

    public Integer getIsNeedCheck()
    {
        return isNeedCheck;
    }

    public void setCheckYear(Date checkYear)
    {
        this.checkYear = checkYear;
    }
    public Date getCheckYear()
    {
        return checkYear;
    }

    public void setCheckResultVo(Integer checkResultVo)
    {
        this.checkResultVo = checkResultVo;
    }
    public Integer getCheckResultVo()
    {
        return checkResultVo;
    }

    public Long getApparatusId() {
        return apparatusId;
    }

    public void setApparatusId(Long apparatusId) {this.apparatusId = apparatusId;}

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("factoryNum", getFactoryNum())
                .append("technicalIndex", getTechnicalIndex())
                .append("userDepartment", getUserDepartment())
                .append("responsiblePer", getResponsiblePer())
                .append("storageSite", getStorageSite())
                .append("checkDate", getCheckDate())
                .append("checkCycle", getCheckCycle())
                .append("planCheckDate", getPlanCheckDate())
                .append("isRestricted", getIsRestricted())
                .append("restrictState", getRestrictState())
                .append("rmk", getRmk())
                .append("validDate", getValidDate())
                .append("checkResult", getCheckResult())
                .append("labId", getLabId())
                .append("apparatusName", getApparatusName())
                .append("apparatusModel", getApparatusModel())
                .append("bookbuildingTime", getBookbuildingTime())
                .append("inspectResult", getInspectResult())
                .append("isNeedCheck", getIsNeedCheck())
                .append("checkYear", getCheckYear())
                .append("checkResultVo", getCheckResultVo())
                .append("apparatusId", getApparatusId())
                .toString();
    }
}


2.修改Mapper.xml文件中的更新方法

DataApparatusMapper.xml:

<!--    根据检校表更新设备表相应数据-->
    <update id="updateDataApparatusByCheckScheme" parameterType="DataApparatusVo">
        update data_apparatus da
            LEFT JOIN data_apparatus_check_scheme dacs
            ON  da.id = dacs.apparatus_id
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkYear != null">check_date = #{checkYear}</if>,
            <if test="validDate != null">valid_date = #{validDate},</if>
            <if test="checkResult != null">da.check_result = #{checkResult},</if>
        </trim>
        where da.id = #{apparatusId}
    </update>
    

3.修改其余文件

DataApparatusMapper.java

  /**
     * 根据检校表修改仪器设备清单
     *
     * @param dataApparatusVo 仪器设备清单
     * @return 结果
     */
    public int updateDataApparatusByCheckScheme(DataApparatusVo dataApparatusVo);

IDataApparatusService.java

 /**
     * 根据检校表修改仪器设备清单
     *
     * @param dataApparatusVo 仪器设备清单
     * @return 结果
     */
    public int updateDataApparatusByCheckScheme(DataApparatusVo dataApparatusVo);

DataApparatusServiceImpl.java

/**
     * 根据检校表修改仪器设备清单
     *
     * @param dataApparatusVo 仪器设备清单
     * @return 结果
     */
    @Override
    public int updateDataApparatusByCheckScheme(DataApparatusVo dataApparatusVo)
    {
        return dataApparatusMapper.updateDataApparatusByCheckScheme(dataApparatusVo);
    }

DataApparatusController.java:

 /**
     * 根据检校表修改仪器设备清单
     */
    @PreAuthorize("@ss.hasPermi('system:apparatus:editByCheckScheme')")
    @Log(title = "仪器设备清单", businessType = BusinessType.UPDATE)
    @PutMapping("/byCheckScheme")
    public AjaxResult editByCheckScheme(@RequestBody DataApparatusVo dataApparatusVo)
    {
        return toAjax(dataApparatusService.updateDataApparatusByCheckScheme(dataApparatusVo));
    }

apparatus.js:

// 根据检校表修改仪器设备清单
export function updateApparatusByCheckScheme(data) {
  return request({
    url: '/system/apparatus/byCheckScheme',
    method: 'put',
    data: data
  })
}

总结

Vo类存储需要更新的数据,然后在mapper.xml修改sql语句。然后修改对应的mapper.java,service,serviceImpl,Controller文件,最后修改前端js文件。

Logo

快速构建 Web 应用程序

更多推荐