面向对象的一对多查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhengTornado/article/details/80321380

以设备表和报警事件表为例

一台设备对应多条报警事件

  1. 需求:查询某台设备的报警事件状态为未处理或者处理中 ,也包括正常状态的设备
  2. 数据库的表结构如下图
    (1)设备表
    设备表
    (2)报警事件表
    这里写图片描述
  3. pojo对象javabean
    (1)设备表对应的映射实体
package com.zxhzn.firecontrol.devices.entity;

import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zxhzn.common.entity.BaseEntity;
import com.zxhzn.firecontrol.fireMaintain.entity.SysFireMaintain;

/**
 * 设备实体
 * @author Administrator
 *
 */
public class Devices extends BaseEntity{
    /**
     * 设备ID
     */
    private String devicesId;
    /**
     * 设备名称
     */
    private String devicesName;
    /**
     * 设备经度
     */ 
    private String devicesLatitude;
    /**
     * 设备纬度
     */ 
    private String devicesLongitude;
    /**
     * 省
     */
    private String province;
    /**
     * 城
     */
    private String city;
    /**
     * 区
     */
    private String region;
    /**
     * 设备详细物理地址
     */
    private String address; 
    /**
     * 负责人 id
     */
    private String devicesPerson;
    /**
     * 设备负责人名称
     */
    private String devicesPersonName;
    /**
     * 设备型号
     */
    private String devicesType;
    /**
     * 设备编号
     */
    private String devicesNumber;
    /**
     * 设备状态 0在线1离线
     */
    private String devicesStatus;
    /**
     * 出厂编号
     */
    private String factoryNumber;
    /**
     * 安装日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    private Date installDate;
    /**
     * 设备用途
     */
    private String devicesPurpose;

    /**
     * 完好程度
     */
    private String devicesDegree;
    /**
     * 预警状态
     */
    private String warningStatus;
    /**
     * 设备类型
     */
    private String devicesClass;
    /**
     * 故障日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    private Date devicesFaultDate;
    /**
     * 故障类型
     */
    private String devicesFaultType;

    /**
     * 扩展字段1
     */
    private Integer extendS1 ; 

    /**
     * 扩展字段2
     */
    private Integer extendS2 ; 
    /**
     * 报警事件表的reason字段,设备表的库表中没有这个字段
     * @return
     */
    private String reason;
    /**
     * 一对多字段,关联报警事件表
     * @return
     */
   private List<SysFireMaintain> sysFireMaintainList;
    /**
     * 以下是get,set方法,此处省略
     * @return
     */
}

(2)报警处理事件表对应的映射实体

package com.zxhzn.firecontrol.fireMaintain.entity;

import java.io.Serializable;
import java.util.Date;

public class SysFireMaintain implements Serializable{
    /**
     * 报警流水id
     */
    private String wid;
    /**
     * 报警设备id(外键:设备表的主键)
     */
    private String fdid;
    /**
     * 报警日期
     */
    private Date wdate;
    /**
     * 报警原因(字典维护:意外撞倒、开盖、电量不足)
     */
    private String reason;
    /**
     * 状态 (0 未处理,1处理中,2已处理)默认未处理
     */
    private String state;
    /**
     * 报警处理人(再没有分配处理人之前,该字段默认为设备的负责人)
     */
    private String opuser;
    /**
     * 以下是get,set方法,此处省略
     * @return
     */
}

