1、數(shù)據(jù)訪問(wèn)
1.1 數(shù)據(jù)訪問(wèn)
- spring data規(guī)范了API進(jìn)行數(shù)據(jù)訪問(wèn)操作
- spring data repository抽象:數(shù)據(jù)訪問(wèn)操作的統(tǒng)一標(biāo)準(zhǔn)
- 不同的訪問(wèn)技術(shù)提供不同的repository,如Spring data JPA有jpaRepository
2、Spring Data JPA
2.1 認(rèn)識(shí)
O/R映射:object-relational mapping:將模型類和數(shù)據(jù)庫(kù)表映射,操作對(duì)象以操作表數(shù)據(jù)
2.2 定義
- 繼承JpaRepository的接口
- spring環(huán)境中,添加@EnableJpaRepositories開(kāi)啟spring data jpa支持,參數(shù)掃描數(shù)據(jù)訪問(wèn)層報(bào)下的數(shù)據(jù)訪問(wèn)的接口定義
2.3 定義查詢方法
2.3.1 常規(guī)查詢
- 屬性名查詢:findByXxx,findByXxxLike,findByXxxAndYyy
findBy->find、read、readBy、query、queryBy、get、getBy
and,or,is,equals,between,lessthen,lessthanequal,greaterthen,greaterthenequal,after,before,isnull,isnotnull,notnull,like,notlike,startingwith,endingwith,containing,orderby,not,in,notin,true,false,ignorecase
2.3.2 限制結(jié)果數(shù)量
findFirst10ByName:獲得符合條件的前10條數(shù)據(jù)
findTop30ByName:前30條數(shù)據(jù)
2.3.3 NamedQuery查詢
- 在entity中注解查詢方法
@NamedQuery(name = "Person.findByName",query = "select p from Person p where p.name = ?1")
- 在Repository中定義方法
List<Person> findByName(String name);
2.3.4 @Query注解查詢
查詢參數(shù):=?1 -> 第一個(gè)參數(shù)
或者 = :name -> 名字為name的參數(shù)
2.3.5 更新查詢
@Modifying @Transactional @Query("update Person p set p.name=?1") int setName(String name);
2.3.5 Specification:基于準(zhǔn)則的查詢:Criteria查詢
繼承JpaSpecificationExecutor接口,重寫toPredicate方法來(lái)構(gòu)造查詢條件
public static Specification<Person> personFromHefei() { @Override public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return cb.equal(root.get("address"), "合肥"); } }; List<Person> people = personRepository.findAll(personFromHefei());
CriteriaBuilder包含的條件:
exists,and,or,not,conjunction,disjunction,isTrue,isFalse,isNull,isNotNull,equal,notEqual,greaterThan,greaterThanOrEqualTo,lessThan,lessThanOrEqualTo,between
2.3.6 排序與分頁(yè)
sort類,Page接口,Pageable接口
- 使用排序:
List<Person> people = personRepository.findByName("xx", new Sort(Direction.ASC,"age"));
- 使用分頁(yè):
Page<Person> people = presonRepository.findByName("xx", new PageRequest(0, 10));
2.4 自定義Repository的實(shí)現(xiàn)
2.4.1 定義接口
@NoRepositoryBean //指明當(dāng)前接口不是領(lǐng)域類的接口 public interface CustomRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> { //使得具備分頁(yè)和排序的能力 public void doSomething(ID id); //定義的數(shù)據(jù)操作方法 }
2.4.2 定義接口實(shí)現(xiàn)
public class CustomRepositoryImpl <T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements CustomRepository<T, ID> { //繼承simplejparepoisitory使用findAll等方法 private final EntityManger entityManager; //獲取entityManger public CustomRepositoryImpl(Class<T> domainClass, EntityManager entityManger) { // super(domainClass, entityManger); this.entityManager = entityMamager; } @Override public void doSomething(ID id) { // } }
2.4.4 自定義RepositoryFactoryBean,獲得RepositoryFactory,從而注冊(cè)自定義的Repository實(shí)現(xiàn)
3、SpringBoot的數(shù)據(jù)支持
在類路徑下放置 schema.sql
文件,會(huì)自動(dòng)初始化表數(shù)據(jù)
在類路徑下放置 data.sql
文件會(huì)自動(dòng)填充表數(shù)據(jù)
3.1 JPA自動(dòng)配置文件:org.springframework.boot.autoconfigure.orm.jpa
Spring Data JPA的自動(dòng)配置:
org.springframework.boot.autoconfigure.data.jpa
3.2 hbiernate的維護(hù)表結(jié)構(gòu)的選項(xiàng)
- create:?jiǎn)?dòng)時(shí)刪除上次的表
- create-drop:?jiǎn)?dòng)時(shí)根據(jù)實(shí)體類生成表,sessionFactory關(guān)閉時(shí)刪除表
- update:?jiǎn)?dòng)時(shí)根據(jù)實(shí)體生成表,實(shí)體類屬性變動(dòng)時(shí),表結(jié)構(gòu)也更新
-validate:?jiǎn)?dòng)時(shí)檢查實(shí)體和數(shù)據(jù)表是否一致
-none:
其他
spring.jpa.show-sql = true
spring.jackson.serialization.indent_output=true //控制器輸入更美觀的json字符串