JPA Spring - 过滤日期

LuM*_*uMi 3 java spring autowired spring-data-jpa

可以过滤日期吗?例如我只想选择最近30天的数据?选择时间应该是从现在到现在 - 30 天之间。我更喜欢没有查询的解决方案。

@Service 
public class PersonService { 

   @Autowired 
   private PersonRepository personRepository;         
   public Stream<Person> all(Person mysearch){ 
       return personRepository 
              .findAll(Example.of(mysearch)) 
              .stream() 
              .map(Person::fromPerson); 
  } 
}
Run Code Online (Sandbox Code Playgroud)

班级人物:

public class Person { 

    public Integer index; 
    public String firstname; 
    public String lastname; 
    @JsonFormat(pattern="dd.MM.yyyy") 
    public Date exdate; 
    public String insnr; 

    private Person(Integer index, String firstname, String lastname, Date exdate, String insnr){ 
        this.index=index; 
        this.firstname=firstname; 
        this.lastname=lastname; 
        this.exdate=exdate; 
        this.insnr=insnr; 
    } 

    public static Person fromPerson(Person person){ 
        return person == null ? null : new Person(person.getIndex(), person.getFirstname(), person.getLastname(), person.getExdate(), person.getInsnr()); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

控制器:

@Autowired 
   private PersonService personService; 
   @RequestMapping(value="/person/list/**") 
   public List<Person> loadPersonList(   
                   @RequestParam(value = "insnr" ,required=false) String insnr) throws ParseException {         
       mysearch.setInsnr(insnr); 
       return personService.all(mysearch).collect(Collectors.toList()); 
   } 
Run Code Online (Sandbox Code Playgroud)

Arn*_*aud 5

Spring JPA 存储库支持GreaterThan关键字查询

你可以考虑这样的事情:

LocalDate dateBefore30Days = LocalDate.now().minusDays(30);

List<Person> persons =  personRepository.findByExdateGreaterThan(dateBefore30Days);
Run Code Online (Sandbox Code Playgroud)