如何使用多个键和值在表格中进行搜索?

huangapple go评论116阅读模式
英文:

How to search in table using multiple key and value?

问题

我正在尝试使用Angular和Spring Boot实现多选搜索功能。
我已经从前端以键值对的形式获得了选择数据。我该如何编写查询以从表中获取数据?

我得到的数据如下:

{"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 "";
}

请告诉我如何编写用于搜索的查询?

英文:

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?

答案1

得分: 2

这里是使用MVC模式的基本结构。

在控制器中:

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

在服务类中:

ProductClass getSingleProduct(Map allRequestParams){
    String query = getQuery(allRequestParams); // 使用映射编写必要的查询
    return productRepository.getProduct(query);
}

在仓库类中:

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

我提供了这个基本结构,以便于你如果是SpringBoot的初学者的话,更容易理解。

英文:

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.

答案2

得分: 0

你可以使用一个简单的Java JSON解析库。例如,Jackson:https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator

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

    // 解析字符串为Map
    Map<String, Object> mapData = new ObjectMapper().readValue(data, HashMap.class);
   
    // 编写查询
    // String bride = mapData.get("groom_Bride")
    // ....
}
英文:

You can use a simple JSON parser library for Java. For example Jackson https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator

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

    // Parse String to Map
	Map&lt;String,Object&gt; mapData = new ObjectMapper().readValue(data, HashMap.class);
   
    // Write query
    // String bride = mapData.get(&quot;groom_Bride&quot;)
    // ....
}

huangapple
  • 本文由 发表于 2020年9月15日 15:06:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63896745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定