【问题标题】:java sql query how can i select all data without parameterjava sql查询如何选择没有参数的所有数据
【发布时间】:2020-10-15 12:51:58
【问题描述】:

您好,最近我一直在研究 REST API。 我想知道如果没有参数,如何获取所有数据。 我只能通过 categoryId 获取数据,如下所示。 http:// .... /categorys?categoryId=2 数据是这样选择和显示的。

{
  "items": [
  {
  "id": 2,
  "name": "music",
  "count": 20
  }   
 ]
}

但我也想获取所有没有参数的数据,如下所示 => http:// .... /类别

{
  "items": [
      {
        "id": 1,
        "name": "movie",
        "count": 10
      },
      {
        "id": 2,
        "name": "music",
        "count": 20
      },
      {
        "id": 3,
        "name": "book",
        "count": 16
      }    
   ]
}

这是该类别数据的mysql查询

SELECT_CATEGORYS = "SELECT category.id as id, category.name as name, count(*) as count FROM product inner join category where product.category_id = category.id group by category.id, category.name;"

这是 CategoryApiController.java

@RestController
@RequestMapping(path="/categorys")
public class CategoryApiController {
@Autowired
ReservationService reservationService;

@GetMapping
public Map<String, Object> categorys(@RequestParam(name="categoryId", required=false, defaultValue="0") int categoryId) {
    
    List<Categorys> list = reservationService.getCategorys(categoryId);
    Map<String, Object> map = new HashMap<>();
    map.put("item", list);
    
    return map;
  }
}

请帮我解决这个问题。 提前致谢。 路径不应该改变,我需要保留这个 getmapping /categorys 所以没有参数它应该是 /categorys 和参数,它应该是这样的 /categories?categoryId = 2

【问题讨论】:

标签: java mysql json spring


【解决方案1】:

查询

 SELECT_CATEGORYS = "SELECT category.id as id, category.name as name, count(*) as count FROM product inner join category group by category.id, category.name;"

控制器

@RestController
    @RequestMapping(path="/categorys")
    public class CategoryApiController {
    @Autowired
    ReservationService reservationService;
    @GetMapping
    public Map<String,List<Category>> categorys() {
        List<Categorys> list = reservationService.getCategories();
        Map<String, Object> map = new HashMap<>();
        map.put("item", list);
        return map;
      }
    }

【讨论】:

    【解决方案2】:

    您可以制作另一种方法,例如:

    @GetMapping("/all")
    public Map<String, Object> getAllCategories(){
        
        List<Categorys> list = reservationService.getAllCategories(); // and this method use your query
        Map<String, Object> map = new HashMap<>();
        map.put("item", list);
        
        return map;
      }
    }
    

    当你调用它时,使用:http:// .... /categorys/all .还有一个拼写错误,category 的复数形式是 categories,但这并不重要。

    【讨论】:

    • 感谢您的评论,但这不是我想要的。我想继续请求映射 /categorys 或 /categories 什么的。
    • 然后,你可以简单地删除你的参数 @RequestParam(name="categoryId", required=false, defaultValue="0") int categoryId 并调用没有'id'参数的reservationService.getCategories()
    【解决方案3】:

    您不应在 getMapping 和查询中使用 categoryId。 在您的查询中,可以是这个,SELECT_CATEGORYS = "SELECT category.id as id, category.name as name, count(*) as count FROM product inner join category group by category.id, category.name;" 在你的 getMapping 上,你不应该有任何参数。它可以像

    @GetMapping
    public Map<String, Object> categorys()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多