SPRING-DATA-JPA How to use

Source: Internet
Author: User
Tags min xmlns

Transferred from: http://blog.csdn.net/linlinv3/article/details/46605719

SPRING-DATA-JPA How to use


1. What is Spring-data

To simplify the code for program-to-database interaction, Spring provides a ready-made DAO layer framework that the Spring family provides spring-data for relational and NoSQL databases;

For example, spring data JPA, spring data Hadoop, spring data MongoDB, spring data Solr, and so on;

Specific can refer to the official website: http://projects.spring.io/spring-data/;

Their common feature is to provide us with the framework code, spring data can automatically create the entity DAO implementation class and custom query, no longer need our own to implement.

API Address: http://jpa.coding.io/Chinese

http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/English


2. What is JPA.

JPA is all called the Java Persistence API (Java Persistence API), JPA is one of the Java EE 5 standards, is an ORM specification, implemented by vendors, currently has hibernate, OpenJPA, TopLink, ECLIPSEJPA and other realization;

3. How do I use JPA?

To provide us with additions and deletions to check the interface:

One, query: There are many ways to query, I do not enumerate, often used here to enumerate:

1, query all data findAll ()

2, paging query FindAll (new Pagerequest (0, 2))

3, according to the ID query FindOne ()

4, according to the entity class attribute query: Findbyproperty (Type property); For example: findbyage (int age);

5, Sort: findAll (sort)

Sort sort = new sort (Sort.Direction.DESC, "age"). and (New sort (Sort.Direction.DESC, "id"));

6, conditional query and/or/findbyagelessthan/lessthanequal and so on,

Example: Findbyusernameandpassword (string Username, string password)

7, Total query count () or the total number of queries based on a property countbyage (int age);

8, whether there is an ID exists () Two, modify, delete, add New: Directly using the Save (T) method Delete: Delete () or Deletebyproperty for example: deletebyage (int age); Update: @Modifying @Query ("Update Customer u set u.age =? 1 where u.id =? 2") int update (int age1, long ID);

The official website has written all the methods, very detailed, here a little list

and = are equivalent to the AND keyword in SQL for example: Findbyusernameandpassword (String user, Striang pwd);

or = is equivalent to the OR keyword in SQL, for example: Findbyusernameoraddress (string user, String addr);

between = is equivalent to the between keyword in SQL, for example: Salarybetween (int max, int min);

LessThan = is equivalent to "<" in SQL, for example: Findbysalarylessthan (int max);

GreaterThan = is equivalent to ">" in SQL, for example: Findbysalarygreaterthan (int min);

IsNull = is equivalent to "is null" in SQL, for example: Findbyusernameisnull ();

Isnotnull = is equivalent to "is not NULL" in SQL, for example: Findbyusernameisnotnull ();

Notnull=> and isnotnull are equivalent;

like = = is equivalent to "as" in SQL, for example: Findbyusernamelike (String user);

Notlike = is equivalent to "not like" in SQL, for example: Findbyusernamenotlike (String user);

"Order BY" in SQL, for example: FINDBYUSERNAMEORDERBYSALARYASC (String user);

not = is equivalent to "in SQL." = ", for example: Findbyusernamenot (String user);

in = is equivalent to "in" in SQL, for example: Findbyusernamein (collection<string> userlist), the parameter of the method can be a Collection type, or it can be an array or an indefinite length parameter;

Notin = is equivalent to ' not in ' in SQL, for example: Findbyusernamenotin (collection<string> userlist), the parameter of a method can be a Collection type, or it can be an array or indefinite length parameters;

Create a Sort object sorted by single field: New Sort (Sort.Direction.DESC, "description"). and (New sort (Sort.Direction.ASC, "id"))



According to the official website of the demo slightly modified to organize, as follows:

Using maven:

Pom.xml as follows:

<?xml version= "1.0"  encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/ 4.0.0 "    xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/maven-v4_0_0.xsd "    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">     <modelVersion>4.0.0</modelVersion>     <groupid >org.springframework</groupId>     <artifactid>gs-accessing-data-jpa</ Artifactid>     <version>0.1.0</version>     <parent >         <groupId>org.springframework.boot</groupId>         <artifactid>spring-boot-starter-parent</artifactid >         <version>1.2.4.RELEASE</version>      </parent>    <properties>         <java.version> 1.8</java.version>     </properties>     <dependencies >         <dependency>              <groupId>org.springframework.boot</groupId>              <artifactid>spring-boot-starter-data-jpa</ Artifactid>         </dependency>          <dependency>              <groupId>com.h2database</groupId>              <artifactId>h2</artifactId>         </ Dependency> &NBSP;&NBSp;  </dependencies>     <build>          <plugins>             <plugin >                 < Groupid>org.springframework.boot</groupid>                  <artifactId>spring-boot-maven-plugin</artifactId>              </plugin>          </plugins>     </build>     < Repositories>         <repository>              <id>spring-releases</id>        

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.