Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction to Spring Data Greg Turnquist @gregturn [email protected] github.com/gregturn

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ GregLTurnquist.com/list

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015 Why are we still writing stuff like…

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015 Why are we still writing stuff like… select * from person where person.first_name = %1

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ With Spring Data, you don’t have to!

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SQL

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SQL JPL HQL EJBQL CQL CQRS Mongo Query Language ????

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Declaring Repositories 5 public interface PersonRepository extends CrudRepository { // This interface and a “Person” class is // ALL YOU NEED! }

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Writing Queries 6 public interface PersonRepository extends CrudRepository { List findByFirstname(String f) List findByFirstnameAndLastname(String f, String l) List findByFirstnameAndAddressZipcode(String f, Zipcode z) List findTop10ByFirstname(…) // or findFirst10ByFirstname List findDistinctPeopleByFirstname(…) List findByFirstnameAndLastnameAllIgnoreCase(…) List findByFirstnameOrderByLastnameAsc(…) List findByLastnameIsNull(…) }

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Why stop there? 7 public interface CoolPersonRepo extends CrudRepository { Stream findByLastname(String lastname) @Async Future findByLastname(…) @Async CompletableFuture findByLastname(…) @Async ListenableFuture findByLastname(…) }

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Paging and Sorting 8 public interface CoolPersonRepo extends CrudRepository { Page findAll(Pageable p) Page findByFirstname(String f, Pageable p) List findAll(Sort s) List findByFirstname(String f, Sort s) Page findByFirstname(String f, Pageable p, Sort s) }

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Not quite enough? 9 public interface CoolPersonRepo extends CrudRepository { @Query("select u from User u where u.emailAddress = ?1”) User myCoolCustomQuery(String e) @Query("select u from User u where u.firstname = :firstname”) User findByFirstname(@Param("firstname") String f) @Query("select u from #{#entityName} u where u.lastname = ?1”) List findByLastname(String lastname) List findAll(Specification spec) }

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What else? § Repository solutions are VERY handy, but not always enough. § Each data store has a custom template • JpaTemplate • MongoTemplate • Neo4jTemplate • RedisTemplate • ….and so forth 10

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Nice JPA stuff, but what about MongoDB, …? § Spring Data supports • JPA • MongoDB • Neo4j • GemFire a.k.a. Apache Geode • Cassandra • Couchbase • Apache Solr • Elasticsearch • Amazon DynamoDB • (Redis is coming) • ….and ANY repository-based data store 11

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Check it out! 12 Visit http://projects.spring.io/spring-data/

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction to Spring Data Greg Turnquist @gregturn [email protected] github.com/gregturn