springdata jpa 查询部分字段

jpql 分页查询部分字段,不采用interface接口类,采用Map接收

  • Map接收查询结果,再从Map里取值
        //查询地图巡查员详情信息
      @Query(value = "select obs.id as id,obs.observerName as observerName,obs.isOnline as isOnline,obs.geoX as geoX,obs.geoY as geoY," +
              "obs.gpsTime as gpsTime,obs.mobile as mobile,obs.userType as userType,ar.areaName as areaName,an.filePath as obsIcon,ows.signDate as signDate " +
              " from Observer obs left join Area ar on (obs.areaId = ar.id) " +
              "left join ObserverWorkSignLog ows on (obs.id = ows.observerId) left join Annexe an on (obs.photoId = an.id)" +
              "where obs.id = ?1 order by ows.signDate DESC ")
      Page<Map<String,Object>> findObserverDetailInfo(Long obsId,Pageable pageable);
    
  • Map取值,这里是因为相关数据有多条,只需要最新一条,采用分页,但只查一条 Pageable pageable=PageRequest.of(0,1);
      @Override
      public SuperviseObserverInfo findObserverDetailInfo(Long obsId){
          Pageable pageable=PageRequest.of(0,1);
          SuperviseObserverInfo observerInfo = null;
          Page<Map<String,Object>> info = observerRepository.findObserverDetailInfo(obsId,pageable);
          if (null != info.getContent()){
              Map<String,Object> map = info.getContent().get(0);
              observerInfo = new SuperviseObserverInfo();
              observerInfo.setId(KEDataTypeUtil.objToLong(map.get("id")));
              observerInfo.setAreaName(KEDataTypeUtil.objToString(map.get("areaName")));
              observerInfo.setObserverName(map.get("observerName").toString());
              observerInfo.setMobile(map.get("mobile").toString());
              observerInfo.setIsOnline(KEDataTypeUtil.objToInteger(map.get("isOnline")));
              observerInfo.setUserType(KEDataTypeUtil.objToInt(map.get("userType")));
              observerInfo.setGeoX(KEDataTypeUtil.objToDouble(map.get("geoX")));
              observerInfo.setGeoY(KEDataTypeUtil.objToDouble(map.get("geoY")));
              observerInfo.setObsIcon(KEDataTypeUtil.objToString(map.get("obsIcon")));
              observerInfo.setGpsTime(KEDataTypeUtil.objToTimestamp(map.get("gpsTime")));
              observerInfo.setSignDate(KEDataTypeUtil.objToTimestamp(map.get("signDate")));
          }
          return observerInfo;
      }
    
  • 如果查询多条,并且分页可以使用interface写法
    interface查询部分字段

你可能感兴趣的:(jpa)