4.mapper.xml的封装
(1)设备Mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zxhzn.firecontrol.devices.dao.DevicesMapper">
    <cache/> 
  <resultMap id="BaseResultMap" type="com.zxhzn.firecontrol.devices.entity.Devices">
    <id column="devices_id" jdbcType="VARCHAR" property="devicesId" />
    <result column="devices_name" jdbcType="VARCHAR" property="devicesName" />
    <result column="devices_latitude" jdbcType="VARCHAR" property="devicesLatitude" />
    <result column="devices_longitude" jdbcType="VARCHAR" property="devicesLongitude" />
    <result column="province" jdbcType="VARCHAR" property="province" />
    <result column="city" jdbcType="VARCHAR" property="city" />
    <result column="region" jdbcType="VARCHAR" property="region" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="devices_person" jdbcType="VARCHAR" property="devicesPerson" />
    <result column="devices_type" jdbcType="VARCHAR" property="devicesType" />
    <result column="devices_status" jdbcType="CHAR" property="devicesStatus" />
    <result column="factory_number" jdbcType="VARCHAR" property="factoryNumber" />
    <result column="install_date" jdbcType="TIMESTAMP" property="installDate" />
    <result column="devices_purpose" jdbcType="VARCHAR" property="devicesPurpose" />
    <result column="devices_number" jdbcType="VARCHAR" property="devicesNumber" />
    <result column="status" jdbcType="CHAR" property="status" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="devices_person_name" jdbcType="VARCHAR" property="devicesPersonName" />
    <!--以下是预留字段,暂时无用 -->  
    <result column="devices_degree" jdbcType="VARCHAR" property="devicesDegree" />
    <result column="warning_status" jdbcType="CHAR" property="warningStatus" />  
    <result column="devices_class" jdbcType="CHAR" property="devicesClass" />
    <result column="devices_fault_date" jdbcType="TIMESTAMP" property="devicesFaultDate" />
    <result column="devices_fault_type" jdbcType="CHAR" property="devicesFaultType" /> 
    <collection property="sysFireMaintainList" column="devices_id"
        select="com.zxhzn.firecontrol.fireMaintain.dao.FireMaintainMapper.getFireMaintainList" ></collection>
  </resultMap>

  <sql id="Base_Column_List">
    devices_id,devices_name, devices_latitude,devices_longitude,province,city,region,address,
    devices_person,devices_person_name,devices_type,devices_status,factory_number, install_date,devices_purpose,devices_number,    
    status, create_by, create_date, update_by, update_date, remarks,
    devices_degree, warning_status, devices_class, devices_fault_date,  devices_fault_type 
  </sql>

  <!--查询某台设备的报警事件状态为未处理或者处理中 的报警原因,报警日期,设备负责人,详细地址  -->
  <select id="devicesWithFireMaintainList" resultMap="BaseResultMap" parameterType="java.lang.String">
    select 
    <include refid="Base_Column_List"/>
    from sys_devices 
    <where>
        and status=0  
    </where>
  </select>
  <!--查询某个用户登录后的所有 正常状态 -->
  <select id="selectAll" parameterType="java.lang.String" resultType="com.zxhzn.firecontrol.devices.entity.Devices">
    select
    <include refid="Base_Column_List"/>,w.reason
    from sys_devices  d
    LEFT JOIN sys_fire_maintain w 
    on d.devices_id=w.fdid
    <where>
    and d.status=0
    <if test="_parameter !=null and _parameter!='' ">
        and d.devices_person=#{_parameter,jdbcType=VARCHAR}
    </if>
    </where>
  </select>

(2)报警事件Mapper

<mapper namespace="com.zxhzn.firecontrol.fireMaintain.dao.FireMaintainMapper">
  <resultMap id="BaseResultMap" type="com.zxhzn.firecontrol.fireMaintain.entity.FireMaintain">
    <id column="wid" jdbcType="VARCHAR" property="wid" />
    <id column="devices_purpose" jdbcType="VARCHAR" property="devicespurpose" />
    <result column="region" jdbcType="VARCHAR" property="region" />
    <result column="wdate" jdbcType="TIMESTAMP" property="wdate" />
    <result column="reason" jdbcType="VARCHAR" property="reason" />
    <result column="state" jdbcType="CHAR" property="state" />
    <result column="devices_person" jdbcType="VARCHAR" property="devicesPerson" />
  </resultMap>
    <sql id="deFm_Column_List">
        wid,fdid,wdate,reason,state,opuser
   </sql>
     <!--查询某台设备的报警事件状态为未处理或者处理中 的数据  -->
    <select id="getFireMaintainList" resultType="com.zxhzn.firecontrol.fireMaintain.entity.SysFireMaintain" parameterType="java.lang.String">
        select 
          <include refid="deFm_Column_List"/>
         from sys_fire_maintain
         <where>
            fdid = #{devicesId}
            and state in (0,1)
         </where>
    </select>
 </mapper>
  • 设备表的Mapper层
    /**
     * 查询某台设备的报警事件状态为未处理或者处理中 的报警原因,报警日期,设备负责人,详细地址  
     */
    public List<Devices> devicesWithFireMaintainList(); 
  • 设备表的sevice层
    /**
     * 查询某台设备的报警事件状态为未处理或者处理中 的报警原因,报警日期,设备负责人,详细地址  
     * @return
     */
    public List<Devices> devicesWithFireMaintainList(); 
  • 设备表的Service实现层
    /**
     * 查询某台设备的报警事件状态为未处理或者处理中 的报警原因,报警日期,设备负责人,详细地址  
     * @return
     */
    public List<Devices> devicesWithFireMaintainList(){
        List<Devices> list=devicesMapper.devicesWithFireMaintainList();
        return list;
    }
  • 设备表的Controller

    /**
     * 查询某台设备的报警事件状态为未处理或者处理中 的报警原因,报警日期,设备负责人,详细地址  
     * @return
     */
    @RequestMapping(value="/queryDeviceDetail.do")
    @ResponseBody
     public List<Devices> devicesWithFireMaintainList(){
        return deviceService.devicesWithFireMaintainList();
    }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页