久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Spring Date jpa 獲取最新一條數據的實例代碼

瀏覽:97日期:2023-08-10 14:58:30

#Repository

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

import org.springframework.validation.annotation.Validated;import javax.validation.constraints.NotBlank;@Validatedpublic interface TestService { /** * 獲取賬號最新的一個數據(指單個字段,例如最新時間,最新年齡等) * @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;@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 Date jpa 獲取最新一條數據的實例代碼

Spring Date jpa 獲取最新一條數據的實例代碼

補充知識:【spring data jpa】根據一個時間字段 查詢 時間段的處理方法

處理方法 包含結束時間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));}

包含結束時間 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 獲取最新一條數據的實例代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 潜江市| 塔城市| 从化市| 平罗县| 金川县| 浪卡子县| 襄城县| 淳安县| 安康市| 甘泉县| 偃师市| 徐州市| 汉中市| 武强县| 嘉鱼县| 基隆市| 鹤庆县| 集贤县| 邹城市| 嘉峪关市| 沈阳市| 云浮市| 黄平县| 陆良县| 稷山县| 霍邱县| 福鼎市| 泗水县| 定远县| 金山区| 明星| 丰原市| 宝鸡市| 筠连县| 泰州市| 新和县| 上蔡县| 正镶白旗| 班戈县| 晋江市| 奎屯市|