Persisting data with object-relational mapping-006Spring-Data的运行规则(@Enabl

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Persisting data with object-relational mapping-006Spring-Data的运行规则(@Enabl相关的知识,希望对你有一定的参考价值。

一、JpaRepository

1.要使Spring自动生成实现类的步骤

(1)配置文件xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/data/jpa
3 http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
4     <jpa:repositories base-package="com.habuma.spittr.db" /> ...
5 </beans>

或java

1 @Configuration
2 @EnableJpaRepositories(basePackages="com.habuma.spittr.db")
3 public class JpaConfiguration {
4     ...
5 }

 

(2)dao接口继承JpaRepository接口

1 public interface SpitterRepository extends JpaRepository<Spitter, Long> {
2 
3 }

 

2.为什么dao接口继承JpaRepository接口及设置好@EnableJpaRepositories后,Spring就会自动生成实现类

 

继承JpaRepository则会间接继承Repository接口,而@EnableJpaRepositories和<jpa:repositories> scans its base package for any interfaces that extend Spring Data JPA ’s Repository interface.When it finds any interface extending Repository , it automatically (at applicationstartup time) generates an implementation of that interface.

 

二、方法的命名规则

1.Spring是如何决定接口的方法要如何实现?

技术分享

 

技术分享

如下命名规则的方法,Spring都可以自动实现

 1 public interface SpitterRepository extends JpaRepository < Spitter, Long > {
 2     Spitter findByUsername(String username);
 3     readSpitterByFirstnameOrLastname() ;
 4     List<Spitter> readByFirstnameOrLastname(String first, String last);
 5     List<Spitter> readByFirstnameIgnoringCaseOrLastnameIgnoresCase(String first, String last);
 6     List<Spitter> readByFirstnameOrLastnameAllIgnoresCase(String first, String last);
 7     List<Spitter> readByFirstnameOrLastnameOrderByLastnameAsc(String first, String last);
 8     List<Spitter> readByFirstnameOrLastnameOrderByLastnameAscFirstnameDesc(String first, String last);
 9     List<Pet> findPetsByBreedIn(List<String> breed)
10 ?  int countProductsByDiscontinuedTrue()
11 ?  List<Order> findByShippingDateBetween(Date start, Date end)
12 }

 

三、使用@Query

当自动实现不能满足要求进,考虑用@Query

1 @Query("select s from Spitter s where s.email like ‘%gmail.com‘")
2 List<Spitter> findAllGmailSpitters();

 

四、混合自定义实现方法

1.When Spring Data JPA generates the implementation for a repository interface, it also looks for a class whose name is the same as the interface’s name postfixed with Impl . If the class exists, Spring Data JPA merges its methods with those generated by Spring Data JPA . For the SpitterRepository interface, the class it looks for is named SpitterRepositoryImpl

2.可以改变扫描的后缀

 

1 @EnableJpaRepositories(basePackages="com.habuma.spittr.db",repositoryImplementationPostfix="Helper")

 

或xml方式

<jpa:repositories base-package="com.habuma.spittr.db" repository-impl-postfix="Helper" />

 

以上是关于Persisting data with object-relational mapping-006Spring-Data的运行规则(@Enabl的主要内容,如果未能解决你的问题,请参考以下文章

[]Python][simple]Serialize data with Pickle and deserialize data from pickle

7.5 Models -- Persisting Records

[Redux] Persisting the State to the Local Storage

nginx编译增加模块

QT在Ubuntu18中persisting cmake state

QT在Ubuntu18中persisting cmake state