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" />
  • 相关阅读:
    算法生成卐和卍字图
    分形之可编辑折线
    算法生成太极八卦图
    通过算法生成一幅太极图
    使用异或运算对数据及文件进行加密处理,附软件及源码
    C语言中将0到1000的浮点数用强制指针类型转换的方式生成一幅图像
    算法之美---100幅由程序生成的图像,总有一幅让你感到惊艳[下]
    【python基础学习】基础重点难点知识汇总
    【深入学习linux】在linux系统下怎么编写c语言程序并运行
    【深入学习linux】CentOS 7 最小化安装后程序必须安装的组件
  • 原文地址:https://www.cnblogs.com/shamgod/p/5345877.html
Copyright © 2011-2022 走看看