SpringBoot-JPA custom SQL (paging) statement

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 了

doubt:

I do not know why I can only use "nativeQuery = true", otherwise I will write HQL, then an error

Published 63 original articles · won praise 1 · views 2661

Guess you like

Origin blog.csdn.net/qq_42039738/article/details/104613056