java分页查询

添加依赖:

<!-- 分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>

实体类:

package com.zbkj.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

@Data
public class DeviceSelfCheckData {
    private Integer id;
    private String name;
    private String detectionValue;
    private Integer state;
    private String alarmValue;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private String updateTime;

    @Override
    public String toString() {
        return "DeviceSelfCheckData{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", detectionValue='" + detectionValue + '\'' +
                ", state=" + state +
                ", alarmValue='" + alarmValue + '\'' +
                ", updateTime='" + updateTime + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetectionValue() {
        return detectionValue;
    }

    public void setDetectionValue(String detectionValue) {
        this.detectionValue = detectionValue;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getAlarmValue() {
        return alarmValue;
    }

    public void setAlarmValue(String alarmValue) {
        this.alarmValue = alarmValue;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }
}

Dao:

package com.zbkj.dao;

import com.github.pagehelper.PageInfo;
import com.zbkj.entity.DeviceSelfCheckData;
import com.zbkj.entity.ViewData;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DeviceDao {
    List<ViewData> selectDataBydate(String date);

    List<DeviceSelfCheckData> selectData();

    PageInfo<DeviceSelfCheckData> selectDeviceDataByPage(int pageNum, int pageSize);
}

Service:

package com.zbkj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.dao.DeviceDao;
import com.zbkj.entity.DeviceSelfCheckData;
import com.zbkj.entity.ViewData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeviceService {
    @Autowired
    private DeviceDao deviceDao;


    public List<ViewData> selectDataBydate(String date) {
        return deviceDao.selectDataBydate(date);
    }


    public List<DeviceSelfCheckData> selectData() {
        return deviceDao.selectData();
    }

    public PageInfo<DeviceSelfCheckData> selectDeviceDataByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceSelfCheckData> list = deviceDao.selectData();
        PageInfo<DeviceSelfCheckData> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}

Mapper.xml:

<?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.zbkj.dao.DeviceDao">
    <select id="selectDataBydate" resultType="com.zbkj.entity.ViewData">
        SELECT d.NAME name,
               AVG(v.VALUE) value
        FROM
            device AS d
            LEFT JOIN realtime_value AS v
        ON d.id = v.device_id
        WHERE
            DATE_FORMAT( v.date
            , '%Y-%m' ) = #{date}
        GROUP BY
            v.device_id
    </select>
    <select id="selectData" resultType="com.zbkj.entity.DeviceSelfCheckData">
        select device_id       id,
               device_name     name,
               detection_value detectionValue,
               device_state    state,
               alarm_value     alarmValue,
               update_time     updateTime
        from device_self_check
    </select>
    <select id="selectDeviceDataByPage" resultType="com.zbkj.entity.DeviceSelfCheckData">
        select *
        from device_self_check
    </select>
</mapper>

Controller:

package com.zbkj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.entity.DeviceSelfCheckData;
import com.zbkj.entity.ViewData;
import com.zbkj.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeviceController {
    @Autowired
    private DeviceService iDeviceService;

    @RequestMapping(value = "/getData/{date}", method = RequestMethod.GET)
    public List<ViewData> getData(@PathVariable String date) {
        return iDeviceService.selectDataBydate(date);

    }

    @RequestMapping(value = "/getSelfCheck", method = RequestMethod.POST)
    public List<DeviceSelfCheckData> getSelfCheckData() {
        PageHelper.startPage(1, 10);
        return iDeviceService.selectData();
    }

    @GetMapping("page")
    public PageInfo<DeviceSelfCheckData> getSelfCheckDataByPage(@RequestParam(value = "page", defaultValue = "1") int page) {
        PageInfo<DeviceSelfCheckData> result = iDeviceService.selectDeviceDataByPage(page, 10);
        return result;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值