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());
}
}
# 1 楼答案
我认为,如果您在存储库方法中使用关键字Like和After,就会容易得多
这可能有助于https://stackoverflow.com/a/45692717/2494123和Supported keywords inside method names Keyword Sample
# 2 楼答案
我以这种方式使用了类似的条件:
因此,在您的情况下,应:
# 3 楼答案
您可以使用更改存储库
您可以使用所需的日期和作为
insnr
的“1”调用该方法