欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Date jpa 獲取最新一條數(shù)據(jù)的實例代碼

 更新時間:2020年10月07日 09:59:06   作者:Cobraes  
這篇文章主要介紹了Spring Date jpa 獲取最新一條數(shù)據(jù)的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

#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 {
  /**
   * 獲取賬號最新的一個數(shù)據(jù)(指單個字段,例如最新時間,最新年齡等)
   * @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這里就不寫了,很普通,只說重點!

看下官方文檔的解釋:

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

處理方法 包含結(jié)束時間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é)束時間 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ù)的實例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Netty實現(xiàn)WebSocket的示例代碼

    SpringBoot整合Netty實現(xiàn)WebSocket的示例代碼

    本文主要介紹了SpringBoot整合Netty實現(xiàn)WebSocket的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • Fluent Mybatis零xml配置實現(xiàn)復雜嵌套查詢

    Fluent Mybatis零xml配置實現(xiàn)復雜嵌套查詢

    本文主要介紹了Fluent Mybatis零xml配置實現(xiàn)復雜嵌套查詢,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java SSM整合開發(fā)統(tǒng)一結(jié)果封裝詳解

    Java SSM整合開發(fā)統(tǒng)一結(jié)果封裝詳解

    這篇文章主要介紹了Java SSM整合開發(fā)實現(xiàn)統(tǒng)一結(jié)果封裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • 關(guān)于Java雙大括號{{}}的具體使用

    關(guān)于Java雙大括號{{}}的具體使用

    本文主要介紹了關(guān)于Java雙大括號{{}}的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • 最新評論