The following table describes the keywords supported for JPA and what a method containing that keyword translates to:
下表描述了JPA支持的关键字以及包含该关键字的方法:
public interface UserRepository extends JpaRepository<User, Long> { List<User> findByLastname(String lastname); User findByEmailAddress(String emailAddress); } 复制代码
public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where u.emailAddress = ?1") User findByEmailAddress(String emailAddress); } 复制代码
public interface UserRepository extends JpaRepository<User, Long> { // 使用nativeQuery =ture 可以标识该sql是原生的sql查询, @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true) User findByEmailAddress(String emailAddress); } 复制代码
public interface UserRepository extends JpaRepository<User, Long> { @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1", countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1", nativeQuery = true) Page<User> findByLastname(String lastname, Pageable pageable); } #—————————————————————— #Pageable对象是分页构造对象,用来传入page,size,Sort # Sort.Direction.DESC 排序规则 # "id" 排序 字段 Sort sort=new Sort(Sort.Direction.DESC,"id"); Page<User> UserPage=UserDao.findByLastname(lastname,new PageRequest(0,10,sort)); 复制代码
public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where u.lastname like ?1%") List<User> findByAndSort(String lastname, Sort sort); } #_____________ 测试: String lastname="白"; List<User> users= userDao.findByAndSort(lastname,new Sort(Sort.Direction.DESC,"id")); for (User a: users) { System.out.println(a); } 复制代码
By default, Spring Data JPA uses position-based parameter binding, as described in all the preceding examples. This makes query methods a little error-prone when refactoring regarding the parameter position. To solve this issue, you can use @Param annotation to give a method parameter a concrete name and bind the name in the query, as shown in the following example:
public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname") User findByLastnameOrFirstname(@Param("lastname") String lastname, @Param("firstname") String firstname); } 复制代码
# 对 @Modifying @Query(value = "update User set User.title=?1 where User.id=?2",nativeQuery = true) int setTitle(String title,Integer id); 调用时需要在方法上添加事务注解 @Test @Transactional public void index4_3(){ String title="白居易"; int count =wangyiyunDao.setTitle(title,823); System.out.println("成功修改了"+count+"条"); } <!--______________错误_____________________--> # 错 #@Query(value = "update Wangyiyun set Wangyiyun.title=?1 where Wangyiyun.id=?2",nativeQuery = true) #int setTitle2(String title,Integer id); # 错 #@Modifying #@Query(value = "update Wangyiyun set Wangyiyun.title=?1 where Wangyiyun.id=?2") #int setTitle3(String title,Integer id); 复制代码
Its usage is select x from #{#entityName} x. It inserts the entityName of the domain type associated with the given repository. 它的用法是从#{#entityName} x中选择x。它插入与给定存储库关联的域类型的entityName。 The entityName is resolved as follows: If the domain type has set the name property on the @Entity annotation, it is used. entityName解析如下:如果域类型在@Entity注释上设置了name属性,则使用它。
public interface UserRepository extends JpaRepository<User,Long> { // #{#entityName} 相当于 User // 既 当前 @Query("select u from #{#entityName} u where u.lastname = ?1") List<User> findByLastname(String lastname); } 复制代码