如何用spring+hibernate+mysql实现搜索过滤器?

7qhs6swi  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(334)

首先,我不熟悉spring框架和hibernate。请容忍我。
我正在尝试实现一个搜索过滤器,用户可以选择指定名字、姓氏、出生日期、性别、国家、语言等 Guest 它存储这些记录。一些列(如gender、country和language)存储在具有唯一主键的单独表中。也就是说,我把这些记录的外键存储在 Guest table。
搜索的所有字段都是可选的。对于每个缺少的搜索字段,程序都必须假定为“any”。
考虑到这一点,我可以 LIKE 以及字符串列的通配符,例如名字和姓氏。对于外键列,我使用 = 接线员。如果缺少搜索字段,则该字段将替换为 null .
当我只指定名字(或者姓)时,我无法在输出中得到任何东西。
这是密码 CrudRepository 我写的接口。

public interface GuestRepository extends CrudRepository<Guest, Integer> {

    @Query("SELECT guest FROM Guest guest WHERE guest.propertyIdentifier = ?1 AND guest.firstName LIKE %?2% AND guest.lastName LIKE %?3% AND guest.titleIdentifier = ?4 AND guest.dateOfBirth = ?5 AND guest.genderIdentifier = ?6 AND guest.countryIdentifier = ?7 AND guest.languageIdentifier = ?8 AND guest.passport LIKE %?9%")
    Iterable<Guest> findGuests(Integer propertyIdentifier, String firstName, String lastName, Integer titleIdentifier, Date dateOfBirth, Integer genderIdentifier, Integer countryIdentifier, Integer languageIdentifier, String passport);
}

这是你的答案 Guest 实体。

@Entity
public class Guest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer identifier;

    private Integer propertyIdentifier;

    private String firstName;

    private String lastName;

    private Integer titleIdentifier;

    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;

    private Integer genderIdentifier;

    private Integer countryIdentifier;

    private Integer languageIdentifier;

    private String passport;

    private Integer status;

    public Integer getIdentifier() {
        return identifier;
    }

    public void setIdentifier(Integer identifier) {
        this.identifier = identifier;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getPassport() {
        return passport;
    }

    public void setPassport(String passport) {
        this.passport = passport;
    }

    public Integer getLanguageIdentifier() {
        return languageIdentifier;
    }

    public void setLanguageIdentifier(Integer languageIdentifier) {
        this.languageIdentifier = languageIdentifier;
    }

    public Integer getCountryIdentifier() {
        return countryIdentifier;
    }

    public void setCountryIdentifier(Integer countryIdentifier) {
        this.countryIdentifier = countryIdentifier;
    }

    public Integer getGenderIdentifier() {
        return genderIdentifier;
    }

    public void setGenderIdentifier(Integer genderIdentifier) {
        this.genderIdentifier = genderIdentifier;
    }

    public Integer getTitleIdentifier() {
        return titleIdentifier;
    }

    public void setTitleIdentifier(Integer titleIdentifier) {
        this.titleIdentifier = titleIdentifier;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPropertyIdentifier() {
        return propertyIdentifier;
    }

    public void setPropertyIdentifier(Integer propertyIdentifier) {
        this.propertyIdentifier = propertyIdentifier;
    }
}

如果用户没有为搜索字段指定值,如何实现假定为“any”的搜索筛选器?我还有别的选择吗?

ilmyapht

ilmyapht1#

考虑使用标准api。它看起来像这样:

@Service
public class GuestService {

@PersistenceContext
private EntityManager em;

public List<Guest> findGuests(Guest searchedGuest) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Guest> cq = cb.createQuery(Guest.class);
    Root<Guest> quest = cq.from(Guest.class);
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (searchedGuest.getPropertyIdentifier != null) {
        predicates.add(cb.equal(quest.get("propertyIdentifier"), searchedGuest.getPropertyIdentifier));
    }
    if (searchedGuest.getFirstName != null) {
        predicates.add(cb.like(quest.get("firstName"), "%" + searchedGuest.getFirstName + "%"));
    }
    // other predicates

    cq.select(quest).where(predicates.toArray(new Predicate[] {}));
    List<Guest> guests = em.createQuery(cq).getResultList();
    return guests;
}

其中searchedguest是具有可选填充字段的对象。

相关问题