當(dāng)前位置:首頁(yè) > IT技術(shù) > 數(shù)據(jù)庫(kù) > 正文

Spring系列:JPA 原生SQL查詢方法
2021-09-11 10:29:39

基礎(chǔ)

Spring系列:JPA JPQL使用方法

?

原生SQL方法

設(shè)置@Query的屬性為nativeQuery = true

@Query(value = "select u.* from Article u where u.title =  ? ", nativeQuery = true)

:title 、?1 匹配第一個(gè)參數(shù)title

    @Query(value = "select u.* from Article u where u.title = :title ", nativeQuery = true)
    List<Article> queryArticleList(String title) ;
    @Query(value = "select u.* from Article u where u.title = ?1 ", nativeQuery = true)
    List<Article> queryArticleList(String title) ;
Demo?

主要方法

    /**
     * Description: 文章列表
     */
    @Transactional
    @RequestMapping("/list2/{title}")
    public ModelAndView articlelist2(
                                     @PathVariable(value = "title"  ) String title) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(0, 2, sort);

        Page<Article> result = articleRepository.queryArticleList(title,pageable);
        System.out.println(result);

        ModelAndView mav = new ModelAndView("article/list");
        mav.addObject("page", result);
        return mav;
    }

Spring系列:JPA 原生SQL查詢方法_Spring系列

@Transactional:事務(wù)?

本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >