【问题标题】:spring boot search with mysql native query with LIKE return empty使用带有 LIKE 的 mysql 本机查询的 spring boot 搜索返回空
【发布时间】:2020-12-24 16:21:12
【问题描述】:

我有一个城市和国家的桌子。我尝试用 LIKE 的一些首字母选择城市和国家名称,但每次它返回空列表。在 MySql Workbench 中,此查询返回结果。我尝试了很多版本,但没有任何效果。

@Query(value = "SELECT city, country FROM test8.cities WHERE city LIKE ?'%'", nativeQuery = true)
List<City> findCity(String str);

我尝试了,这个方法也返回空列表。

   List<City> findByCityStartsWith(String str);
   List<City> findTop20DistinctByCityIsStartingWith(String str);

@Query(value = "SELECT city, country FROM test8.cities WHERE city LIKE ?1", nativeQuery = true) 列出 findByCityIsStartingWith(String str);

@Query(value = "select city, country FROM test8.cities WHERE city = :str", nativeQuery = true) 列出 findByCity2(@Param("str")String str);

【问题讨论】:

  • 你的参数 str 应该是 %someCity% 你能分享字符串参数 @Param("str")String str 的值吗?此外,您可以使用 Containing、Contains、IsContaining
  • @GetMapping("/cities") public @ResponseBody List getCitiesCountry(@RequestParam("searchChars") String searchChars){ List resultList = countryRepo.findCity(searchChars);返回结果列表; }
  • searchChars 的值是多少?你应该用 % 包裹它。您的查询看起来像:select city, country FROM test8.cities WHERE city like ‘test’ 而不是 ‘%test%’
  • 我每次按输入类型时都会选择带有首字母的城市,应该得到新的城市列表,所以我在后面加上 +'%' ?通配符。

标签: mysql sql spring spring-boot jpa


【解决方案1】:

问题出在已更改的实体中。

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 2013-05-27
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2011-10-04
    • 1970-01-01
    • 2012-02-26
    相关资源
    最近更新 更多