有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java选择具有相似条件的数据

你好

  • 例如,我试图选择所有以1开头的保险编号,但我只是接收以1开头的行,而不是所有以1开头的行
  • 我只想收到30天后到期的

我必须如何更改代码才能使这两个条件生效? 我已经试过几个例子来改变我的查询,但是没有一个是有效的

此外,我还添加了我的代码:

存储库:

@Query("From PERSON where exdate > current_date - to_char(30) and insnr like :insnr%") 
       Stream<PERSON> findAllWithSearchParams(@Param("insnr") String insnr); 

控制器:

@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()); 
   } 

服务:

@Service 
public class PersonService { 

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

班长:

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()); 
    } 
} 

共 (3) 个答案

  1. # 2 楼答案

    我以这种方式使用了类似的条件:

    @Query("select ....... where codition and date like  CONCAT('%','-',:m,'-',:d)")
    

    因此,在您的情况下,应:

    @Query("From PERSON where exdate > current_date - to_char(30) and insnr like CONCAT(:insnr,'%'))
    
  2. # 3 楼答案

    您可以使用更改存储库

    List<Person> findByExdateAfterAndInsnrStartingWith(Date exdate, String insnr);
    

    您可以使用所需的日期和作为insnr的“1”调用该方法