NO IMAGE

專案之前分頁是在前端寫的,現在因為資料量比較大,需要把分頁邏輯放到後端寫,重新溫習了下分頁,並且做下記錄,裡面是我自己的一些業務邏輯,需要的話換成自己的邏輯就OK了。

 

1.工具類ResultData


package com.zknw.util;
import com.zknw.constant.Code;
public class ResultData<T> {
private String code;
private String message;
private T data;
private Integer count;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public ResultData() {
super();
// TODO Auto-generated constructor stub
}
public ResultData(T data) {
super();
this.code = Code.SUCCESS;
this.message ="成功";
this.data = data;
}
public ResultData(String code, String message, T data) {
super();
this.code = code;
this.message = message;
this.data = data;
}
public ResultData(String code, String message, Integer count) {
super();
this.code = code;
this.message = message;
this.count = count;
}
public ResultData(String code, String message, T data, Integer count) {
super();
this.code = code;
this.message = message;
this.data = data;
this.count = count;
}
public void setUp(String code, String message, T data){
this.code = code;
this.message = message;
this.data = data;
} 
}

2.分頁工具類RHWarnVO,繼承的實體類我就不貼了


package com.zknw.basicmanage.model;
import java.io.Serializable;
public class RHWarnVO extends ReportHisWarnVO implements Serializable {
/**
* turbineIds
*/
private Integer[] deviceIds;
/**
* tagIds
*/
private Integer[] tagIds;
/**
*當前頁
*/
private Integer pageNumber;
/**
*每頁多少
*/
private Integer quantity;
/**
*總條數
*/
private Integer count;
/**
*起始索引
*/
private Integer start;
/**
* 排序欄位
*/
private String sortField;
/**
* 排序數值,1正序,-1倒序
*/
private Integer sortOrder;
/**
* 模糊查詢條件
*/
private String divWarnName;
public Integer getPageNumber() {
return pageNumber;
}
public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public String getSortField() {
return sortField;
}
public void setSortField(String sortField) {
this.sortField = sortField;
}
public Integer getSortOrder() {
return sortOrder;
}
public void setSortOrder(Integer sortOrder) {
this.sortOrder = sortOrder;
}
public String getDivWarnName() {
return divWarnName;
}
public void setDivWarnName(String divWarnName) {
this.divWarnName = divWarnName;
}
public Integer[] getDeviceIds() {
return deviceIds;
}
public void setDeviceIds(Integer[] deviceIds) {
this.deviceIds = deviceIds;
}
public Integer[] getTagIds() {
return tagIds;
}
public void setTagIds(Integer[] tagIds) {
this.tagIds = tagIds;
}
}

3.controller介面


package com.zknw.basicmanage.controller;
import com.zknw.basicmanage.api.ReportHisWarnService;
import com.zknw.basicmanage.model.RHWarnVO;
import com.zknw.basicmanage.model.ReportHisWarn;
import com.zknw.basicmanage.model.ReportHisWarnVO;
import com.zknw.constant.Code;
import com.zknw.sysmanage.controller.BaseController;
import com.zknw.util.ResultData;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* 歷史預警
*/
@RestController
@RequestMapping(value = "${versionPath}/reportHisWarns",produces="text/plain; charset=UTF-8")
public class ReportHisWarnController extends BaseController {
@Autowired
private ReportHisWarnService reportHisWarnService;
private static final Logger logger = Logger.getLogger(ReportHisWarnController.class);
/**
* 查詢歷史預警
*/
@RequestMapping(value="/getReportHisWarnVOByIds",method=RequestMethod.POST)
public void getReportHisWarnVOByIds(HttpServletRequest request,HttpServletResponse response, @RequestBody RHWarnVO rhWarnVO){
try{
retBases(request,response,new ResultData(Code.SUCCESS, "查詢成功", reportHisWarnService.getReportHisWarnVOByIds(rhWarnVO), reportHisWarnService.countrte(rhWarnVO)));
}catch(Exception e){
logger.error("歷史預警的控制層",new Throwable(e.getMessage()));
retBases(request,response,new ResultData(Code.ERROR, "查詢失敗", e.getMessage()));
}
}
}

4.service介面就不貼了,直接上實現類


