【问题标题】:Query in spring data Jpa multiple condition在spring data Jpa中查询多个条件
【发布时间】:2015-04-11 19:29:44
【问题描述】:

结果sql应该是:

"SELECT * FROM items where id LIKE '%"+ key + "%' or name LIKE '%"+ key + "%'";  

这里的key是可变的。我需要在spring data jpa中做这个sql。

我尝试如下,但代码不起作用

@Transactional
public List<Item> findItemNameOrId(String key) {
    return  itemRepository.findByItemNameOrIdContaining(key);       
}

这是 ItemRepository

public interface ItemRepository extends JpaRepository<Item, Integer> {

List<Item> findByItemNameOrIdContaining(String key);
}

【问题讨论】:

  • 请添加您的方法生成的查询。
  • 而且你总是可以添加@Query("select * from items where id like %?1% or name like %?1%");

标签: spring jpa spring-data-jpa


【解决方案1】:

像这样尝试,它应该可以运行

public interface ItemRepository extends JpaRepository<Item, Integer> {

@Query("SELECT i FROM items i where i.id LIKE %:key% or i.name LIKE %:key%")
    List<Item> findByItemNameOrIdContaining(@Param("key") String key);
}

【讨论】:

    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 2016-01-31
    • 2018-10-31
    • 2016-06-03
    • 2022-01-06
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多