SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、)

时间:2023-03-08 15:50:08
SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、<jpa:repositories>)

一、JpaRepository

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

(1)配置文件xml

 <?xml version="1.0" encoding="UTF-8"?>
<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
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<jpa:repositories base-package="com.habuma.spittr.db" /> ...
</beans>

或java

 @Configuration
@EnableJpaRepositories(basePackages="com.habuma.spittr.db")
public class JpaConfiguration {
...
}

(2)dao接口继承JpaRepository接口

 public interface SpitterRepository extends JpaRepository<Spitter, Long> {

 }

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 IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、<jpa:repositories>)

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、<jpa:repositories>)

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

 public interface SpitterRepository extends JpaRepository < Spitter, Long > {
Spitter findByUsername(String username);
readSpitterByFirstnameOrLastname() ;
List<Spitter> readByFirstnameOrLastname(String first, String last);
List<Spitter> readByFirstnameIgnoringCaseOrLastnameIgnoresCase(String first, String last);
List<Spitter> readByFirstnameOrLastnameAllIgnoresCase(String first, String last);
List<Spitter> readByFirstnameOrLastnameOrderByLastnameAsc(String first, String last);
List<Spitter> readByFirstnameOrLastnameOrderByLastnameAscFirstnameDesc(String first, String last);
List<Pet> findPetsByBreedIn(List<String> breed)
 int countProductsByDiscontinuedTrue()
 List<Order> findByShippingDateBetween(Date start, Date end)
}

三、使用@Query

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

 @Query("select s from Spitter s where s.email like '%gmail.com'")
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.可以改变扫描的后缀

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

或xml方式

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