【问题标题】:How to find letter in all columns with Spring Data JpaRepository如何使用 Spring Data JpaRepository 在所有列中查找字母
【发布时间】:2018-02-02 15:44:11
【问题描述】:

这是我的仓库

public interface NoteRepository extends JpaRepository<Note,Long> {
    List<Note> findByContentContains(String content);

以及我在 NoteController 类中的方法,它在 Content 列中返回带有“e”的单词

@GetMapping("/notesletter")
    public List<String> getLetters(){
        return noteRepository.findByContentContains("e")
                .stream()
                .map(note -> note.getContent())
                .collect(Collectors.toList());
    }

请帮助我找到一种方法,该方法将返回每个带有字母“e”的单词,例如,从所有列中返回。 我正在与邮递员合作

【问题讨论】:

    标签: java spring repository spring-data-jpa


    【解决方案1】:

    当然你也可以原生查询

     @Query("select u from Note u where u.COL1 = ?1 and u.COL2 = ?1")
     List<Note> findByContentContains(String content)
    

    或者使用下面给出的jpa功能,注意findByLastnameOrFirstnameStartingWith被提到很容易理解,所以将你的名字,姓氏分别替换为colu1name,col2name。Refer here for more jpa method conventions`

    public interface NoteRepository extends JpaRepository<Note,Long> {
    
         List<Note> findByLastnameOrFirstnameStartingWith(String param1,String param2)
    
         static List<Note> findByContentContains(String content){
          findByLastnameOrFirstnameStartingWith(content,content);
       }
     }
    

    【讨论】:

      【解决方案2】:

      您可以尝试如下方法名称中的或操作。 或者 findByLastnameOrFirstname

      https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html

      其他方法也是可能的。

      如果您知道返回检查实体所有列的结果的本机查询或 JPQL 等效项。

      使用

      @Query上面repository接口中的方法可以接受JPQL或者native query。

      @NamedQuery 如果您知道查询,也可以使用注释。该注解用于Entity类,name属性用于指定返回数据的访问方法名,可用于repository接口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-23
        • 2017-04-04
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        相关资源
        最近更新 更多