接口定义的方法进行查询
- 继承
JpaRepository
的后方法 - 继承
JpaSpecificationExecutor
后的方法
使用 JPQL 方式查询
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
@Query(value = "from Customer where custName = ?")
public Customer findJpql(String custName);
@Query(value = "from Customer where custName = ? and custId = ?")
public Customer findCustNameAndId(String name,Long id);
@Query(value = "update Customer set custName = ?2 where id = ?1")
@Modifying
public void updateCustomer(long id,String name);
}
使用原生SQL查询
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
@Query(value = "select * from cst_customer",nativeQuery = true)
public List<Object[]> findSql();
@Query(value = "select * from cst_customer where cust_name like ?",nativeQuery = true)
public List<Object[]> findByName(String name);
方法命名规则查询
public Customer findByCustName(String custName);
public List<Customer> findByCustNameLike(String custName);
public Customer findByCustNameLikeAndCustIndustry(String name,String industry);