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

一、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" />
时间: 2024-12-28 00:22:16

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-002设置JPA的EntityManagerFactory(&lt;persistence-unit&gt;、&lt;jee:jndi-lookup&gt;)

一.EntityManagerFactory的种类 1.The JPA specification defines two kinds of entity managers: ? Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity manage

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-005Spring-Data-JPA例子的代码

一.结构 二.Repository层 1. 1 package spittr.db; 2 3 import java.util.List; 4 5 import org.springframework.data.jpa.repository.JpaRepository; 6 7 import spittr.domain.Spitter; 8 9 /** 10 * Repository interface with operations for {@link Spitter} persistenc

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一.Spring支持的View Resolver 二.InternalResourceViewResolver Spring supports JSP views in two ways:? InternalResourceViewResolver ? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features. 1.在java中定义

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's Lonely Hearts Club Band", "The Beatles"); } < bean id = "sgtPeppers" class = "soundsystem.BlankDisc" c: _title = &quo

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main-tain some state and therefore isn’t safe for reuse. In that case, declaring the class as asingleton bean probably isn’t a good idea because that obje

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field=&quot;*{firstName}&quot;)

一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three beans that enable Thymeleaf-Spring integration:? A ThymeleafViewResolver that resolves Thymeleaf template views from logical view names? A SpringTempl

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model

一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 1 package spittr.web; 2 3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springfra