转载

Spring Data JPA 入门使用

The following table describes the keywords supported for JPA and what a method containing that keyword translates to:

下表描述了JPA支持的关键字以及包含该关键字的方法:

Spring Data JPA 入门使用
Spring Data JPA 入门使用

使用开始

public interface UserRepository extends JpaRepository<User, Long> {

  List<User> findByLastname(String lastname);

  User findByEmailAddress(String emailAddress);
}
复制代码

使用 @Query 手动定义查询,查询返回结果需要是一个对象,不能使用 *

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);
}
复制代码

JPA 分页对象

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);
        }

复制代码

Jpa默认使用位置?1,给自定义sql中的参数传值,也可以使用参数名称传值

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);
}
复制代码

注释

  1. @Entity 实体类标识
  2. @Table ( name ="user" ) 表明映射
  3. @Id id 标识
  4. @GeneratedValue(strategy=GenerationType.AUTO) 主键自增
  5. @Column(name = "song_id" ) 跟表字段的映射
  6. @Transient 忽略字段和数据库对应

增,删,改

# 对
    @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);
}
复制代码
原文  https://juejin.im/post/5da3c5bff265da5bbc3e92e1
正文到此结束
Loading...