【问题标题】:Spring Boot search is not working properlySpring Boot 搜索无法正常工作
【发布时间】:2021-11-10 16:53:22
【问题描述】:

这里我想通过hotel name检索Hotels,但它只返回包含我包含的第一个字母的Hotels。

我将酒店名称搜索为“Sigiriya”,但当我输入“s”时,结果是 Sigiriya。但是当我开始输入更多(“igiriya”)时,结果消失了。

/**
     *
     * @param searchkey
     * @return - List of hotels
     */
    public List<Hotel> searchHotelByName( String searchkey){
        searchkey = searchkey.toLowerCase();
        searchkey = "%" + searchkey + "%";

        return hotelRepository.findAllByHotelNameContentLike( searchkey );
    }

【问题讨论】:

  • 您使用的是哪个数据库?请将标签添加到您的问题中。酒店名称和搜索词是什么?
  • 我加了,mysql

标签: mysql database spring-boot backend


【解决方案1】:

使用containsJPA keyword

public List<Hotel> searchHotelByName( String searchkey){
    return hotelRepository.findAllByHotelName IgnoreCaseContaining( searchkey );
}

contains 负责 % 等。

【讨论】:

    【解决方案2】:

    我认为原因是即使您将搜索键转换为小写,但您的数据库包含大写字母。因此使用ignore case search

    /**
         *
         * @param searchkey
         * @return - List of hotels
         */
        public List<Hotel> searchHotelByName( String searchkey){
            searchkey = searchkey.toLowerCase();
    
            return hotelRepository.findAllByHotelNameIgnoreCaseContaining( searchkey );
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2020-02-09
      • 2019-01-23
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2019-08-04
      • 2017-01-28
      相关资源
      最近更新 更多