Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Spring Data
Oleksii Usyk
Contents
• Spring Data overview
• Core functionality
– Repository support
– Templating
– Object/Datastore mapping

• Sprin...
Spring Data
• Spring Data is high level SpringSource project
• Purpose:
– Ease the work with persistence stores
– Support ...
Spring Data project consists of:
Structure
Programming model
Client

Repository
Template
Object Mapping

Datastore
Templating
The main purpose of all Spring templates is
resource allocation and exception
translation.
Spring Data:
Resours...
Implementations of Templates
Store specific implementations:
• MongoTemplate
• EntityManager – acts like template for JPA
...
Object/Datastore mapping
{store}MappingConverter with default configuration
is created implicitly:
Mapping is based on:
– ...
Object/Datastore mapping
GemFire

References to other objects
Repository
The goal of Spring Data repository abstraction is
to significantly reduce the amount of boilerplate
code requir...
Cooking Spring Data

1) Declare an interface extending Repository or one of its
subinterfaces and type it to the domain cl...
How it works?
• Spring Data provides persistence store specific
implementations for all interfaces that extend
Repository....
Query creation
Defining query methods
• The repository proxy has two ways to derive a
store-specific query from the method name:
– Method...
Custom implementations
1. Define an interface

2. Repository interface should extend the custom interface:
3. Implement th...
Configuration
Spring Data Web support
• Integration with Spring MVC
• No need to write boilerplate code in
controllers
• Spring Data RES...
Web Support configuration

Following components will be registered in basic (not HATEOAS) case:
• DomainClassConverter: en...
Web Support examples
• DomainClassConverter usage:

• PageableHandlerMethodArgumentResolver and
SortHandlerMethodArgumentR...
Repository populators
• Can specify data to populate storage independent format:
JSON and XML

• JSON populator configurat...
JPA support
Provides enhanced support for JPA based data
access layers. It makes it easier to build
Spring-powered applica...
MongoDB support
MongoDB (from "humongous") is an opensource document database, and the leading
NoSQL database written in C...
GemFire support
vFabric GemFire is a distributed data management platform
providing dynamic scalability, high performance,...
Practice
Questions
Useful links
Spring Data
•
•
•
•
•
•
•

http://projects.spring.io/spring-data/
http://spring.io/guides/tutorials/data/
htt...
That’s all folks
Thank you for your attention!
Spring Data MongoDB features
• Spring configuration support using Java based @Configuration classes or an XML namespace fo...
Mongo Configuration
MongoTemplate
• The class MongoTemplate is the central class of the Spring's
MongoDB support providing a rich feature set ...
MongoDB type mapping
Java Person class

Document in mongo “prs” collection
Mapping annotations overview
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

The MappingMongoConverter can use metadata to driv...
Custom Converters
MongoTemplate features
• MapReduce operations support

• Group operations support

• Aggregation Framework support (introd...
Repository
MongoDB JSON based query
methods and field restriction
@org.springframework.data.mongodb.repository.Query

Simple querying...
QueryDSL and type-safe query methods
MongoDB repository support integrates with the QueryDSL
project which provides a mean...
QueryDSL and type-safe query methods
1) QueryDslPredicateExecutor
interface is provided for you by
Spring Data

2) Just ma...
Miscellaneous
• Cross store support
• Logging support
• JMX support
Spring data presentation
Spring data presentation
Spring data presentation
Spring data presentation
Spring data presentation
Próxima SlideShare
Cargando en…5
×

Spring data presentation

2.144 visualizaciones

Publicado el

Presentation about main features about framework.

Publicado en: Tecnología
  • Sé el primero en comentar

