基础
原生SQL方法
设置@Query的属性为nativeQuery = true
@Query(value = "select u.* from Article u where u.title = ? ", nativeQuery = true)
:title 、?1 匹配第一个参数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;
}
@Transactional:事务
本文摘自 :https://blog.51cto.com/u