zoukankan      html  css  js  c++  java
  • 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)
    10int 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" />
  • 相关阅读:
    VM安装CentOs7虚拟机后无法上网之解决方法
    vue中touchEnd事件和touchStart、touchMove获取坐标不一样,IOS绑定touch事件失效
    【个推独家】让你一次性掌握Neo4j性能优化秘籍的三大狠招
    【深度干货】异构数据的SQL一站式解决方案
    个推基于Jenkins的自动打包构建实践
    个推CTO叶新江专访| 数据智能的未来,是不提大数据但其无所不在的时代
    应用系统间数据传输方式总结(附相关概念解释)
    JavaScript中“&&”和“||”操作符的意义,深入理解和使用场景
    静态布局、自适应布局、流式布局、响应式布局、弹性布局等的概念和区别
    前端跨域问题相关知识详解(原生js和jquery两种方法实现jsonp跨域)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5345877.html
Copyright © 2011-2022 走看看