On spring Data (i)---spring Data overview

Source: Internet
Author: User
Tags apache solr cassandra ldap solr gemfire

  Because I have a project to use more than SQL and NoSQL two different data structures, but in the programming I want the unified interface api, so that the different types of databases can operate in the same programming interface Mode. A spring web site was found, and a spring data project was Discovered. Spring data unifies all interfaces that access the database, providing developers with a more streamlined way to develop it. Spring data website

Here are a few of the core project introductions

    • Spring Data Commons-the Core foundation of each spring data project (so that every spring data project uses a unified Interface)
    • Spring Data Gemfire-provides Simple configuration and access to Gemfire in spring
    • Spring Data jpa-simplifies All JPA Standard database access and Operations (E.G. hibernate mybatis, Etc.)
    • Spring Data KeyValue-simplifies All building of the spring Data module with the map structure database and the SPIs service
    • Spring Data ldap-provides Basic support for spring ldap.
    • Spring Data MongoDB-building a spring-based support and data access component for a document database
    • Spring Data REST-a restful resource that can output a spring DW as a hypermedia form
    • Spring Data redis-provides An easy way to access Redis
    • Spring data for Apache cassandra-apache Cassandra Spring Data module
    • Spring data for Apache solr-apache Solr's Spring Data module

Before spring Data we are going to implement this in the usual way for a database

Let's start by writing an interface that shows what we're going to do with the database, and then using generics (T) to pass in to save the entity type, in hibernate as an example

 public Interface commondao{         // Save entity public         <T> Serializable save (T entity);             // Save or update entity    public        void  saveorupdate (T entity);                     // get an Entity object            public <T> get (class<t> entityname, Serializable id);

     }

But we have to write a specific implementation class, because you just tell the user what to do, but did not tell the backend how to do the addition and deletion check

 publicCommondaoimpl implments commondao{//Hibernate Session@Autowired @Qualifier ("sessionfactory")    Privatesessionfactory sessionfactory;  publicSession getsession () {//the transaction must be open (Required), otherwise it cannot get        returnsessionfactory.getcurrentsession (); }        publicSerializable Save (T Entity) {Try{Serializable ID=getsession (). Save (entity);         GetSession (). Flush (); returnid}Catch(Exception E) {...} } public<T>saveorupdate (T Entity) {//Save or update transaction code    } public<T>get (T entity,serializable id) {//get a single entity code}}

Write a data service interface for easy hiding of specific implementations

 public Interface commonservice{      //  Save entity public       <T> Serializable Save (T Entity) ;         // Save or update         public void saveorupdate (T entity);

Additional data added to the search method}

Write a common data service implementation class

@Service public classcommonserviceimpl{//Data Access Interface@Autowired publicCommondao commondao; //Saving entity implementations      public<T>Serializable Save (T Entity) {//working with DataCommondao.save (entity); }               //Save or update       public<T>voidsaveorupdate (T Entity) {//Save or update an entitycommondao.saveorupdate (entity); }       //Other service methods}

Writing a specified model interface


Publicinterface UserService extends Commonservice

Inherit all additions and deletions to the parent class interface
New interface
Public

Public list<user> getuserbyusername (String username);
//..... Other user methods
}

@service // annotation for component injection for sprign management of Interfaces

 public classUserserviceimpl extends Commonserviceimpl implments commonservice{               /**
* Override parent class to save entity method
**/ public<T> Serializable Save (T Entity) {//pre-business processing for user..... this.save (User entity);//Save User//back to save information}

/**
* Get a single User object
**/
Public User GetUser (String Id) {
This.get (user.class,id);
}

/**
* Custom User Search
**/
Public User Getuserbyusername (String Username) {

Custom implementations
HQL implementation
String hql = "from User u where U.password =?" "
list<user> users = this.querybyhql (password);
To determine that the users are not present
If (users!=null&&users.size>0)
return Users.get (0);

Return null;

}





}

That's when we're done with a complete data service, and WE'LL review this coding step

1. Write Data access Layer common interface

2, write the data Access Layer universal implementation

3. Write a common service interface

4. Write Generic Service implementations (including custom queries, custom HQL or package Criteria)

5. Convert to Pojo

The above is the traditional Java EE crud coding process.

Next we're going to introduce our main character today, Spring-data.

Reading the above a bunch of code is not really annoying, why? Because I have to write the interface, but also write the implementation, write interface to tell the customer what to do, and then write an implementation tells the Java virtual machine what to do. There is a need to increase the engineering nature of Java (using interfaces to separate the implementation from the behavior itself), reducing the User's cost of use and increasing the maintainability and extensibility of the Software. For a large and complex system, this is a robust hair-making. But this undoubtedly adds to the cost of Development. 1, the interface and the implementation of its own code volume Increased. 2, The requirements and design is not clear, to the interface extension, in fact, the implementation must also be Expanded. The so-called interface is the life gate of Java engineering. Interface makers are often the most experienced engineers. Because these things really affect the efficiency. So Spring-data was Born. Spring-data means that you just need to tell spring in the interface what you want to query, what kind of query you want, and it will do it for YOU. You don't have to write query statements Yourself. No more complicated queries, if you query this person based on your last name, you just have to write, it's like Magic. Get to an object list

Interface extends repository<person, long> {  List<Person> findbylastname (String lastname);}

You don't have to write your DAO implementation, goodbye hql, Goodbye sql, goodbye to the criteria, everything came so Concise. What does spring data do for us? Let's review the history of the interaction between the database and Java.

Can see our database operation to solve the problem of an approximate course, Spring-data realized an important step, the implementation of the various ORM framework is hidden, the query rules and query interface unified, is the implementation of the interface to define the database query implementation, And you don't have to focus on which ORM framework you're using, because the query model (using the unified Interface) and the return model are all unified, and orm, the query wrapper, spring-data all of them for you.

The following are the interface rules for Spring-data

Logical keyword

Keyword Expressions

AND

And

OR

Or

AFTER

After,IsAfter

BEFORE

Before,IsBefore

CONTAINING

Containing, IsContaining,Contains

BETWEEN

Between,IsBetween

ENDING_WITH

EndingWith, IsEndingWith,EndsWith

EXISTS

Exists

FALSE

False,IsFalse

GREATER_THAN

GreaterThan,IsGreaterThan

GREATER_THAN_EQUALS

GreaterThanEqual,IsGreaterThanEqual

IN

In,IsIn

IS

Is, Equals , (or No Keyword)

IS_NOT_NULL

NotNull,IsNotNull

IS_NULL

Null,IsNull

LESS_THAN

LessThan,IsLessThan

LESS_THAN_EQUAL

LessThanEqual,IsLessThanEqual

LIKE

Like,IsLike

NEAR

Near,IsNear

NOT

Not,IsNot

NOT_IN

NotIn,IsNotIn

NOT_LIKE

NotLike,IsNotLike

REGEX

Regex, MatchesRegex,Matches

STARTING_WITH

StartingWith, IsStartingWith,StartsWith

TRUE

True,IsTrue

WITHIN

Within,IsWithin

On spring Data (i)---spring Data overview

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.