一、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都可以自动实现

 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" />

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

  1. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-003编写JPA-based repository( @PersistenceUnit、 @PersistenceContext、PersistenceAnnotationBeanPostProcessor)

    一.注入EntityManagerFactory的方式 package com.habuma.spittr.persistence; import java.util.List; import jav ...

  2. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)

    一.EntityManagerFactory的种类 1.The JPA specification defines two kinds of entity managers:  Applicatio ...

  3. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)

    一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...

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

    一.结构 二.Repository层 1. package spittr.db; import java.util.List; import org.springframework.data.jpa. ...

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

    一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...

  6. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)

    一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...

  7. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice

    No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...

  8. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver

    一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...

  9. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener

    一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

随机推荐

  1. 内核中的 likely() 与 unlikely()

    内核中的 likely() 与 unlikely() 在 2.6 内核中,随处可以见到 likely() 和 unlikely() 的身影,那么为什么要用它们?它们之间有什么区别? 首先要明确: if ...

  2. skill-判断浏览器

    判断是ie浏览器还是火狐等标准浏览器 var ie=!+"\v1"; 因为ie浏览器不支持\v,也就是水平制表符,所以"\"符号会被忽略,前面的+号是把&quo ...

  3. [网络配置相关]——netstat命令

    netstat:显示网络状态信息 -a   显示所有连接状态的网络的所有选项-l    仅显示LISTEN状态的连接-n   直接显示IP地址,而不通过域名服务器-p   把进程名和进程PID也显示出 ...

  4. C++ 学习笔记(一)

    只是记录自己学习C++ 笔记实例来自<c++ primer> 1.static: static 局部对象确保不迟于在程序执行流程第一次经过该对象的定义语句时进行初始化.这种对象一旦被创建, ...

  5. SQL Server Reporting Services – Insufficient Rights Error

    http://www.sql-server-performance.com/2011/security-ssrs-reporting-error/ SQL Server Reporting Servi ...

  6. jQuery中ready与load事件

    jQuery中ready与load事件(来自慕课网) jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //docume ...

  7. HTML统一资源定位器

    w3c 更好的解释 在OSGi in action中安装bundle时要加文本传输协议,要不然shell判断不出来

  8. Linux常用命令查看日志

    cattail -f日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关的日 ...

  9. android 通过socket获取IP

    如题<android 通过socket获取IP>: socket.getInetAddress().getHostAddress();

  10. EntityFramework走马观花之CRUD(上)

    对于任何一个ORM框架,CRUD都是其核心功能,可以这么说,CRUD功能实现得好坏,直接决定了此ORM框架的命运. CRUD是英文Create.Read.Update.Delete四个单词的缩写,对应 ...