JPA分页查询

封装的实体类:


@Entity


@Table(name="active_alarm")


public class ActiveAlarm {


//告警编号


@Id


@GeneratedValue(strategy=GenerationType.IDENTITY)


@Column(name="alarm_id")


private Integer alarmId;


//告警源


@Column(name="alarm_source")


private String alarmSource;


//告警级别,1:清除告警;4:提示告警;


//5:一般告警6:重要告警;7:紧急告警


@Column(name="alarm_level")


private Integer alarmLevel;


//告警最后发生时间


@Column(name="last_event_time")


private Date lastEventTime;


//重复告警次数


@Column(name="alarm_count")


private Integer alarmCount;


//告警信息


@Column(name="alarm_information")


private String alarmInformation;


//额外信息


@Column(name="additionalInformation")


private String additionalInformation;


//详细信息


@Column(name="additional_text")


private String additionalText;


//确认标志,0:未确认;1:已确认


@Column(name="ack_flag")


private Integer ackFlag;


//确认时间


@Column(name="ack_time")


private Date ackTime;


//确认用户


@Column(name="ack_user")


private String ackUser;


//清除标志,0:未清除;1:已清除


@Column(name="clear_flag")


private Integer clearFlag;


//清除时间


@Column(name="clear_time")


private Date clearTime;


//清除用户


@Column(name="clear_user")


private String clearUser;


//工单编号


@Column(name="order_no")


private Integer orderNo;


//告警数量


@Transient


private Integer alarmLevelCount;


}


DAO使用JPA注解进行查询:


@Repository("activeAlarmDao")


public interface ActiveAlarmDao extends


JpaRepository,


JpaSpecificationExecutor{


    /**


    * 通过告警源查询告警信息


    * @param alarmSource


    * @return


    */


@Query("select  alarm from ActiveAlarm alarm


  where alarm.alarmSource=(?1)")


    List queryByAlarmSource(String alarmSource);


    /**


    * 查询每种告警级别的告警数量


    * @return


    */


@Query("select alarm.alarmLevel,count(alarmLevel)


from  ActiveAlarm alarm


group by alarmLevel")


  List queryCountLeveL();


}


service曾的处理:


  /**


  * 查询告警级别的数量


  */


@Override


public List queryCountLeveL() {


List query=activeAlarmDao.queryCountLeveL();


List list=new ArrayList();


for(Object[] obj:query){


ActiveAlarm alarm=new ActiveAlarm();


alarm.setAlarmLevel(Integer.parseInt(obj[0].toString()));


alarm.setAlarmLevelCount(Integer.parseInt(obj[1].toString()));


list.add(alarm);


}


return list;


}

你可能感兴趣的:(JPA分页查询)