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;@Repositorypublic interface TestEntityRepository extends JpaRepositoryImplementation<TestEntity,Integer> {}
#Service
import org.springframework.validation.annotation.Validated;import javax.validation.constraints.NotBlank;@Validatedpublic 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;@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這里就不寫(xiě)了,很普通,只說(shuō)重點(diǎn)!
看下官方文檔的解釋?zhuān)?/b>
補(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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決2. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊3. 使用JSP技術(shù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線測(cè)試系統(tǒng)的實(shí)例詳解4. asp批量添加修改刪除操作示例代碼5. 開(kāi)發(fā)效率翻倍的Web API使用技巧6. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)7. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題8. ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹(shù)形下拉框的方法9. .NET6打包部署到Windows Service的全過(guò)程10. WML的簡(jiǎn)單例子及編輯、測(cè)試方法第1/2頁(yè)
