时间:2020-10-07来源:www.pcxitongcheng.com作者:电脑系统城
#Repository
?| 1 2 3 4 5 6 7 |
import test.demo.domain.entity.TestEntity;import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;import org.springframework.stereotype.Repository;@Repositorypublic interface TestEntityRepository extends JpaRepositoryImplementation<TestEntity,Integer> {} |
#Service
?| 1 2 3 4 5 6 7 8 9 10 11 12 |
import org.springframework.validation.annotation.Validated;import javax.validation.constraints.NotBlank;@Validatedpublic interface TestService { /** * 获取账号最新的一个数据(指单个字段,例如最新时间,最新年龄等) * @param username * @return */ String findLastage(@NotBlank String username);} |
#ServiceImpl
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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;@Servicepublic 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这里就不写了,很普通,只说重点!
看下官方文档的解释:


补充知识:【spring data jpa】根据一个时间字段 查询 时间段的处理方法
处理方法 包含结束时间00.00.00
?| 1 2 3 4 5 6 7 |
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)); } |
包含结束时间 23.59.59
?| 1 2 3 4 5 6 7 |
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 获取最新一条数据的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
2024-07-16
如何使用 Go 依赖库管理器修复损坏的依赖项?2024-07-07
Java框架如何简化代码的调试过程2023-03-17
Python 使用tf-idf算法计算文档关键字权重并生成词云的方法有这么一段代码,可以先看一下有没有什么问题,作用是输入一段json字符串,反序列化成map,然后将另一个inputMap的内容,merge进这个map 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
2023-03-15
由于数据库的类型为Data 类型,所以插入数据库的时候我先把前端传入的string类型的时间转为Time 再插入。 Go 提供了两种插入的方式,即time.Parse 和 time.ParseInLocation 。两种方式,他们的差异比较大。...
2023-03-09