SpringBoot-JPA自定义SQL(分页)语句

package com.example.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.example.bean.Product;

public interface ProductDao extends JpaRepository<Product, Integer>{
	
	@Query(value = "select * from product p where p.cid=?1", nativeQuery=true)
	public Page<Product> findByCategory_id(int cid, Pageable pageable);
	

}

注意了:
	即使是用@Query自定义SQL语句, 其中方法的命名也是讲究的, 否则会报黄色警告的!

findByCategory_id中:
1、Category是外键类
2、_id是外键类的属性

只涉及查询的SQL语句只用 @Query 就好了
如果涉及到更新,删除,插入的话就要加 @Modifying 了

疑问:

不知道为何我只能用 “nativeQuery=true” , 否则我写HQL的话就会报错

发布了63 篇原创文章 · 获赞 1 · 访问量 2661

猜你喜欢

转载自blog.csdn.net/qq_42039738/article/details/104613056