Spring Data Spring Data JPA Hello World Repository

  • Slides: 39
Download presentation

Spring. Data 概述

Spring. Data 概述

Spring. Data JPA Hello. World

Spring. Data JPA Hello. World

Repository 接口

Repository 接口

Spring. Data 方法定义规范

Spring. Data 方法定义规范

索引参数与命名参数 • 如果是 @Query 中有 LIKE 关键字,后面的参数需要前 面或者后面加 %,这样在传递参数值的时候就可以不加 %: – @Query("select o from

索引参数与命名参数 • 如果是 @Query 中有 LIKE 关键字,后面的参数需要前 面或者后面加 %,这样在传递参数值的时候就可以不加 %: – @Query("select o from User. Model o where o. name like ? 1%") public List<User. Model> find. By. Uuid. Or. Age(String name); – @Query("select o from User. Model o where o. name like %? 1%") public List<User. Model> find. By. Uuid. Or. Age(String name);

用@Query来指定本地查询 • 还可以使用@Query来指定本地查询,只要 设置native. Query为true,比如: – @Query(value="select * from tbl_user where name like %?

用@Query来指定本地查询 • 还可以使用@Query来指定本地查询,只要 设置native. Query为true,比如: – @Query(value="select * from tbl_user where name like %? 1" , native. Query=true) public List<User. Model> find. By. Uuid. Or. Age(String name);

@Modifying 注解和事务

@Modifying 注解和事务

Crud. Repository 接口

Crud. Repository 接口

Crud. Repository • Crud. Repository 接口提供了最基本的对实体类的添删改查操作 – T save(T entity); //保存单个实体 – Iterable<T> save(Iterable<?

Crud. Repository • Crud. Repository 接口提供了最基本的对实体类的添删改查操作 – T save(T entity); //保存单个实体 – Iterable<T> save(Iterable<? extends T> entities); //保存集合 – T find. One(ID id); //根据id查找实体 – boolean exists(ID id); //根据id判断实体是否存在 – Iterable<T> find. All(); //查询所有实体, 不用或慎用! – long count(); //查询实体数量 – void delete(ID id); //根据Id删除实体 – void delete(T entity); //删除一个实体 – void delete(Iterable<? extends T> entities); //删除一个实体的集合 – void delete. All(); //删除所有实体, 不用或慎用!

Paging. And. Sorting. Repository接口

Paging. And. Sorting. Repository接口

Paging. And. Sorting. Repository • 该接口提供了分页与排序功能 – Iterable<T> find. All(Sort sort); //排序 – Page<T>

Paging. And. Sorting. Repository • 该接口提供了分页与排序功能 – Iterable<T> find. All(Sort sort); //排序 – Page<T> find. All(Pageable pageable); //分页查询 (含排序功能)

Jpa. Repository接口

Jpa. Repository接口

Jpa. Repository • 该接口提供了JPA的相关功能 – List<T> find. All(); //查找所有实体 – List<T> find. All(Sort sort);

Jpa. Repository • 该接口提供了JPA的相关功能 – List<T> find. All(); //查找所有实体 – List<T> find. All(Sort sort); //排序、查找所有实体 – List<T> save(Iterable<? extends T> entities); //保存 集合 – void flush(); //执行缓存与数据库同步 – T save. And. Flush(T entity); //强制执行持久化 – void delete. In. Batch(Iterable<T> entities); //删除一个 实体集合

Jpa. Specification. Executor接口

Jpa. Specification. Executor接口

属性的属性 name 属性 contact Customer 实体类:Root

属性的属性 name 属性 contact Customer 实体类:Root

实际上在使用 Person. Repository 的 test 方 法时,会调用 Person. Repository. Impl 中 test 方法的实现

实际上在使用 Person. Repository 的 test 方 法时,会调用 Person. Repository. Impl 中 test 方法的实现