SPRING in ACTION 4th Note-11th chapter persisting data with Object-relational Mapping-006spring-data run rules (@EnableJpaRepositories, <jpa:repositories>)

Source: Internet
Author: User

First, Jparepository

1. To enable spring to automatically generate the implementation class

(1) configuration file 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/JPA3 http:// www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd "> 4     <jpa:repositories base-package = "com.habuma.spittr.db"/> ... 5 </beans>

or Java

1 @Configuration 2 @EnableJpaRepositories (basepackages= "com.habuma.spittr.db")3 Public Class  jpaconfiguration {4    ... 5 }

(2) DAO interface Inherits Jparepository interface

1  Public Interface extends Jparepository<spitter, long> {23 }

2. When the DAO interface inherits the Jparepository interface and the @enablejparepositories is set, Spring automatically generates the implementation class

inheriting jparepository indirectly inherits the Repository interface, while @enablejparepositories and <jpa:repositories> scans Its-base package for any interfaces this 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.

Ii. naming rules for methods

1.Spring is how to decide how to implement the interface method?

The following naming rules are the methods that spring can automatically implement

1  Public InterfaceSpitterrepositoryextendsJparepository < Spitter, Long > {2 spitter Findbyusername (String username);3 readspitterbyfirstnameorlastname ();4List<spitter>Readbyfirstnameorlastname (string First, String last);5List<spitter>readbyfirstnameignoringcaseorlastnameignorescase (string First, String last);6List<spitter>readbyfirstnameorlastnameallignorescase (string First, String last);7List<spitter>READBYFIRSTNAMEORLASTNAMEORDERBYLASTNAMEASC (string First, String last);8List<spitter>Readbyfirstnameorlastnameorderbylastnameascfirstnamedesc (string First, String last);9List<pet> Findpetsbybreedin (list<string>breed)Ten?intcountproductsbydiscontinuedtrue () One? List<order>Findbyshippingdatebetween (date start, date end) A}

Third, the use of @query

When the automatic implementation does not meet the requirements, consider using @query

1 @Query ("Select S from Spitter s where s.email like '%gmail.com '")2 list<spitter> Finda Llgmailspitters ();

Iv. Hybrid Custom Implementation method

1.whenSpring 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. Can change the scan suffix

1 @EnableJpaRepositories (basepackages= "com.habuma.spittr.db", repositoryimplementationpostfix= "Helper")

or XML mode

<jpa:repositories base-package = "Com.habuma.spittr.db" repository-impl-postfix= "Helper"/>

SPRING in ACTION 4th Note-11th chapter persisting data with Object-relational Mapping-006spring-data run rules (@EnableJpaRepositories, <jpa:repositories>)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.