Spring data presentation

  1. 1. Spring Data Oleksii Usyk
  2. 2. Contents • Spring Data overview • Core functionality – Repository support – Templating – Object/Datastore mapping • Spring data extensions: – Web support – Repository populations • Practice
  3. 3. Spring Data • Spring Data is high level SpringSource project • Purpose: – Ease the work with persistence stores – Support for new data access technologies – Improve support for RDBMS – Unify the access to different kinds of persistence stores, both RDBMS and NoSQL* data stores.
  4. 4. Spring Data project consists of:
  5. 5. Structure
  6. 6. Programming model Client Repository Template Object Mapping Datastore
  7. 7. Templating The main purpose of all Spring templates is resource allocation and exception translation. Spring Data: Resourse - datastore ExceptionTranslation – from datastore driver specific exceptions to well known unchecked DataAccessException hierarchy
  8. 8. Implementations of Templates Store specific implementations: • MongoTemplate • EntityManager – acts like template for JPA • GemfireTemplate • Neo4jTemplate • RedisTemplate • …
  9. 9. Object/Datastore mapping {store}MappingConverter with default configuration is created implicitly: Mapping is based on: – Convention – Metadata Explicit conversion {store}MappingConverter can be configured explicitly via both XML and Java
  10. 10. Object/Datastore mapping GemFire References to other objects
  11. 11. Repository The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. org.springframework.data.repository.Repository org.springframework.data.repository.CrudRepository org.springframework.data.repository.PagingAndSortingRepository
  12. 12. Cooking Spring Data 1) Declare an interface extending Repository or one of its subinterfaces and type it to the domain class: 2) Declare query methods on the interface: 3) Set up Spring to create proxy instances for those interfaces: 4) Get the repository instance injected and use it:
  13. 13. How it works? • Spring Data provides persistence store specific implementations for all interfaces that extend Repository. • … no need to write an implementation? simple cases – yes more complex operations – require some additional code Benefits: • no boilerplate for simple CRUD • full control on persisting
  14. 14. Query creation
  15. 15. Defining query methods • The repository proxy has two ways to derive a store-specific query from the method name: – Method name – Additionally created query • Strategies: • CREATE • USE_DECLARED_QUERY • CREATE_IF_NOT_FOUND (default)
  16. 16. Custom implementations 1. Define an interface 2. Repository interface should extend the custom interface: 3. Implement the defined interface with the custom functionality 4. Put custom interface implementation in the appropriate place:
  17. 17. Configuration
  18. 18. Spring Data Web support • Integration with Spring MVC • No need to write boilerplate code in controllers • Spring Data REST project provides a solid foundation on which to expose CRUD operations to your JPA Repository-managed entities using plain HTTP REST semantics.
  19. 19. Web Support configuration Following components will be registered in basic (not HATEOAS) case: • DomainClassConverter: enables Spring MVC to resolve instances of repository managed domain classes from request parameters or path variables. • HandlerMethodArgumentResolver implementations to let Spring MVC resolve Pageable and Sort instances from request parameters.
  20. 20. Web Support examples • DomainClassConverter usage: • PageableHandlerMethodArgumentResolver and SortHandlerMethodArgumentResolver usage: Request Parameter Description page Page you want to retrieve. size Size of the page you want to retrieve. sort Properties that should be sorted by in the format property,property(,ASC| DESC).
  21. 21. Repository populators • Can specify data to populate storage independent format: JSON and XML • JSON populator configuration: • XML populator configuration:
  22. 22. JPA support Provides enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.
  23. 23. MongoDB support MongoDB (from "humongous") is an opensource document database, and the leading NoSQL database written in C++. Spring Data MongoDB works with • MongoDB 1.4 or higher • Java SE 5 or higher.
  24. 24. GemFire support vFabric GemFire is a distributed data management platform providing dynamic scalability, high performance, and database-like persistence. • JDK > 6.0 • Spring Framework 3 • vFabric GemFire > 6.6
  25. 25. Practice
  26. 26. Questions
  27. 27. Useful links Spring Data • • • • • • • http://projects.spring.io/spring-data/ http://spring.io/guides/tutorials/data/ http://docs.spring.io/spring-data/ http://www.infoq.com/articles/spring-data-intro http://www.slideshare.net/mcgray/spring-data-new-approach-to-persistence http://www.odbms.org/blog/2013/01/the-spring-data-project-interview-with-david-turanski/ http://spring.io/guides/gs/accessing-data-mongo/ JPA http://www.spring-source.ru/docs_simple.php?type=manual&theme=docs_simple&docs_simple=chap03_p02 Mongo http://docs.mongodb.org/manual/ GemFire http://community.gemstone.com/display/gemfire/GemFire+Tutorial
  28. 28. That’s all folks Thank you for your attention!
  29. 29. Spring Data MongoDB features • Spring configuration support using Java based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets • MongoTemplate helper class that increases productivity performing common Mongo operations. Includes integrated object mapping between documents and POJOs. • Exception translation into Spring's portable Data Access Exception hierarchy • Feature Rich Object Mapping integrated with Spring's Conversion Service • Annotation based mapping metadata but extensible to support other metadata formats • Persistence and mapping lifecycle events • Java based Query, Criteria, and Update DSLs • Automatic implementation of Repository interfaces including support for custom finder methods. • QueryDSL integration to support type-safe queries. • Cross-store persistence - support for JPA Entities with fields transparently persisted/retrieved using MongoDB • Log4j log appender • GeoSpatial integration
  30. 30. Mongo Configuration
  31. 31. MongoTemplate • The class MongoTemplate is the central class of the Spring's MongoDB support providing a rich feature set to interact with the database. • Provides wide range of convenience CRUD operations for MongoDB documents • Provides a mapping between domain objects and MongoDB documents. • Provides similar functionality to MongoDB driver methods but also gracefully handles mapping for you. (Using pure driver methods you have to deal with DBObject instead of your domain object.)
  32. 32. MongoDB type mapping Java Person class Document in mongo “prs” collection
  33. 33. Mapping annotations overview • • • • • • • • • • • • • • • • • • • • • The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below @Id - applied at the field level to mark the field used for identiy purpose. @Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored. @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef. @Indexed - applied at the field level to describe how to index the field. @CompoundIndex - applied at the type level to declare Compound Indexes @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field. @Transient - by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database @PersistenceConstructor - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject. @Value - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: @Value("#root.myProperty") where root refers to the root of the given document. @Field - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.
  34. 34. Custom Converters
  35. 35. MongoTemplate features • MapReduce operations support • Group operations support • Aggregation Framework support (introduced in Mongo version 2.2) • Index and Collection management • Executing Commands • Lifecycle Events (do smth Before,After x Save,Load,Convert) • Exception Translation (DataAccessException and its children) • GridFs support
  36. 36. Repository
  37. 37. MongoDB JSON based query methods and field restriction @org.springframework.data.mongodb.repository.Query Simple querying: Querying with field restriction:
  38. 38. QueryDSL and type-safe query methods MongoDB repository support integrates with the QueryDSL project which provides a means to perform type-safe queries in Java. Features: • Code completion in IDE (all properties, methods and operations can be expanded in your favorite Java IDE) • Almost no syntactically invalid queries allowed (type-safe on all levels) • Domain types and properties can be referenced safely (no Strings involved!) • Adopts better to refactoring changes in domain types • Incremental query definition is easier
  39. 39. QueryDSL and type-safe query methods 1) QueryDslPredicateExecutor interface is provided for you by Spring Data 2) Just make your repository interface extend QueryDslPredicateExecutor 3) Enjoy type-safe queries
  40. 40. Miscellaneous • Cross store support • Logging support • JMX support

×