package com.zknw.basicmanage.impl;
import com.zknw.basicmanage.api.ReportHisWarnService;
import com.zknw.basicmanage.dao.ReportHisWarnDao;
import com.zknw.basicmanage.model.RHWarnVO;
import com.zknw.basicmanage.model.ReportHisWarn;
import com.zknw.basicmanage.model.ReportHisWarnVO;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 報表預警服務
*/
@Service("reportHisWarnService")
public class ReportHisWarnServiceImpl implements ReportHisWarnService {
@Resource
private ReportHisWarnDao reportHisWarnDao;
private static final Logger logger = Logger.getLogger(ReportHisWarnServiceImpl.class);
/**
*查詢歷史預警
* @return
*/
@Override
public List<ReportHisWarnVO> getReportHisWarnVOByIds(RHWarnVO rhWarnVO) throws Exception{
try{
Map<String,Object> map=new HashMap<String,Object>();
map.put("startTime", rhWarnVO.getStartTime());
map.put("endTime", rhWarnVO.getEndTime());
map.put("deviceIds", rhWarnVO.getDeviceIds());
map.put("tagIds", rhWarnVO.getTagIds());
map.put("pageNumber",rhWarnVO.getPageNumber());
map.put("quantity",rhWarnVO.getQuantity());
map.put("divWarnName",rhWarnVO.getDivWarnName());
map.put("sortField",rhWarnVO.getSortField());
if (rhWarnVO.getSortOrder() != null && rhWarnVO.getSortOrder() == 1){
map.put("sortOrder","asc");
}else if (rhWarnVO.getSortOrder() != null && rhWarnVO.getSortOrder() == -1){
map.put("sortOrder","desc");
}else {
map.put("sortOrder","asc");
}
return reportHisWarnDao.getReportHisWarnVOByIds(map);
}catch (Exception e){
logger.info("報表預警服務",new Throwable(e.getMessage()));
throw e;
}
}
@Override
public Integer countrte(RHWarnVO rhWarnVO) {
try{
return reportHisWarnDao.countrte(rhWarnVO);
}catch (Exception e){
logger.info("報表預警服務",new Throwable(e.getMessage()));
return null;
}
}
}

5.mapper檔案,dao層的介面就不貼了


<?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.zknw.basicmanage.dao.ReportHisWarnDao" >
<resultMap id="BaseResultMap" type="com.zknw.basicmanage.model.ReportHisWarn" >
<id column="earlywarningid" property="earlyWarningId" jdbcType="BIGINT" />
<result column="deviceid" property="deviceId" jdbcType="BIGINT" />
<result column="tagid" property="tagId" jdbcType="BIGINT" />
<result column="stateid" property="stateId" jdbcType="BIGINT" />
<result column="starttime" property="startTime" jdbcType="DATE" />
<result column="endtime" property="endTime" jdbcType="DATE" />
<result column="statename" property="stateName" jdbcType="VARCHAR" />
<result column="maxval" property="maxVal" jdbcType="DECIMAL" />
<result column="minval" property="minVal" jdbcType="DECIMAL" />
</resultMap>
<select id="getReportHisWarnVOByIds" resultType="com.zknw.basicmanage.model.ReportHisWarnVO" parameterType="java.util.Map">
SELECT skip #{pageNumber} FIRST #{quantity}
rte.earlywarningid,
rte.deviceid,
rte.tagid,
rte.stateid,
rte.starttime,
rte.endtime,
rte.statename,
rte.maxval,
rte.minval,
bd.devicename turbinename,bta.tagname,bf.farmname,bms.statecolor from r_turbine_earlywarning rte
LEFT JOIN basic_device bd ON bd.deviceid = rte.deviceid
LEFT JOIN basic_tag bta ON bta.tagid =rte.tagid
LEFT JOIN basic_farm  bf ON bf.farmid = bd.farmid
LEFT JOIN basic_matrix_state bms ON bms.stateid = rte.stateid
where 1=1
<if test="deviceIds != null">
rte.deviceid IN
<foreach item="item" index="index" collection="deviceIds" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="startTime != null and startTime != ''">
AND rte.starttime <![CDATA[>=]]> #{startTime}
</if>
<if test="endTime != null and endTime != ''">
AND rte.endtime <![CDATA[<=]]> #{endTime}
</if>
<if test="divWarnName != null and divWarnName != ''">
AND concat(concat(bf.farmname,bd.devicename),bta.tagname) like '%${divWarnName}%'
</if>
<if test="sortField != null and sortField != ''">
ORDER BY ${sortField} ${sortOrder}
</if>
</select>
<select id="countrte" resultType="java.lang.Integer" parameterType="com.zknw.basicmanage.model.RHWarnVO">
SELECT count(*) from r_turbine_earlywarning rte
LEFT JOIN basic_device bd ON bd.deviceid = rte.deviceid
LEFT JOIN basic_tag bta ON bta.tagid =rte.tagid
LEFT JOIN basic_farm  bf ON bf.farmid = bd.farmid
LEFT JOIN basic_matrix_state bms ON bms.stateid = rte.stateid
where 1=1
<if test="deviceIds != null">
rte.deviceid IN
<foreach item="item" index="index" collection="deviceIds" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="startTime != null and startTime != ''">
AND rte.starttime <![CDATA[>=]]> #{startTime}
</if>
<if test="endTime != null and endTime != ''">
AND rte.endtime <![CDATA[<=]]> #{endTime}
</if>
<if test="divWarnName != null and divWarnName != ''">
AND concat(concat(bf.farmname,bd.devicename),bta.tagname) like '%${divWarnName}%'
</if>
</select>
</mapper>