Skip to content
Advertisement

How to search in table using multiple key and value?

I am trying to implement multiple select search using angular and spring boot. I got the select data form frontend in key and value pair. How can i write query to get the data from table?

I am getting the data like this {"groom_Bride":"groom","ageFrom":"18","ageTo":"24","city":"Graduate","education":"Graduate"}

    @RequestMapping(value = "/Search")
    String showSingleProduct(@RequestParam(value = "data", required = false) String data) {

    System.out.println("this is data" +data);

    return "";

Please tell me how can i write query for search?

Advertisement

Answer

Here’s the skeleton using MVC. Return results as single object/List of your ProductClass so it is easy to use.

//in controller
@RequestMapping(value = "/Search", method = RequestMethod.GET)
ProductClass showSingleProduct(@RequestParam Map allRequestParams) {
    
    return someService.getSingleProduct(allRequestParams);
}

// in service class
ProductClass getSingleProduct(Map allRequestParams){
   String query = getQuery(allRequestParams); // write query as needed using the map 
   return productRepository.getProduct(query);
}

// in repository
 ProductClass getProduct(String query){ 
    return entityManager.createNativeQuery(query,ProductClass.class).getSingleResult();
 }

I gave this skeleton so that its easier for you if you are a beginner in SpringBoot.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement