// ------------------------------------ 使用 @Query 注解 // 没有参数的查询 @Query("select p from Person p where p.id = (select max(p2.id) from Person p2)") Person getMaxIdPerson(); //使用 ? 和数字代表传入的参数 @Query("select p from Person p where lastName=?1 and email=?2") Person readPersonByLastNameAndEmail(String lastName,String email); //使用 :代表传入的参数 @Query("select p from Person p where email=:email and lastName=:name") Person readPersonByLastNameAndEmailThroughName(@Param("name") String lastName,@Param("email") String email); // 使用 like @Query("select p from Person p where lastName like ?1") Person readPersonByLike(String likeName); // @Query 注解支持使用百分号 @Query("select p from Person p where lastName like %?1%") Person readPersonByLike2(String likeName); // @Query 注解支持使用百分号 @Query("select p from Person p where lastName like %:lastName%") Person readPersonByLike3(@Param("lastName")String name); // 使用原生的 SQL @Query(value="select * from jpa_person p1 where p1.last_name like %:lastName%",nativeQuery=true) Person getPersonUsingOriginSQL(@Param("lastName")String lastName);
时间: 2024-10-14 00:42:02