gpt4 book ai didi

java - JPA Spring - 带字符串的过滤器

转载 作者:行者123 更新时间:2023-12-02 11:50:09 25 4
gpt4 key购买 nike

我有个人,我只想获取以某个字母开头的人(该字母来自输入字段)。是否可以在不查询的情况下获得此信息?我怎样才能做到这一点?

@Service 
public class PersonService {

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

类(class)人员:

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

Controller :

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

最佳答案

您的意思是:

String start = "";
return personRepository.findAll().stream()
.filter(person -> person.getName().startsWith(start)) //<<---Note this
.map(Person::fromPerson);
<小时/>

或者,如果您有不敏感的情况,您可以使用:

String start = "";
return personRepository.findAll().stream()
.filter(person ->
person.getName().matches("(?i)^" + Pattern.quote(input) + ".*"))//(1)
.map(Person::fromPerson);
<小时/>

(1) 对于不区分大小写,例如,如果您的记录是“Hello”并且输入是“he”,则会选择它,除了匹配项之外,还使用正则表达式,因此您的正则表达式应该类似于 "( ?i)他。*”

关于java - JPA Spring - 带字符串的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com