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都可以自动实现
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>)的更多相关文章
- 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 ...
- 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 ...
- 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; /** * ...
- 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. ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- 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 ...
- 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 ...
- 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 ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
随机推荐
- 编译QT时出现lib/libQtGui.so: undefined reference to `ts_read_raw'的解决办法
lib/libQtGui.so: undefined reference to `ts_read_raw' /lib/libQtGui.so: undefined reference to `ts_o ...
- JAVA类与对象(七)------继承
理解:继承可以理解为一个对象获取属性的过程.如果类A是类B的父类,而类B是类C的父类,我们也称C是A的子类,类C是从类A继承而来. 在java中,类的继承是单一继承,也就是说,一个子类只能拥有一 ...
- 【AFNetworking】AFNetworking源码阅读(一)
1. 前言 2. iOS Example代码结构 3.AFNetworkActivityIndicatorManager 4. UIRefreshControl+AFNetworking 5. AFN ...
- SQL Server 2008 没有可用于 charge_sys_Log.LDF 的编辑器
因为上网问题重新装了系统.今天在整 SQL Server 的时候出现了这样一个问题. 因为之前装 SQL Server 的时候没有遇到过这种情况,感觉很新奇.所以详细的记录一下,希望对别人能有一定 ...
- Notes of the scrum meeting(12.7)
meeting time:18:30~19:10p.m.,December 7th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- iOS UIApplicatin和它的delegate
每一个UIApplication代表一个应运程序,而且UIApplication是个单例类: ios程序一旦启动,创建的第一个对象就是UIApplication对象: // 拿到UIApplica ...
- Codeforces Round #265 (Div. 1) C. Substitutes in Number dp
题目链接: http://codeforces.com/contest/464/problem/C J. Substitutes in Number time limit per test 1 sec ...
- Ext通过后台校验字段是否重复
话不多说,直接上代码: handlerRybh : function(textField) { Ext.Ajax.request({// ajax请求的方法 url : 'userManage/per ...
- thinkPHP生成静态分页列表
改造分页类Pagehtml.class.php <?php // 静态分页列表类 class Pagehtml extends Think { //分页url public $pageUrl; ...
- ACM入门记
[2015.12]零基础进队 [2016.4.10]浙大第十六届程序设计大赛 [2016.6.4]团体程序设计天梯赛初赛 [2016.7.16]团体程序设计天梯赛决赛 赛后总结:比赛的时候好慌,一道题 ...