repository仓库
public interface PersonRepository extends Repository<Person,Integer>{
//根据lastName来获取对应的Person
Person getByLastName(String lastName);
//WHERE lastName LIKE ?% AND id < ?
List<Person> getByLastNameStartingWithAndIdLessThan(String lastName,Integer id);
//WHERE lastName LIKE %? AND id<?
List<Person> getByLastNameEndingWithAndIdLessThan(String lastName,Integer id);
//WHERE email IN(?,?,?) OR birth <?
List<Person> getByEmailInOrBirthLessThan(List<String> emails,Date birth);
//WHERE a.id>?
List<Person> getByAddressIdCreaterThan(Integer id);
//getByAddress_IdCreaterThan(Integer id);
//查询id值最大的那个Person
//使用@Query注解可以自定义JPQL语句以实现更灵活的查询
@Query("SELECT p FROM Person p WHERE p.id=(SELECT max(p.id) FROM Person p2)")
Person getMaxIdPerson();
//1.@Query注解传递参数的方式1,使用占位符
@Query("SELECT p FROM Person p WHERE p.lastName=?1 AND p,email =?2")
List<Person> testQueryAnnotationParam1(String lastName,String email);
//2.位Query注解传递参数的方式2:命名参数的方式
@Query("SELECT p FROM Person p WHERE p.lastName= :lastName AND p.email=:email")
List<Person> testQueryAnnotationParams2(@Param("email")String email,@Param("lastName")String lastName);
//SpringData 允许在占位符上添加 %%.
@Query("SELECT p FROM Person p WHERE p.lastName LIKE %?1% OR p.email LIKE %?2%")
List<Person> testQueryAnnotationLikeParam(String lastName, String email);
//SpringData 允许在占位符上添加 %%.
@Query("SELECT p FROM Person p WHERE p.lastName LIKE %:lastName% OR p.email LIKE %:email%")
List<Person> testQueryAnnotationLikeParam2(@Param("email") String email, @Param("lastName") String lastName);
//设置 nativeQuery=true 即可以使用原生的 SQL 查询
@Query(value="SELECT count(id) FROM jpa_persons", nativeQuery=true)
long getTotalCount();
}
|