|
|
package com.rehome.mqttclienttemperature.dao;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.rehome.mqttclienttemperature.entity.TemperatureEsp;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
|
import org.springframework.data.jpa.repository.Query;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Esp8266 DHT传感器获取到的温湿度数据
|
|
|
* 参数一 T :当前需要映射的实体
|
|
|
* 参数二 ID :当前映射的实体中的OID的类型
|
|
|
*
|
|
|
*/
|
|
|
public interface TemperatureEspRepository extends JpaRepository<TemperatureEsp,Long> {
|
|
|
|
|
|
//方法名称必须要遵循驼峰式命名规则,findBy(关键字)+属性名称(首字母大写)+查询条件(首字母大写)
|
|
|
TemperatureEsp findByDataDate(String dataDate);
|
|
|
TemperatureEsp findByDataHour(String dataHour);
|
|
|
TemperatureEsp findByDataMinute(String dataMinute);
|
|
|
@Query(value = "select * from temperature t where t.data_date=?1 and t.location_desc=?2", nativeQuery = true)
|
|
|
List<TemperatureEsp> findByDataDateAndLocationDesc(String dataDate,String locationDesc);
|
|
|
|
|
|
List<TemperatureEsp> findFirst10ByDataDateAndLocationDesc(String dataDate,String locationDesc, Sort sort);
|
|
|
|
|
|
Page<TemperatureEsp> queryFirst10ByDataDateAndLocationDesc(String dataDate,String locationDesc, Pageable pageable);
|
|
|
|
|
|
TemperatureEsp findFirstByOrderByDataDateDesc();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 五、分页查询
|
|
|
//
|
|
|
// Page<User> findALL(Pageable pageable);
|
|
|
// Page<User> findByUserName(String userName,Pageable pageable);
|
|
|
//
|
|
|
// 在查询的方法中,需要传入参数Pageable ,
|
|
|
// 当查询中有多个参数的时候Pageable建议做为最后一个参数传入 。
|
|
|
// Pageable 是 Spring 封装的分页实现类,使用的时候需要传入页数、每页条数和排序规则。
|
|
|
//
|
|
|
// int page=1,size=10;
|
|
|
// Sort sort = new Sort(Direction.DESC, "id");
|
|
|
// Pageable pageable = new PageRequest(page, size, sort);
|
|
|
// userRepository.findALL(pageable);
|
|
|
// userRepository.findByUserName("testName", pageable);
|
|
|
//
|
|
|
//
|
|
|
// 六、限制查询
|
|
|
//
|
|
|
// 查询前 N 个元素
|
|
|
//
|
|
|
// User findFirstByOrderByLastnameAsc();
|
|
|
// User findTopByOrderByAgeDesc();
|
|
|
// Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
|
|
|
// List<User> findFirst10ByLastname(String lastname, Sort sort);
|
|
|
// List<User> findTop10ByLastname(String lastname, Pageable pageable);
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序【专题】
|
|
|
//
|
|
|
// 首先来说一下分页和排序所用到的Page、Pageable接口和Sort类都是什么
|
|
|
//
|
|
|
// JpaRepository提供了两个和分页和排序有关的查询
|
|
|
//
|
|
|
// List findAll(Sort sort) 返回所有实体,按照指定顺序排序返回
|
|
|
//
|
|
|
// List findAll(Pageable pageable) 返回实体列表,实体的offest和limit通过pageable来指定
|
|
|
//
|
|
|
// Sort对象用来指示排序,最简单的Sort对象构造可以传入一个属性名列表(不是数据库列名,是属性名),默认采用升序排序。例:
|
|
|
//
|
|
|
// Sort sort = new Sort("id");
|
|
|
// //或 Sort sort = new Sort(Direction.ASC,"id");
|
|
|
// return userDao.findAll(sort);
|
|
|
// 程序将查询所有user并按照id进行生序排序。Sort还包括其他一些构造方法,在这里就不一一赘述。
|
|
|
//
|
|
|
// Pageable接口用于构造翻页查询,PageRequest是其实现类,可以通过提供的工厂方法创建PageRequest:
|
|
|
//
|
|
|
// public static PageRequest of(int page, int size)
|
|
|
// 也可以在PageRequest中加入排序:
|
|
|
//
|
|
|
// public static PageRequest of(int page, int size, Sort sort)
|
|
|
// 方法中的参数,page总是从0开始,表示查询页,size指每页的期望行数。
|
|
|
//
|
|
|
// Page接口可以获得当前页面的记录、总页数、总记录数、是否有上一页或下一页等。Spring Data翻页查询总是返回Page对象,Page对象提供了以下常用的方法:
|
|
|
//
|
|
|
// int getTotalPages() 总的页数
|
|
|
// long getTotalElements() 返回总数
|
|
|
// List getContent() 返回此次查询的结果集
|
|
|
// ————————————————
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |