Spring Date jpa 獲取最新一條數(shù)據(jù)的實(shí)例代碼
#Repository
import test.demo.domain.entity.TestEntity;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.stereotype.Repository;
@Repository
public interface TestEntityRepository extends JpaRepositoryImplementation<TestEntity,Integer> {
}
#Service
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
@Validated
public interface TestService {
/**
* 獲取賬號(hào)最新的一個(gè)數(shù)據(jù)(指單個(gè)字段,例如最新時(shí)間,最新年齡等)
* @param username
* @return
*/
String findLastage(@NotBlank String username);
}
#ServiceImpl
import test.demo.domain.entity.TestEntity;
import test.demo.repository.TestEntityRepository;
import test.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestEntityRepository testEntityRepository;
@Override
public String findLastAge(String username) {
Sort createTime = new Sort(Sort.Direction.DESC, "createTime");
PageRequest of = PageRequest.of(0, 1, createTime);
List<TestEntity> testEntities= testEntityRepository.findAll(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.and(
criteriaBuilder.equal(root.get("age"), age),
criteriaBuilder.lessThan(root.get("expiredTime"), LocalDateTime.now())
)));
if (testEntities.isEmpty()){
return null;
}
String age = TestEntities.get(0).getCode();
return age;
}
}
Entity這里就不寫(xiě)了,很普通,只說(shuō)重點(diǎn)!
看下官方文檔的解釋:


補(bǔ)充知識(shí):【spring data jpa】根據(jù)一個(gè)時(shí)間字段 查詢 時(shí)間段的處理方法
處理方法 包含結(jié)束時(shí)間00.00.00
Date createDate = boxCodeLog.getCreateDate();
if (createDate != null){
LocalDate localDate=createDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
localDate = localDate.plusDays(1);
Date endDate = java.sql.Date.valueOf(localDate);
predicates.add(cb.between(root.get("createDate"),createDate,endDate));
}
包含結(jié)束時(shí)間 23.59.59
Date createDate = receipt.getCreateDate();
if (createDate != null){
LocalDate localDate=createDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
localDate = localDate.plusDays(1);
Date endDate = new Date(java.sql.Date.valueOf(localDate).getTime()-1L);
predicates.add(cb.between(root.get("createDate"),createDate,endDate));
}
以上這篇Spring Date jpa 獲取最新一條數(shù)據(jù)的實(shí)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java轉(zhuǎn)換流(InputStreamReader/OutputStreamWriter)的使用
本文主要介紹了Java轉(zhuǎn)換流(InputStreamReader/OutputStreamWriter)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Spring Boot配置動(dòng)態(tài)更新問(wèn)題
這篇文章主要介紹了Spring Boot配置動(dòng)態(tài)更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
springboot整合RabbitMQ 中的 TTL實(shí)例代碼
TTL 是 RabbitMQ 中一個(gè)消息或者隊(duì)列的屬性,表明一條消息或者該隊(duì)列中的所有消息的最大存活時(shí)間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下2022-09-09
詳解mybatis-plus的 mapper.xml 路徑配置的坑
這篇文章主要介紹了詳解mybatis-plus的 mapper.xml 路徑配置的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot注冊(cè)第三方Bean的方法總結(jié)
眾所周知,SpringBoot默認(rèn)會(huì)掃描啟動(dòng)類所在的包及其子包,一般我們都是在需要的類上通過(guò)注解的方式去將Bean注冊(cè)交給IOC進(jìn)行管理,但是注冊(cè)第三方Bean的方案卻不支持,所以本文給大家介紹了SpringBoot注冊(cè)第三方Bean的方法,需要的朋友可以參考下2024-01-01
SpringCache輕松啟用Redis緩存的全過(guò)程
Spring Cache是Spring提供的一種緩存抽象機(jī)制,旨在通過(guò)簡(jiǎn)化緩存操作來(lái)提高系統(tǒng)性能和響應(yīng)速度,本文將給大家詳細(xì)介紹SpringCache如何輕松啟用Redis緩存,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-07-07
URLConnection發(fā)送HTTP請(qǐng)求的方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了URLConnection發(fā)送HTTP請(qǐng)求的方法,主要介紹了如何通過(guò)Java(模擬瀏覽器)發(fā)送HTTP請(qǐng)求,有興趣的可以了解一下2017-07-07

