简体   繁体   中英

How to implement advanced search functionality in Spring MVC with Spring Boot?

I am new to Spring Boot. I am implementing the microservices architecture. I have two entities driver and car. There is one to one relationship between driver and car. One driver can select only one car and one car can be selected by only one driver.

I want to implement advanced search functionality. It should be possible to search for drivers by their attributes (username, online_status) as well as car characteristics (license number, car name, etc). I am using Database as H2 (In memory). I want to avoid nested if and else.

What is the best way to do this in Spring Boot?

I think the best way to do it is enable hibernate-search module in your code. For this aim you could use Indexed and Field annotation.

example below

@Entity
@Indexed
public class Car {
    @Column(name = "name")
    @Field
    private String name;

    @Column(name = "license_number")
    @Field
    private Integer licenseNumber;


}

And after that you could use search functionality in your service layer

@Service
public class CarServiceImpl implements CarService {

    @PersistenceContext
    private EntityManager entityManager;

@Override
    @Transactional
    public List<Car> search(Sting name, String licenseNumber) {
        FullTextSearchBuilder builder = new FullTextSearchBuilder(entityManager, Car.class);
        builder.appendAndQuery(name, "name");
        builder.appendAndQuery(licenseNumber, "licenseNumber");
        FullTextQuery fullTextQuery = builder.createFullTextQuery();
        return (List<Car>) fullTextQuery.getResultList();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM