jpa @Query demo 演示

.GEEK發表於2020-12-14

package com.lyr.easymybatis.repository;

import com.lyr.easymybatis.pojo.po.ApiInfoDemo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 參考資料:
 * https://blog.csdn.net/qq_39264897/article/details/105106375
 *
 *
 *
 * @Author lyr
 * @create 2020/12/14 22:32
 */
public interface ApiInfoDemoRepository extends JpaRepository<ApiInfoDemo,Long> {


   /**
    * url like
    * or
    * title like
    * or
    * markdownInfo like
    *
    *
    * @param searchText
    * @return
    */
   List<ApiInfoDemo> findByUrlContainingOrTitleContainingOrMarkdownInfoContaining(String searchText,String searchText2,String txt);


   @Query(value = "select a  from ApiInfoDemo a where a.markdownInfo like %:q%" )
   List<ApiInfoDemo> searchByTxt(@Param("q") String q);
}


分頁查詢:

package com.lyr.easymybatis.repository;

import com.lyr.easymybatis.pojo.po.ApiInfoDemo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 參考資料:
 * https://blog.csdn.net/qq_39264897/article/details/105106375
 *
 *
 *
 * @Author lyr
 * @create 2020/12/14 22:32
 */
public interface ApiInfoDemoRepository extends JpaRepository<ApiInfoDemo,Long> {


   /**
    * url like
    * or
    * title like
    * or
    * markdownInfo like
    *
    *
    * @param searchText
    * @return
    */
   List<ApiInfoDemo> findByUrlContainingOrTitleContainingOrMarkdownInfoContaining(String searchText,String searchText2,String txt);


   @Query(value = "select a  from ApiInfoDemo a where a.markdownInfo like %:q%" )
   List<ApiInfoDemo> searchByTxt(@Param("q") String q,Pageable pageable);
}



@Test
    void queryLLL() {
        System.out.println(
                apiInfoDemoRepository
                .searchByTxt("world",PageRequest.of(0,10))
        );
    }


注意:要從 第0頁開始,外國人從0開始的

相關文章